Sha256: 4a1d6b81aee4783d09ad697690a2424e2df6c2e608154d0336931c2350f3fe6b
Contents?: true
Size: 798 Bytes
Versions: 11
Compression:
Stored size: 798 Bytes
Contents
module Wordpress class Category < Taxonomy has_many :sub_categories, class_name: "Wordpress::Category", foreign_key: :parent, primary_key: :id 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 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 end end
Version data entries
11 entries across 11 versions & 1 rubygems