Class: Gluttonberg::AssetCategory
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Gluttonberg::AssetCategory
- Defined in:
- app/models/gluttonberg/asset_category.rb
Class Method Summary collapse
-
.build_defaults ⇒ Object
Ensure the default categories exist in the database.
-
.ensure_exists(name, unknown = false) ⇒ Object
find category if not exists it makes new one.
-
.find_assets_by_category(category_name) ⇒ Object
Find assets for a category.
-
.find_assets_by_category_and_collection(category_name, collection) ⇒ Object
Find assets for a category within a collection.
-
.method_missing(methId, *args) ⇒ Object
Dynamic methods for categories.
Class Method Details
.build_defaults ⇒ Object
Ensure the default categories exist in the database.
29 30 31 32 33 34 35 |
# File 'app/models/gluttonberg/asset_category.rb', line 29 def self.build_defaults ensure_exists('audio') ensure_exists('image') ensure_exists('video') ensure_exists('document') ensure_exists(Library::UNCATEGORISED_CATEGORY, true) end |
.ensure_exists(name, unknown = false) ⇒ Object
find category if not exists it makes new one
80 81 82 83 84 85 86 87 88 |
# File 'app/models/gluttonberg/asset_category.rb', line 80 def self.ensure_exists(name, unknown=false) cat = where(:name => name).first if cat then cat.unknown = unknown cat.save else cat = create(:name => name, :unknown => unknown) end end |
.find_assets_by_category(category_name) ⇒ Object
Find assets for a category. It supports comma seperated categories It also works for ‘all’
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/gluttonberg/asset_category.rb', line 39 def self.find_assets_by_category(category_name) if category_name == "all" || category_name.blank? then # ignore asset category if user selects 'all' from category Asset.includes(:asset_type) else req_categories = AssetCategory.where(:name => category_name.split(",")).all # if category is not found then raise exception if req_categories.blank? raise ActiveRecord::RecordNotFound else asset_types = [] req_categories.each do |req_category| asset_types << req_category.asset_types.all.collect{|type| type.id} end asset_types = asset_types.flatten unless asset_types.blank? Asset.where(:asset_type_id => asset_types).includes(:asset_type) end end # category#all end |
.find_assets_by_category_and_collection(category_name, collection) ⇒ Object
Find assets for a category within a collection. It supports comma seperated categories It also works for ‘all’
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/gluttonberg/asset_category.rb', line 61 def self.find_assets_by_category_and_collection(category_name, collection) if category_name == "all" || category_name.blank? then collection.assets else req_categories = AssetCategory.where(:name => category_name.split(",")).all # if category is not found then raise exception if req_categories.blank? raise ActiveRecord::RecordNotFound else asset_types = [] req_categories.each do |req_category| asset_types << req_category.asset_types.all.collect{|type| type.id} end collection.assets.where({:asset_type_id => asset_types }) unless asset_types.blank? end end end |
.method_missing(methId, *args) ⇒ Object
Dynamic methods for categories.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/models/gluttonberg/asset_category.rb', line 14 def self.method_missing(methId, *args) method_info = methId.id2name.split('_') if method_info.length == 2 then if method_info[1] == 'category' then cat_name = method_info[0] if cat_name then cat = where(:name => cat_name).first return cat unless cat.blank? end end end raise NoMethodError end |