Module: NestedAttributeReassignable::ClassMethods

Defined in:
lib/nested_attribute_reassignable.rb

Overview

Yes, this could use refactoring love, I do not have time right now D: Just go by the tests.

Instance Method Summary collapse

Instance Method Details

#reassignable_nested_attributes_for(association_name, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
114
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
# File 'lib/nested_attribute_reassignable.rb', line 55

def reassignable_nested_attributes_for(association_name, *args)
  options = args.extract_options!.symbolize_keys
  options.update({ :allow_destroy => true })
  nonexistent_id = options.delete(:nonexistent_id) || :raise

  accepts_nested_attributes_for(association_name, options.except(:lookup_key))

  define_method "#{association_name}_attributes=" do |attributes|
    reflection_klass  = self.class._reflect_on_association(association_name)
    association_klass = reflection_klass.klass
    association       = association(association_name)
    lookup_key        = options[:lookup_key] || association.klass.primary_key

    Helper.symbolize_keys!(attributes)

    if attributes.is_a?(Array)
      id_attribute_sets = attributes.select { |a| a.has_key?(:id) }

      ids = attributes.map { |a| a[:id] }
      children = association_klass.where(lookup_key => ids)

      # If we're deleting or destroying, we want to validate the record in question
      # is actually part of this relationship
      if id_attribute_sets.any? { |set| Helper.has_destroy_flag?(set) || Helper.has_delete_flag?(set) }
        existing_associated = association.scope.where(lookup_key => ids)
      end

      id_attribute_sets.each do |id_attributes|
        if existing_record = children.find { |c| c.send(lookup_key).to_s == id_attributes[:id].to_s }
          if Helper.has_destroy_flag?(id_attributes)
            if record = existing_associated.find { |e| e.send(lookup_key).to_s == id_attributes[:id].to_s }
              record.mark_for_destruction
              association.add_to_target(record, :skip_callbacks)
            else
              raise_nested_attributes_record_not_found!(association_name, id_attributes[:id])
            end
          elsif Helper.has_delete_flag?(id_attributes)
            if record = existing_associated.find { |e| e.send(lookup_key).to_s == id_attributes[:id].to_s }
              association.add_to_target(record, :skip_callbacks)
              send(association_name).delete(record)
            else
              raise_nested_attributes_record_not_found!(association_name, id_attributes[:id])
            end
          else
            id_attributes[lookup_key] = id_attributes[:id]
            existing_record.assign_attributes(id_attributes.except(:id))
            self.send(association_name).concat(existing_record)
          end
        else
          if nonexistent_id == :create
            new_record = association_klass.new(lookup_key => id_attributes[:id])
            self.send(association_name).concat(new_record)
          else
            raise_nested_attributes_record_not_found!(association_name, id_attributes[:id])
          end
        end
      end
      non_id_attribute_sets = attributes.reject { |a| a.has_key?(:id) }
      non_id_attribute_sets.each do |non_id_attributes|
        self.send(association_name).build(non_id_attributes)
      end
      self.association(association_name).loaded!
    else
      if attributes[:id]
        if Helper.has_destroy_flag?(attributes)
          self.send(association_name).mark_for_destruction
        elsif Helper.has_delete_flag?(attributes)
          send("#{association_name}=", nil)
        elsif existing_record = association_klass.find_by(lookup_key => attributes[:id])
          attributes[lookup_key] = attributes.delete(:id)
          existing_record.assign_attributes(attributes)
          self.send("#{association_name}=", existing_record)
        else
          if nonexistent_id == :create
            new_record = association_klass.new(lookup_key => attributes[:id])
            self.send("#{association_name}=", new_record)
          else
            raise_nested_attributes_record_not_found!(association_name, attributes[:id])
          end
        end
      else
        reflection  = self.class._reflect_on_association(association_name)
        if reflection.has_one? and send(association_name).present?
          raise RelationExists.new(self, association_name)
        else
          super(attributes)
        end
      end
    end
  end
end