Module: Immunio::SpawnHooks

Extended by:
ActiveSupport::Concern
Defined in:
lib/immunio/plugins/active_record_relation.rb

Instance Method Summary collapse

Instance Method Details

#merge_with_immunio(other, *args) ⇒ Object

Sometimes ActiveRecord creates a new relation with a new condition and merges it into an existing relation. I’m not sure why, and I’m not going to ask. Just copy the context data from the other relation into this one.



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
# File 'lib/immunio/plugins/active_record_relation.rb', line 44

def merge_with_immunio(other, *args)
  return merge_without_immunio(other, *args) unless other && !other.is_a?(Array)

  # Rails 4 added the ability to call merge with a proc, like:
  #
  # relation.merge(-> { where(:foo) })
  #
  # We don't need to do anything here. If the proc calls relation methods,
  # they will be called on the right relation and everything will be good.
  if !other.is_a?(ActiveRecord::Relation) && other.respond_to?(:to_proc)
    return merge_without_immunio(other, *args)
  end

  # Rails 4 added the ability to merge in a hash of conditions, like:
  #
  # Developer.merge(where: 'projects IS NOT NULL')
  #
  # Use the internal HashMerger to build a real relation from the hash
  # before merging values into the spawned relation.
  if other.is_a?(Hash)
    # This shouldn't happen, but let's be safe.
    unless defined? ActiveRecord::Relation::HashMerger
      return merge_without_immunio(other, *args)
    end

    other = ActiveRecord::Relation::HashMerger.new(self, other).other
  end

  spawned = merge_without_immunio(other, *args)
  Request.time "plugin", "Immunio::RelationTracking" do
    QueryTracker.instance.merge_relations spawned, other
  end
  spawned
end