Sha256: eea17d952b6f0f12e6a5c6f14906257f42221a8fb79bacfa526a70bbe92e64eb

Contents?: true

Size: 1.13 KB

Versions: 32

Compression:

Stored size: 1.13 KB

Contents

class CategoryType < ActiveRecord::Base
  has_many :categories
  validates_presence_of :name
  validates_uniqueness_of :name
  is_searchable
  named_scope :named, lambda {|name| {:conditions => ['category_types.name = ?', name] } }
  
  # Return a map when the key is category type id as a string
  # and the value is an array of arrays, each entry having 
  # the first value as the category path and the second value
  # being the category id as a string
  def self.category_map
    all.inject(Hash.new([])) do |map, ct| 
      map[ct.id.to_s] = ct.category_list.map{|c| [c.path, c.id.to_s]}
      map
    end
  end
  
  # This is used to get the full list of categories for this category type in the correct order.
  def category_list(order="name")
    list = []
    fn = lambda do |cat|
      list << cat
      cat.children.all(:order => order).each{|c| fn.call(c)}
    end
    categories.top_level.all(:order => order).each{|cat| fn.call(cat)}
    list
  end
  
  def cannot_be_deleted_message
    categories.count.zero? ? nil : "This cannot be deleted because it is in use in #{categories.count} #{"category".pluralize(categories.count)}"
  end
  
end

Version data entries

32 entries across 32 versions & 10 rubygems

Version Path
SFEley-browsercms-3.0.2 app/models/category_type.rb
buzzware-browsercms-3.0.2 app/models/category_type.rb
coredumplings-browsercms-3.0.0 app/models/category_type.rb
nate-browsercms-3.0.210 app/models/category_type.rb
nate-browsercms-3.0.211 app/models/category_type.rb
we5-browsercms-3.0.1.1 app/models/category_type.rb
webficient-browsercms-3.0.1 app/models/category_type.rb
webficient-browsercms-3.0.2 app/models/category_type.rb
webficient-browsercms-3.0.3 app/models/category_type.rb
webficient-browsercms-3.0.4 app/models/category_type.rb
browsercms-3.1.5 app/models/category_type.rb
browsercms-3.1.4 app/models/category_type.rb
browsercms-3.1.3 app/models/category_type.rb
bf4-browsercms-3.1.0 app/models/category_type.rb
drujensen-browsercms-3.2.0 app/models/category_type.rb
browsercms-3.1.2 app/models/category_type.rb
browsercms-3.1.1 app/models/category_type.rb
browsercms-3.1.0 app/models/category_type.rb
we5-browsercms-3.1.0 app/models/category_type.rb
browsercms-3.0.6 app/models/category_type.rb