Class: Wordpress::Category

Inherits:
Taxonomy show all
Defined in:
app/models/wordpress/category.rb

Class Method Summary collapse

Methods inherited from Taxonomy

#breadcrumbs, find_sti_class, #set_defaults, sti_name, #title

Methods inherited from WpBase

prefix_table_name

Class Method Details

.cloudObject



5
6
7
8
9
# File 'app/models/wordpress/category.rb', line 5

def self.cloud
  cats = all.reject{|r| r.posts.empty?}
  total_posts = cats.inject(0){|sum, t| sum += t.count}
  cats.map{|t| {category: t, size: 1.0 + (t.count / total_posts.to_f * 2)}}.sort_by{|sb| sb[:category].slug}
end

.find_or_create(category_name, parent = nil) ⇒ Object



11
12
13
14
15
16
# File 'app/models/wordpress/category.rb', line 11

def self.find_or_create category_name, parent = nil
  raise "category name can't be blank" if category_name.blank?
  parent_id = parent.try(:id).to_i
  category = joins(:term).where(wp_terms: {name: category_name}, parent: parent_id).first
  category ||= create!(description: category_name, term_id: Term.create!(name: category_name).id, parent: parent_id)
end