Class: ActiveType::NestedAttributes::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_type/nested_attributes/association.rb

Direct Known Subclasses

NestsManyAssociation, NestsOneAssociation

Instance Method Summary collapse

Constructor Details

#initialize(owner, target_name, options = {}) ⇒ Association

Returns a new instance of Association.



11
12
13
14
15
16
17
18
19
# File 'lib/active_type/nested_attributes/association.rb', line 11

def initialize(owner, target_name, options = {})
  options.assert_valid_keys(:build_scope, :find_scope, :scope, :allow_destroy, :reject_if)

  @owner = owner
  @target_name = target_name.to_sym
  @allow_destroy = options.fetch(:allow_destroy, false)
  @reject_if = options.delete(:reject_if)
  @options = options.dup
end

Instance Method Details

#assign_attributes(parent, attributes) ⇒ Object

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/active_type/nested_attributes/association.rb', line 21

def assign_attributes(parent, attributes)
  raise NotImplementedError
end

#save(parent) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_type/nested_attributes/association.rb', line 25

def save(parent)
  keep = assigned_children(parent)
  changed_children(parent).each do |child|
    if child.marked_for_destruction?
      child.destroy if child.persisted?
      keep.delete(child)
    else
      child.save(:validate => false) or raise ActiveRecord::Rollback
    end
  end
  assign_children(parent, keep)
end

#validate(parent) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_type/nested_attributes/association.rb', line 38

def validate(parent)
  changed_children(parent).each do |child|
    unless child.valid?
      child.errors.each do |attribute, message|
        attribute = "#{@target_name}.#{attribute}"
        parent.errors[attribute] << message
        parent.errors[attribute].uniq!
      end
    end
  end
end