Module: ActsAsHasMany::ClassMethods
- Defined in:
- lib/acts_as_has_many.rb
Instance Method Summary collapse
- #acts_as_accepts_nested_attributes_for(attribute, _options = {}) ⇒ Object
- #acts_as_has_many(attribute, options = {}) ⇒ Object
Instance Method Details
#acts_as_accepts_nested_attributes_for(attribute, _options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/acts_as_has_many.rb', line 28 def acts_as_accepts_nested_attributes_for(attribute, = {}) define_method("#{attribute}_attributes=".to_sym) do |associates| writeable_associates = associates.values.map do |associate| associate.except('_destroy') unless associate['_destroy'].present? end.compact public_send("#{attribute}=".to_sym, writeable_associates) end end |
#acts_as_has_many(attribute, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/acts_as_has_many.rb', line 11 def acts_as_has_many(attribute, = {}) validates attribute, associated: true column = [:attribute].presence || attribute klass = [:class_name].presence || attribute.to_s.classify define_method(attribute.to_sym) do @options ||= read_attribute(column).map do |option| klass.constantize.new(option) end end define_method("#{attribute}=".to_sym) do |value| write_attribute(column, value.as_json) end end |