Module: Para::Ext::ActiveRecord::NestedAttributesClassMethods

Defined in:
lib/para/ext/active_record_nested_attributes.rb

Constant Summary collapse

PARA_REJECT_ALL_BLANK_PROC =

Override default :reject_all proc to handle fake ids as empty keys, avoiding the model to be created with only its fake id.

proc { |attributes|
  attributes.all? { |key, value|
    (key == 'id' && value.to_s.match(/\A__/)) || key == '_destroy' || value.blank?
  }
}

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributes_for(*attr_names) ⇒ Object

Intercept the ‘reject_if: :all_blank` option to also consider fake ids as blank fields and avoid empty params assignation



43
44
45
46
47
# File 'lib/para/ext/active_record_nested_attributes.rb', line 43

def accepts_nested_attributes_for(*attr_names)
  options = attr_names.extract_options!
  options[:reject_if] = PARA_REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
  super(*attr_names, options)
end