Module: Banyan::Categorizable::ClassMethods

Defined in:
lib/banyan/categorizable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_categorizable(opts = {}) ⇒ Object

Add categories to model

Parameters:

  • opts (Hash) (defaults to: {})

    Options for connection with categories

Options Hash (opts):

  • :single (Boolean)

    Should categories be connected via has_one instead of has_many? Default: false

  • :as (String)

    Association under which category will handle specified categoriable list. Default: class_name.underscore.gsub(‘/’,‘_’) + ‘_categorizations’



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/banyan/categorizable.rb', line 12

def acts_as_categorizable(opts = {})
  single = !!opts.delete(:single)
  as = opts.delete(:as) || (self.name.underscore.gsub('/','___') + '_categorizations')

  if single
    has_one :categorization, :as => :categorizable, :class_name => "::Banyan::Categorization", :dependent => :destroy
    has_one :category, opts.merge(:through => :categorization)
  else
    has_many :categorizations, :as => :categorizable, :class_name => "::Banyan::Categorization", :dependent => :destroy
    has_many :categories, opts.merge(:through => :categorizations, :uniq => true)
  end

  Banyan::Category.has_many as, :through => :categorizations, :source => :categorizable, :source_type => self.name
end

#acts_as_group_categorizable(opts = {}) ⇒ Object

Add category groups to model

Parameters:

  • opts (Hash) (defaults to: {})

    Options for connection with category groups

Options Hash (opts):

  • :single (Boolean)

    Should groups be connected via has_one instead of has_many? Default: false



30
31
32
33
34
35
36
37
38
# File 'lib/banyan/categorizable.rb', line 30

def acts_as_group_categorizable(opts = {})
  single = !!opts.delete(:single)

  if single
    has_one :category_group, opts.merge(:as => :group_categorizable, :class_name => "Banyan::CategoryGroup")
  else
    has_many :category_groups, opts.merge(:as => :group_categorizable, :class_name => "Banyan::CategoryGroup", :uniq => true)
  end
end