Module: Labelized::Support
- Defined in:
- lib/labelized/support.rb
Class Method Summary collapse
Instance Method Summary collapse
- #is_labelized? ⇒ Boolean
-
#setup_label_scope(scopes) ⇒ Object
Sets up scoping for labels and label sets Typically the labeled item is passed to the scope So label_set with a :community_id scope, will look for labels with a community_id that have the same value as the community_id on the thing being labled.
- #setup_labelized(params) ⇒ Object
- #setup_labelized_label_set_names(names) ⇒ Object
- #setup_labelized_options(options) ⇒ Object
Class Method Details
.extract_params(*params) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/labelized/support.rb', line 9 def self.extract_params(*params) labels = params.to_a.flatten.compact = labels[-1].kind_of?(Hash) ? labels.slice!(-1) : {} labels = labels.map(&:to_sym) [labels, ] end |
.singular?(str) ⇒ Boolean
5 6 7 |
# File 'lib/labelized/support.rb', line 5 def self.singular?(str) str.to_s.pluralize != str.to_s && str.to_s.singularize == str.to_s end |
Instance Method Details
#is_labelized? ⇒ Boolean
46 47 48 |
# File 'lib/labelized/support.rb', line 46 def is_labelized? true end |
#setup_label_scope(scopes) ⇒ Object
Sets up scoping for labels and label sets Typically the labeled item is passed to the scope So label_set with a :community_id scope, will look for labels with a community_id that have the same value as the community_id on the thing being labled
29 30 31 32 33 34 |
# File 'lib/labelized/support.rb', line 29 def setup_label_scope(scopes) return if scopes.blank? self.class_eval do scope :label_scope, lambda {|labeled| where(*scopes.collect{|s| {s => labeled.send(s)}}.flatten)} end end |
#setup_labelized(params) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/labelized/support.rb', line 16 def setup_labelized(params) label_sets, = Support.extract_params(params) scopes = [[:scope]].flatten self.() self.setup_labelized_label_set_names(label_sets) self.setup_label_scope(scopes) end |
#setup_labelized_label_set_names(names) ⇒ Object
41 42 43 44 |
# File 'lib/labelized/support.rb', line 41 def setup_labelized_label_set_names(names) write_inheritable_attribute(:labelized_label_set_names, names) class_inheritable_reader(:labelized_label_set_names) end |
#setup_labelized_options(options) ⇒ Object
36 37 38 39 |
# File 'lib/labelized/support.rb', line 36 def () write_inheritable_attribute(:labelized_options, ) class_inheritable_reader(:labelized_options) end |