Module: ActiveRecord::JsonAssociations

Defined in:
lib/active_record/json_associations.rb,
lib/active_record/json_associations/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Method Summary collapse

Instance Method Details

#belongs_to_many(many, class_name: nil, touch: nil) ⇒ 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
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
# File 'lib/active_record/json_associations.rb', line 46

def belongs_to_many(many, class_name: nil, touch: nil)
  one = many.to_s.singularize
  one_ids = :"#{one}_ids"
  one_ids_equals = :"#{one_ids}="
  many_equals = :"#{many}="
  many_eh = :"#{many}?"

  class_name ||= one.classify

  unless columns_hash[one_ids.to_s].type == :json
    raise ArgumentError, "#{one_ids} column must be of type :json"
  end

  if touch
    after_commit do
      unless no_touching?
        old_ids, new_ids = saved_changes[one_ids.to_s]
        ids = Array(send(one_ids)) | Array(old_ids) | Array(new_ids)
        class_name.constantize.where(self.class.primary_key => ids).touch_all
      end
    end
  end

  extend Module.new {
    define_method :"#{one_ids}_including" do |id|
      raise "can't query for a record that does not have an id!" if id.blank?
      if id.is_a?(Hash)
        Array(id[:any]).inject(none) do |context, id|
          context.or(FIELD_INCLUDE_SCOPE_BUILDER_PROC.call(self, one_ids, id))
        end
      else
        FIELD_INCLUDE_SCOPE_BUILDER_PROC.call(self, one_ids, id)
      end
    end
  }

  include Module.new {
    define_method one_ids do
      super().tap do |value|
        return send(one_ids_equals, []) if value.nil?
      end
    end

    define_method one_ids_equals do |ids|
      super Array(ids).select(&:present?).map(&:to_i).uniq
    end

    define_method many do
      klass = class_name.constantize
      ids = send(one_ids).map(&:to_i)
      scope = klass.where(klass.primary_key => ids)
      ORDER_BY_IDS_PROC.call(scope, ids)
    end

    define_method many_equals do |collection|
      send one_ids_equals, collection.map(&:id)
    end

    define_method many_eh do
      send(one_ids).any?
    end
  }
end

#has_many(many, scope = nil, **options, &extension) ⇒ Object



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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/active_record/json_associations.rb', line 110

def has_many many, scope = nil, **options, &extension
  unless (scope.is_a?(Hash) && scope[:json_foreign_key]) || (options.is_a?(Hash) && options[:json_foreign_key])
    return super
  end

  if scope.is_a?(Hash)
    options = scope
    scope   = nil
  end

  one = many.to_s.singularize
  one_ids = :"#{one}_ids"
  one_ids_equals = :"#{one_ids}="
  many_equals = :"#{many}="
  many_eh = :"#{many}?"
  build_one = :"build_#{one}"
  create_one = :"create_#{one}"
  create_one_bang = :"create_#{one}!"

  class_name = options[:class_name] || one.classify

  foreign_key = options[:json_foreign_key]
  foreign_key = :"#{model_name.singular}_ids" if foreign_key == true

  pending_associations_var = :"@pending_#{many}"

  after_create do
    if (pending = instance_variable_get(pending_associations_var))
      instance_variable_set(pending_associations_var, nil)
      send(many_equals, pending)
    end
  end

  include Module.new {
    define_method one_ids do
      send(many).pluck(:id)
    end

    define_method one_ids_equals do |ids|
      klass = class_name.constantize
      normalized_ids = Array(ids).select(&:present?).map(&:to_i).uniq
      send many_equals, klass.find(normalized_ids)
    end

    define_method many do
      klass = class_name.constantize
      FIELD_INCLUDE_SCOPE_BUILDER_PROC.call(klass, foreign_key, id)
    end

    define_method many_equals do |collection|
      if new_record?
        instance_variable_set(pending_associations_var, collection)
      else
        collection.each do |record|
          new_id_array = Array(record.send(foreign_key)) | [id]
          record.update foreign_key => new_id_array
        end
      end
    end

    define_method many_eh do
      send(many).any?
    end

    define_method build_one do |attributes={}|
      klass = class_name.constantize
      klass.new attributes.merge!(foreign_key => [id])
    end

    define_method create_one do |attributes={}|
      klass = class_name.constantize
      klass.create attributes.merge!(foreign_key => [id])
    end

    define_method create_one_bang do |attributes={}|
      klass = class_name.constantize
      klass.create! attributes.merge!(foreign_key => [id])
    end
  }
end