Module: E9Attributes::Model::ClassMethods
- Defined in:
- lib/e9_attributes/model.rb
Instance Method Summary collapse
-
#has_record_attributes(*attributes) ⇒ Object
By default, it reformats the attributes passed into association names and defines them.
Instance Method Details
#has_record_attributes(*attributes) ⇒ Object
By default, it reformats the attributes passed into association names and defines them. To add a record attribute without defining an association name, pass the association names literally and specify the option :skip_name_format. This will cause the method to skip adding associations for any attribute not ending in “_attributes”, which you must add manually, e.g.
has_many :users
accepts_nested_attributes_for :users, :allow_destroy => true
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/e9_attributes/model.rb', line 16 def has_record_attributes(*attributes) = attributes. .symbolize_keys! class_inheritable_accessor :record_attributes attributes.flatten! attributes.map!(&:to_s) unless [:skip_name_format] attributes.map! do |a| a = a.classify a = a =~ /Attribute$/ ? a : "#{a}Attribute" a.constantize rescue next a.underscore.pluralize end.compact end self.record_attributes = attributes has_many :record_attributes, :as => :record self.record_attributes.select {|r| r =~ /attributes$/ }.each do |association_name| has_many association_name.to_sym, :class_name => association_name.classify, :as => :record accepts_nested_attributes_for association_name.to_sym, :allow_destroy => true, :reject_if => :reject_record_attribute? end end |