diff --git a/lib/active_touch.rb b/lib/active_touch.rb index 9d4badc..4b573e8 100644 --- a/lib/active_touch.rb +++ b/lib/active_touch.rb @@ -2,6 +2,7 @@ require 'active_touch/configuration' require 'active_touch/define_touch' require 'active_touch/network' +require 'active_touch/touch' require 'active_touch/touch_job' require 'active_support/concern' diff --git a/lib/active_touch/define_touch.rb b/lib/active_touch/define_touch.rb index 60b2e2f..d6cabed 100644 --- a/lib/active_touch/define_touch.rb +++ b/lib/active_touch/define_touch.rb @@ -11,20 +11,25 @@ def initialize(klass, association, options) @klass = klass @association = association @options = default_options.merge(options) - @touch_method = "touch_#{SecureRandom.uuid}" + @update_touch_method = "touch_#{SecureRandom.uuid}" + @destroy_touch_method = "touch_#{SecureRandom.uuid}" end def define - define_touch_method - add_active_record_callback + define_update_touch_method + add_active_record_callback(@update_touch_method) + + define_destroy_touch_method + add_active_record_callback(@destroy_touch_method) + add_to_network end - def define_touch_method + def define_update_touch_method association = @association options = @options - @klass.send :define_method, @touch_method do |*args| + @klass.send :define_method, @update_touch_method do |*args| changed_attributes = self.previous_changes.keys.map(&:to_sym) watched_changes = (options[:watch] & changed_attributes) @@ -35,19 +40,28 @@ def define_touch_method if options[:async] TouchJob .set(queue: ActiveTouch.configuration.queue) - .perform_later(self, association.to_s, options[:after_touch].to_s, true) + .perform_later(self, association.to_s, options[:after_touch].to_s) else - TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s, false) + TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s) end end end end - def add_active_record_callback - touch_method = @touch_method - @klass.send(:after_commit) { send(touch_method) } + def define_destroy_touch_method + association = @association + options = @options + + @klass.send :define_method, @destroy_touch_method do |*args| + Rails.logger.debug "Touch: #{self.class}(#{self.id}) => #{association} due to destroy" + TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s, true) + end + end + + def add_active_record_callback(method) + @klass.send(:after_commit) { send(method) } end def add_to_network diff --git a/lib/active_touch/touch.rb b/lib/active_touch/touch.rb new file mode 100644 index 0000000..556269f --- /dev/null +++ b/lib/active_touch/touch.rb @@ -0,0 +1,41 @@ +module ActiveTouch + class Touch + + attr_accessor :record, :association, :after_touch, :is_destroy + + def initialize(record, association, after_touch, is_destroy = false) + @record = record + @association = association + @after_touch = after_touch + @is_destroy = is_destroy + end + + def run + if associated.is_a? ActiveRecord::Base + unless ActiveTouch.configuration.timestamp_attribute.nil? + associated.update_columns(ActiveTouch.configuration.timestamp_attribute => record.updated_at) + end + + associated.send(after_touch) unless after_touch.blank? + + elsif !associated.nil? && !associated.empty? + unless ActiveTouch.configuration.timestamp_attribute.nil? + associated.update_all(ActiveTouch.configuration.timestamp_attribute => record.updated_at) + end + + associated.each { |associate| associate.send(after_touch) } unless after_touch.blank? + end + end + + def associated + @associated ||= begin + if association == 'self' + is_destroy ? 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 ecfa23a..e1a89c1 100644 --- a/lib/active_touch/touch_job.rb +++ b/lib/active_touch/touch_job.rb @@ -1,23 +1,8 @@ module ActiveTouch class TouchJob < ActiveJob::Base - def perform(record, association, after_touch, is_async = ActiveTouch.configuration.async) - associated = association == 'self' ? record : record.send(association) - - if associated.is_a? ActiveRecord::Base - unless ActiveTouch.configuration.timestamp_attribute.nil? - associated.update_columns(ActiveTouch.configuration.timestamp_attribute => record.updated_at) - end - - associated.send(after_touch) unless after_touch.blank? - - elsif !associated.nil? && !associated.empty? - unless ActiveTouch.configuration.timestamp_attribute.nil? - associated.update_all(ActiveTouch.configuration.timestamp_attribute => record.updated_at) - end - - associated.each { |associate| associate.send(after_touch) } unless after_touch.blank? - end + def perform(record, association, after_touch, is_destroy = false) + Touch.new(record, association, after_touch, is_destroy).run end end diff --git a/lib/active_touch/version.rb b/lib/active_touch/version.rb index 3c30af7..1db114e 100644 --- a/lib/active_touch/version.rb +++ b/lib/active_touch/version.rb @@ -1,3 +1,3 @@ module ActiveTouch - VERSION = '3.0.3' + VERSION = '4.0.0' end