Class: NestedRecord::Methods::Many

Inherits:
NestedRecord::Methods show all
Defined in:
lib/nested_record/methods/many.rb

Instance Method Summary collapse

Methods inherited from NestedRecord::Methods

#attributes_writer_method_name, #define, #define_attributes_writer_method, #reader_method_name, #rewrite_attributes_method_name, #upsert_attributes_method_name, #writer_method_name

Constructor Details

#initialize(setup) ⇒ Many

Returns a new instance of Many.



3
4
5
6
7
8
9
10
11
# File 'lib/nested_record/methods/many.rb', line 3

def initialize(setup)
  super
  define :reader
  define :writer
  define :rewrite_attributes
  define :upsert_attributes
  define :validation
  define_attributes_writer_method
end

Instance Method Details

#reader_method_bodyObject



17
18
19
20
21
22
23
# File 'lib/nested_record/methods/many.rb', line 17

def reader_method_body
  setup = @setup
  ivar = :"@_#{@setup.name}_collection_proxy"
  proc do
    instance_variable_get(ivar) || instance_variable_set(ivar, setup.collection_proxy_class.new(self))
  end
end

#rewrite_attributes_method_bodyObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nested_record/methods/many.rb', line 76

def rewrite_attributes_method_body
  setup = @setup
  writer_method_name = self.writer_method_name
  proc do |data|
    attributes_collection =
      if data.is_a? Hash
        data.values
      else
        data
      end
    collection = setup.collection_class.new
    attributes_collection.each do |attributes|
      attributes = attributes.stringify_keys
      next if setup.reject_if_proc&.call(attributes)
      collection.build(attributes)
    end
    public_send(writer_method_name, collection)
  end
end

#upsert_attributes_method_bodyObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nested_record/methods/many.rb', line 45

def upsert_attributes_method_body
  setup = @setup
  name = @setup.name
  proc do |data|
    attributes_collection =
      if data.is_a? Hash
        data.values
      else
        data
      end
    collection = public_send(name)
    attributes_collection.each do |attributes|
      attributes = attributes.stringify_keys
      next if setup.reject_if_proc&.call(attributes)

      pkey_check = setup.primary_check(attributes['type'])
      unless pkey_check
        raise NestedRecord::ConfigurationError, 'You should specify a primary_key when using :upsert strategy'
      end

      pkey = pkey_check.build_pkey(attributes)

      if (record = collection.find_by(pkey))
        record.assign_attributes(attributes)
      else
        collection.__build__(attributes)
      end
    end
  end
end

#validation_method_bodyObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nested_record/methods/many.rb', line 96

def validation_method_body
  name = @setup.name
  proc do
    collection = public_send(name)
    collection.map.with_index do |record, index|
      next true if record.valid?
      record.errors.each do |attribute, message|
        error_attribute = "#{name}[#{index}].#{attribute}"
        errors[error_attribute] << message
        errors[error_attribute].uniq!
      end
      record.errors.details.each_key do |attribute|
        error_attribute = "#{name}[#{index}].#{attribute}"
        record.errors.details[attribute].each do |error|
          errors.details[error_attribute] << error
          errors.details[error_attribute].uniq!
        end
      end
      false
    end.all?
  end
end

#validation_method_nameObject



13
14
15
# File 'lib/nested_record/methods/many.rb', line 13

def validation_method_name
  :"validate_associated_records_for_#{@setup.name}"
end

#writer_method_bodyObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nested_record/methods/many.rb', line 25

def writer_method_body
  setup = @setup
  proc do |records|
    collection_class = setup.collection_class
    if records.is_a? collection_class
      collection = records.dup
    else
      collection = collection_class.new
      records.each { |record| collection << record }
    end
    collection.group_by { |record| setup.primary_check(record.read_attribute('type')) }.each do |check, records|
      next unless check
      records.each do |record|
        check.perform!(collection, record)
      end
    end
    super(collection)
  end
end