Skip to content

Commit

Permalink
merge Touch into TouchJob.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Pheasey committed Oct 15, 2016
1 parent 325bb0d commit 61432e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
41 changes: 0 additions & 41 deletions lib/active_touch/touch.rb

This file was deleted.

35 changes: 34 additions & 1 deletion lib/active_touch/touch_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
module ActiveTouch
class TouchJob < ActiveJob::Base

attr_accessor :record, :association, :after_touch, :is_destroy, :is_touched, :touch_updates

def perform(record, association, options = {})
Touch.new(record, association, options).run
@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

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

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

0 comments on commit 61432e6

Please sign in to comment.