Module: ActiveRecord::ActsAs::HasEav::ClassMethods
- Defined in:
- lib/has_eav.rb
Instance Method Summary collapse
-
#class_eav_attributes ⇒ Object
class accessor - when the superclass != AR::Base asume we are in STI mode.
-
#eav_attribute(name, type = String) ⇒ Object
Add an other attribute to the class list.
-
#eav_class ⇒ Object
class accessor - when the superclass != AR::Base asume we are in STI mode.
-
#has_eav(opts = {}, &block) ⇒ Object
Specify that the ActiveModel is an EAV model.
Instance Method Details
#class_eav_attributes ⇒ Object
class accessor - when the superclass != AR::Base asume we are in STI mode
61 62 63 64 65 |
# File 'lib/has_eav.rb', line 61 def class_eav_attributes # :nodoc: superclass != ActiveRecord::Base ? superclass.class_eav_attributes : @eav_attributes end |
#eav_attribute(name, type = String) ⇒ Object
Add an other attribute to the class list
53 54 55 56 57 |
# File 'lib/has_eav.rb', line 53 def eav_attribute name, type = String name = name.to_s if !name.is_a? String self.class_eav_attributes[name] = type end |
#eav_class ⇒ Object
class accessor - when the superclass != AR::Base asume we are in STI mode
69 70 71 |
# File 'lib/has_eav.rb', line 69 def eav_class # :nodoc: superclass != ActiveRecord::Base ? superclass.eav_class : @eav_class end |
#has_eav(opts = {}, &block) ⇒ Object
Specify that the ActiveModel is an EAV model
Usage
# specifiy eav_attributes at instance level has_eav :through => :some_class_with_name_and_value_attributes def available_eav_attributes
case self.origin
when "remote"
%(remote_ip user_agent)
when "local"
%(user)
end + [ :uniq_id ]
end
# specify some eav_attributes at class level has_eav :through => :x_attributes do
eav_attribute :remote_ip
eav_attribute :uniq_id
end
Mixing class and instance defined EAV attributes
You can define EAV attributes both in class and instance context and they will be both adhered
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/has_eav.rb', line 33 def has_eav opts={}, &block klass = opts.delete :through raise( "Eav Class cannot be nil. Specify a class using " + "has_eav :through => :class" ) if klass.blank? class_eval do after_save :save_eav_attributes end @eav_class = klass.to_s.camelize.constantize @eav_attributes = {} yield if block_given? send :include, ActiveRecord::ActsAs::HasEav::InstanceMethods end |