Module: Labelized::Support

Defined in:
lib/labelized/support.rb

Class Method Summary collapse

Instance Method Summary collapse

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
  options = labels[-1].kind_of?(Hash) ? labels.slice!(-1) : {}
  labels = labels.map(&:to_sym)
  [labels, options]
end

.singular?(str) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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, options = Support.extract_params(params)
  scopes = [options[:scope]].flatten
  
  self.setup_labelized_options(options) 
  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 setup_labelized_options(options)
  write_inheritable_attribute(:labelized_options, options)
  class_inheritable_reader(:labelized_options)
end