Module: ActiveRecord::JsonAssociations

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

Constant Summary collapse

VERSION =
"0.12.0"

Instance Method Summary collapse

Instance Method Details

#belongs_to_many(many, class_name: nil, touch: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_record/json_associations.rb', line 14

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


  if ActiveRecord.version >= Gem::Version.new("7.1")
    serialize one_ids, coder: JSON
  else
    serialize one_ids, JSON
  end

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

        if scope.respond_to?(:touch) # AR 6.0+
          scope.touch_all
        elsif self.class.respond_to?(:touch_attributes_with_time) # AR 5.1+
          scope.update_all self.class.touch_attributes_with_time
        else # AR 5.0
          attributes = timestamp_attributes_for_update_in_model.inject({}) do |attributes, key|
            attributes.merge(key => current_time_from_proper_timezone)
          end
          scope.update_all attributes
        end
      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)
    end

    define_method many do
      klass = class_name.constantize
      scope = klass.all

      ids = send(one_ids)
      scope.where!(klass.primary_key => ids)

      fragments = []
      fragments += ["#{klass.primary_key} NOT IN (#{ids.map(&:to_s).join(",")})"] if ids.any?
      fragments += ids.reverse.map { |id| "#{klass.primary_key}=#{id}" }
      order_by_ids = fragments.join(", ")
      scope.order!(Arel.sql(order_by_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



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/active_record/json_associations.rb', line 100

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
  klass = class_name.constantize

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

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

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

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

    define_method many_equals do |collection|
      collection.each do |record|
        new_id_array = Array(record.send(foreign_key)) | [id]
        raise "FIXME: Cannot assign during creation, because no id has yet been reified." if new_id_array.any?(&:nil?)
        record.update foreign_key => new_id_array
      end
    end

    define_method many_eh do
      send(many).any?
    end

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

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

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