Class: ActiveTouch::TouchJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/active_touch/touch_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_touchObject

Returns the value of attribute after_touch.



4
5
6
# File 'lib/active_touch/touch_job.rb', line 4

def after_touch
  @after_touch
end

#associationObject

Returns the value of attribute association.



4
5
6
# File 'lib/active_touch/touch_job.rb', line 4

def association
  @association
end

#is_destroyObject

Returns the value of attribute is_destroy.



4
5
6
# File 'lib/active_touch/touch_job.rb', line 4

def is_destroy
  @is_destroy
end

#is_touchedObject

Returns the value of attribute is_touched.



4
5
6
# File 'lib/active_touch/touch_job.rb', line 4

def is_touched
  @is_touched
end

#recordObject

Returns the value of attribute record.



4
5
6
# File 'lib/active_touch/touch_job.rb', line 4

def record
  @record
end

#touch_updatesObject

Returns the value of attribute touch_updates.



4
5
6
# File 'lib/active_touch/touch_job.rb', line 4

def touch_updates
  @touch_updates
end

Instance Method Details

#associatedObject



32
33
34
35
36
37
38
39
40
# File 'lib/active_touch/touch_job.rb', line 32

def associated
  @associated ||= begin
    if association == 'self'
      is_destroy || record.destroyed? ? nil : record
    else
      record.send(association)
    end
  end
end

#perform(record, association, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/active_touch/touch_job.rb', line 6

def perform(record, association, options = {})
  @record = record
  @association = association
  @after_touch = options[:after_touch]
  @is_destroy = options[:is_destroy]
  @is_touched = options[:is_touched]
  @touch_updates = (options[:touch_updates] || {})

  run
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_touch/touch_job.rb', line 17

def run
  if !ActiveTouch.configuration.timestamp_attribute.nil?
    touch_updates[ActiveTouch.configuration.timestamp_attribute] = record.updated_at
  end

  if associated.is_a? ActiveRecord::Base
    associated.update_columns(touch_updates) if !touch_updates.empty? && !is_touched
    associated.send(after_touch) unless after_touch.blank?

  elsif !associated.nil?
    associated.update_all(touch_updates) if !touch_updates.empty? && !is_touched
    associated.each { |associate| associate.send(after_touch) } unless after_touch.blank?
  end
end