Module: Ripple::NestedAttributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/ripple/nested_attributes.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

UNASSIGNABLE_KEYS =
%w{ _destroy }
TRUE_VALUES =
[ true, "true", 1, "1", "yes", "ok", "y" ]

Instance Method Summary collapse

Instance Method Details

#assign_to_or_mark_for_destruction(record, attributes, association_name, allow_destroy) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/ripple/nested_attributes.rb', line 251

def assign_to_or_mark_for_destruction(record, attributes, association_name, allow_destroy)
  if has_destroy_flag?(attributes) && allow_destroy
    (self.marked_for_destruction[association_name] ||= []) << record
  else
    record.attributes = attributes.except(*UNASSIGNABLE_KEYS)
  end
end

#call_reject_if(association_name, attributes) ⇒ Object



267
268
269
270
271
272
273
274
275
# File 'lib/ripple/nested_attributes.rb', line 267

def call_reject_if(association_name, attributes)
  attributes = attributes.stringify_keys
  case callback = nested_attributes_options[association_name][:reject_if]
  when Symbol
    method(callback).arity == 0 ? send(callback) : send(callback, attributes)
  when Proc
    callback.call(attributes)
  end
end

#has_destroy_flag?(hash) ⇒ Boolean

Returns:



259
260
261
# File 'lib/ripple/nested_attributes.rb', line 259

def has_destroy_flag?(hash)
  TRUE_VALUES.include?(hash.stringify_keys['_destroy'])
end

#reject_new_record?(association_name, attributes) ⇒ Boolean

Returns:



263
264
265
# File 'lib/ripple/nested_attributes.rb', line 263

def reject_new_record?(association_name, attributes)
  has_destroy_flag?(attributes) || call_reject_if(association_name, attributes)
end