Module: Categoryz3::Categorizable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/categoryz3/categorizable.rb
Overview
Adds the categorizable behavior to models
This will give the model 2 scopes:
* inside_category(category) : Filter objects that belongs to the category or any of it's subcategories
* having_category(category) : Filter objects that belongs only to the category
And 2 new relations:
* direct_category_items : This maps the model to the categories it is directly linked
* child_category_items : This is an auxiliary relation to fast map the objet to any subcategory
Instance Method Summary collapse
-
#categories_list ⇒ Object
Public: Returns the id list of the categories linked to the model.
-
#categories_list=(ids) ⇒ Object
Public: Accepts an array of category ids as parameter and adds all of them to the model.
-
#remove_category(*categories) ⇒ Object
(also: #remove_categories)
Public: Removes a category, or categories, from the model.
Instance Method Details
#categories_list ⇒ Object
Public: Returns the id list of the categories linked to the model
66 67 68 |
# File 'lib/categoryz3/categorizable.rb', line 66 def categories_list categories.map(&:id).join(",") end |
#categories_list=(ids) ⇒ Object
Public: Accepts an array of category ids as parameter and adds all of them to the model
73 74 75 76 77 78 |
# File 'lib/categoryz3/categorizable.rb', line 73 def categories_list=(ids) ids = ids.is_a?(String) ? ids.split(',') : ids self.categories = ids.map do |id| Category.where(id: id).first end end |
#remove_category(*categories) ⇒ Object Also known as: remove_categories
Public: Removes a category, or categories, from the model
Examples:
categorizable_object.remove_category dummy_category
categorizable_object.remove_categories dummy_category1, dummy_category2
56 57 58 59 60 61 |
# File 'lib/categoryz3/categorizable.rb', line 56 def remove_category(*categories) categories.each do |category| direct_category_items.where(category_id: category).destroy_all end @categories = nil end |