Sha256: 58052ae2ea409a0cc72d2e4ecd44c8401ea8961b6752b82f225bacb20b35d8a5

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

module Cms
  class CategoryType < ActiveRecord::Base

    has_many :categories, :class_name => 'Cms::Category'
    validates_presence_of :name
    validates_uniqueness_of :name
    is_searchable
    has_content_type :module => :categorization

    include Concerns::IgnoresPublishing

    def self.named(name)
      where(name: name)
    end

    # 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.order(order).each { |c| fn.call(c) }
      end
      categories.top_level.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_unless_one(categories.count)}"
    end

  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
browsercms-artirix-4.0.4 app/models/cms/category_type.rb
browsercms-artirix-4.0.3.3 app/models/cms/category_type.rb
browsercms-artirix-4.0.3.2 app/models/cms/category_type.rb
browsercms-artirix-4.0.3.1 app/models/cms/category_type.rb
browsercms-artirix-4.0.3 app/models/cms/category_type.rb
browsercms-artirix-4.0.2 app/models/cms/category_type.rb
browsercms-artirix-4.0.1.1 app/models/cms/category_type.rb
browsercms-artirix-4.0.0.rc1.art4 app/models/cms/category_type.rb
browsercms-4.0.0.rc1 app/models/cms/category_type.rb
browsercms-4.0.0.beta app/models/cms/category_type.rb
browsercms-4.0.0.alpha app/models/cms/category_type.rb