Module: CustomAttributes::HasCustomAttributes::ClassMethods

Defined in:
lib/custom_attributes/has_custom_attributes.rb

Instance Method Summary collapse

Instance Method Details

#add_custom_attribute(value_or_array) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/custom_attributes/has_custom_attributes.rb', line 29

def add_custom_attribute(value_or_array)
  case value_or_array
    when ::CustomAttribute
      value_or_array.owner = self.to_s
      value_or_array.save
    when Array
      opts = {:scope => ::CustomAttributes.scope}
      opts.merge!(value_or_array.pop) if value_or_array.last.is_a?(Hash)
      value_or_array.each do |name|
        add_custom_attribute ::CustomAttribute.new(opts.merge(:name => name))
      end
    when String, Symbol
      add_custom_attribute [value_or_array]
    else
      raise "Uexpected values for custom_attributes <<: #{value_or_array.inspect}"
  end
end

#custom_attributesObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/custom_attributes/has_custom_attributes.rb', line 16

def custom_attributes
  @custom_attributes ||= {}
  @custom_attributes[::CustomAttributes.scope || :no_scope] ||= begin
    arel = ::CustomAttribute.where(:owner => self.to_s)
    if scope = ::CustomAttributes.scope
      arel = arel.where(:scope_type => scope.class.to_s, :scope_id => scope.id)
    else
      arel = arel.where('scope_type IS NULL AND scope_id IS NULL')
    end
    arel
  end
end