Class: Granite::Form::Model::Associations::NestedAttributes::NestedAttributesMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/granite/form/model/associations/nested_attributes.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

REJECT_ALL_BLANK_PROC =
proc { |attributes|
  attributes.all? do |key, value|
    key == DESTROY_ATTRIBUTE || value.blank?
  end
}

Class Method Summary collapse

Class Method Details

.accepts_nested_attributes_for(klass, *attr_names) ⇒ Object



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
75
76
77
78
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 46

def self.accepts_nested_attributes_for(klass, *attr_names)
  options = { allow_destroy: false, update_only: false }
  options.update(attr_names.extract_options!)
  options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only)
  options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank

  NestedAttributesMethodsExtension.ensure_extended!(klass)

  attr_names.each do |association_name|
    reflection = klass.reflect_on_association(association_name)
    unless reflection
      raise ArgumentError,
            "No association found for name `#{association_name}'. Has it been defined yet?"
    end

    klass.nested_attributes_options =
      klass
      .nested_attributes_options
      .merge(association_name.to_sym => options)

    should_validate_nested = klass.respond_to?(:validates_nested) &&
                             !klass.validates_nested?(association_name)
    klass.validates_nested(association_name) if should_validate_nested

    type = (reflection.collection? ? :collection : :one_to_one)
    klass.nested_attributes_methods_module.class_eval <<-METHOD, __FILE__, __LINE__ + 1
    def #{association_name}_attributes=(attributes)
      Granite::Form::Model::Associations::NestedAttributes::NestedAttributesMethods
        .assign_nested_attributes_for_#{type}_association(self, :#{association_name}, attributes)
    end
    METHOD
  end
end

.assign_nested_attributes_for_collection_association(object, association_name, attributes_collection) ⇒ Object

rubocop:disable Layout/LineLength,Metrics/MethodLength



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 115

def self.assign_nested_attributes_for_collection_association(object, association_name, attributes_collection) # rubocop:disable Layout/LineLength,Metrics/MethodLength
  options = object.nested_attributes_options[association_name]

  unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
    raise ArgumentError,
          "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})" # rubocop:disable Layout/LineLength
  end

  check_record_limit!(options[:limit], attributes_collection)

  association = object.association(association_name)
  primary_attribute_name = primary_name_for(association.reflection.klass)

  unless primary_attribute_name
    raise Granite::Form::UndefinedPrimaryAttribute.new(object.class,
                                                       association_name)
  end

  if attributes_collection.is_a? Hash
    keys = attributes_collection.keys
    attributes_collection =
      if keys.include?(primary_attribute_name) || keys.include?(primary_attribute_name.to_sym)
        [attributes_collection]
      else
        attributes_collection.values
      end
  end

  attributes_collection.each do |attributes|
    attributes = attributes.with_indifferent_access

    if attributes[primary_attribute_name].blank?
      association.build(attributes.except(*unassignable_keys(object))) unless reject_new_object?(
        object, association_name, attributes, options
      )
    else
      existing_record = association.target.detect do |record|
        primary_attribute_value = record.attribute(primary_attribute_name)
                                        .type_definition.prepare(attributes[primary_attribute_name])
        record.primary_attribute == primary_attribute_value
      end
      if existing_record
        assign_to(existing_record, attributes) unless call_reject_if(object, association_name, attributes)
        association.target.delete(existing_record) if destroy_flag?(attributes) && options[:allow_destroy]
      elsif association.reflection.embedded?
        unless reject_new_object?(object, association_name, attributes, options)
          association.reflection.klass.with_sanitize(false) do
            association.build(attributes.except(DESTROY_ATTRIBUTE))
          end
        end
      else
        raise Granite::Form::ObjectNotFound.new(object, association_name,
                                                attributes[primary_attribute_name])
      end
    end
  end
end

.assign_nested_attributes_for_one_to_one_association(object, association_name, attributes) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 80

def self.assign_nested_attributes_for_one_to_one_association(object, association_name, attributes)
  options = object.nested_attributes_options[association_name]
  attributes = attributes.with_indifferent_access

  association = object.association(association_name)
  existing_record = association.target
  primary_attribute_name = primary_name_for(association.reflection.klass)
  if existing_record
    primary_attribute = existing_record.attribute(primary_attribute_name)
    if primary_attribute
      primary_attribute_value = primary_attribute.type_definition
                                                 .prepare(attributes[primary_attribute_name])
    end
  end

  should_assign = !primary_attribute ||
                  options[:update_only] ||
                  existing_record.primary_attribute == primary_attribute_value

  if existing_record && should_assign
    assign_to(existing_record, attributes) unless call_reject_if(object, association_name, attributes)
    association.clear if destroy_flag?(attributes) && options[:allow_destroy]
  elsif attributes[primary_attribute_name].present?
    raise Granite::Form::ObjectNotFound.new(object, association_name, attributes[primary_attribute_name])
  elsif !reject_new_object?(object, association_name, attributes, options)
    assignable_attributes = attributes.except(*unassignable_keys(object))

    if existing_record && !existing_record.persisted?
      existing_record.assign_attributes(assignable_attributes)
    else
      association.build(assignable_attributes)
    end
  end
end

.assign_to(object, attributes) ⇒ Object



188
189
190
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 188

def self.assign_to(object, attributes)
  object.assign_attributes(attributes.except(*unassignable_keys(object)))
end

.call_reject_if(object, association_name, attributes) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 200

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

.check_record_limit!(limit, attributes_collection) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 173

def self.check_record_limit!(limit, attributes_collection)
  limit = case limit
          when Symbol
            send(limit)
          when Proc
            limit.call
          else
            limit
          end

  return unless limit && attributes_collection.size > limit

  raise Granite::Form::TooManyObjects.new(limit, attributes_collection.size)
end

.destroy_flag?(hash) ⇒ Boolean

Returns:



192
193
194
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 192

def self.destroy_flag?(hash)
  Types::Boolean.typecast(hash[DESTROY_ATTRIBUTE])
end

.primary_name_for(klass) ⇒ Object



213
214
215
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 213

def self.primary_name_for(klass)
  klass < Granite::Form::Model ? klass.primary_name : 'id'
end

.reject_new_object?(object, association_name, attributes, options) ⇒ Boolean

Returns:



196
197
198
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 196

def self.reject_new_object?(object, association_name, attributes, options)
  options[:update_only] || destroy_flag?(attributes) || call_reject_if(object, association_name, attributes)
end

.unassignable_keys(object) ⇒ Object



209
210
211
# File 'lib/granite/form/model/associations/nested_attributes.rb', line 209

def self.unassignable_keys(object)
  [primary_name_for(object.class), DESTROY_ATTRIBUTE].compact
end