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
65 66 67 68 69 |
# File 'lib/has_eav.rb', line 65 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
57 58 59 60 61 |
# File 'lib/has_eav.rb', line 57 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
73 74 75 |
# File 'lib/has_eav.rb', line 73 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 => “BoundAttribute” 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 51 52 53 54 |
# File 'lib/has_eav.rb', line 33 def has_eav opts={}, &block klass = opts.delete :through klass = klass.to_s if klass.is_a? Symbol klass = klass.camelize raise( "Eav Class cannot be nil. Specify a class using " + "has_eav :through => :class" ) if klass.blank? class_eval do has_many :eav_attributes, :class_name => klass after_save :save_eav_attributes end @eav_class = klass.constantize @eav_attributes = {} yield if block_given? send :include, ActiveRecord::ActsAs::HasEav::InstanceMethods end |