Skip to content

Commit

Permalink
Move touch logic into Touch class. Add after_destroy method for touch…
Browse files Browse the repository at this point in the history
…ing on destroy.
  • Loading branch information
Kevin Pheasey committed Jun 5, 2016
1 parent 445431b commit 2911ab2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/active_touch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
34 changes: 24 additions & 10 deletions lib/active_touch/define_touch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions lib/active_touch/touch.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 2 additions & 17 deletions lib/active_touch/touch_job.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/active_touch/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveTouch
VERSION = '3.0.3'
VERSION = '4.0.0'
end

0 comments on commit 2911ab2

Please sign in to comment.