From 61432e60cd7595d19130e63d6dfaaa936bb61e9c Mon Sep 17 00:00:00 2001 From: Kevin Pheasey Date: Fri, 14 Oct 2016 20:41:35 -0400 Subject: [PATCH] merge Touch into TouchJob. --- lib/active_touch/touch.rb | 41 ----------------------------------- lib/active_touch/touch_job.rb | 35 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 42 deletions(-) delete mode 100644 lib/active_touch/touch.rb diff --git a/lib/active_touch/touch.rb b/lib/active_touch/touch.rb deleted file mode 100644 index 8fdee7a..0000000 --- a/lib/active_touch/touch.rb +++ /dev/null @@ -1,41 +0,0 @@ -module ActiveTouch - class Touch - - attr_accessor :record, :association, :after_touch, :is_destroy, :is_touched, :touch_updates - - def initialize(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] || {}) - 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 \ No newline at end of file diff --git a/lib/active_touch/touch_job.rb b/lib/active_touch/touch_job.rb index 077f04b..6c91f6b 100644 --- a/lib/active_touch/touch_job.rb +++ b/lib/active_touch/touch_job.rb @@ -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 \ No newline at end of file