Module: ActiveFedora::NestedAttributes::ClassMethods
- Defined in:
- lib/active_fedora/nested_attributes.rb
Overview
Defines an attributes writer for the specified association(s). If you are using attr_protected
or attr_accessible
, then you will need to add the attribute writer to the allowed list.
Supported options:
- :allow_destroy
-
If true, destroys any members from the attributes hash with a
_destroy
key and a value that evaluates totrue
(eg. 1, ‘1’, true, or ‘true’). This option is off by default. - :reject_if
-
Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash. The hash is passed to the supplied Proc or the method and it should return either
true
orfalse
. When no :reject_if is specified, a record will be built for all attribute hashes that do not have a_destroy
value that evaluates to true. Passing:all_blank
instead of a Proc will create a proc that will reject a record where all the attributes are blank. - :limit
-
Allows you to specify the maximum number of the associated records that can be processed with the nested attributes. If the size of the nested attributes array exceeds the specified limit, NestedAttributes::TooManyRecords exception is raised. If omitted, any number associations can be processed. Note that the :limit option is only applicable to one-to-many associations.
- :update_only
-
Allows you to specify that an existing record may only be updated. A new record may only be created when there is no existing record. This option only works for one-to-one associations and is ignored for collection associations. This option is off by default.
Examples:
# creates avatar_attributes=
accepts_nested_attributes_for :avatar, :reject_if => proc { |attributes| attributes['name'].blank? }
# creates avatar_attributes=
accepts_nested_attributes_for :avatar, :reject_if => :all_blank
# creates avatar_attributes= and posts_attributes=
accepts_nested_attributes_for :avatar, :posts, :allow_destroy => true
Constant Summary collapse
- REJECT_ALL_BLANK_PROC =
proc { |attributes| attributes.all? { |key, value| key == '_destroy' || value.blank? } }
Instance Method Summary collapse
Instance Method Details
#accepts_nested_attributes_for(*attr_names) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_fedora/nested_attributes.rb', line 56 def accepts_nested_attributes_for(*attr_names) = { allow_destroy: false, update_only: false } .update(attr_names.) .assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only) [:reject_if] = REJECT_ALL_BLANK_PROC if [:reject_if] == :all_blank attr_names.each do |association_name| if reflection = _reflect_on_association(association_name) reflection.autosave = true define_autosave_association_callbacks(reflection) ## TODO this ought to work, but doesn't seem to do the class inheritance right = self..dup [association_name.to_sym] = self. = type = (reflection.collection? ? :collection : :one_to_one) generate_association_writer(association_name, type) class_eval <<-eoruby, __FILE__, __LINE__ + 1 remove_possible_method(:#{association_name}_attributes=) def #{association_name}_attributes=(attributes) assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes) end eoruby elsif reflect_on_property(association_name) resource_class.accepts_nested_attributes_for(association_name, ) generate_property_writer(association_name, type) # Delegate the setter to the resource. class_eval <<-eoruby, __FILE__, __LINE__ + 1 remove_possible_method(:#{association_name}_attributes=) def #{association_name}_attributes=(attributes) attribute_will_change!(:#{association_name}) resource.#{association_name}_attributes=(attributes) end eoruby else raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?" end end end |