Module: ActiverecordTouchy::Associations::ClassMethods

Defined in:
lib/activerecord_touchy/associations.rb

Instance Method Summary collapse

Instance Method Details

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



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/activerecord_touchy/associations.rb', line 10

def has_many(name, scope = nil, **options, &extension)
  touch = (scope.is_a?(Hash) ? scope : options).delete(:touch)
  result = super

  if touch
    after_commit do
      public_send(name).update_all(updated_at: Time.now.utc)
    end
  end

  result
end

#has_one(name, scope = nil, **options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/activerecord_touchy/associations.rb', line 23

def has_one(name, scope = nil, **options)
  touch = (scope.is_a?(Hash) ? scope : options).delete(:touch)
  result = super

  if touch
    after_commit do
      public_send(name)&.touch
    end
  end

  result
end