-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move touch logic into Touch class. Add after_destroy method for touch…
…ing on destroy.
- Loading branch information
Kevin Pheasey
committed
Jun 5, 2016
1 parent
445431b
commit 2911ab2
Showing
5 changed files
with
69 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |