active_touch 4.0.3 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee45dc1ba07f11a104b79342edd73c93e6294a36
4
- data.tar.gz: d49f37861e80157adcba452556d1da23f459436d
3
+ metadata.gz: a7a498646a1de31a72530822ab3cbef4a9c28525
4
+ data.tar.gz: c2e260ebfc0eb50ebe3c6b10350e2a2362d5bcaa
5
5
  SHA512:
6
- metadata.gz: 3b428e480c2e25b9b76508a9dee60d3390fed7c72780f1bd4a519e45df89e4101597c9757da72f22cd7664f28e29ed831b5b874bc9f1d7cf4bd306f423cd51dd
7
- data.tar.gz: 922d0caa9b5d1ac346700ebc648801bfdca2345e9acf24976534c56fe247c9e22f637a957d803ef9fd748fa59a8dca25082eda9eea51c633396ca7fd80e6e4a0
6
+ metadata.gz: 8cbf64dee01ab998ef025dab0fd43de1806c46bc86a59e54c2045b766ffb523dfa789af9388a8a078f7c1d4b6746d37deba1356d73d80f23b75bb24de8ff5705
7
+ data.tar.gz: 5a5492a98e21fb775b4c39ad02927bc0bd26e785dcafea5f735679f50568de78f484735fba94ea35c613632110ce098db074fd4c6fac5144e9094cd70edd6b47
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_touch (4.0.2)
4
+ active_touch (4.0.3)
5
5
  rails (>= 4.0, < 6.0)
6
6
 
7
7
  GEM
@@ -13,13 +13,15 @@ module ActiveTouch
13
13
  end
14
14
 
15
15
  class Configuration
16
- attr_accessor :async, :ignored_attributes, :queue, :timestamp_attribute
16
+ attr_accessor :async, :ignored_attributes, :queue, :timestamp_attribute, :touch_in_transaction, :touch_updates
17
17
 
18
18
  def initialize
19
19
  @async = false
20
20
  @ignored_attributes = [:updated_at]
21
21
  @queue = 'default'
22
22
  @timestamp_attribute = :updated_at
23
+ @touch_in_transaction = true
24
+ @touch_updates = {}
23
25
  end
24
26
  end
25
27
 
@@ -11,52 +11,90 @@ module ActiveTouch
11
11
  @klass = klass
12
12
  @association = association
13
13
  @options = default_options.merge(options)
14
- @update_touch_method = "touch_#{SecureRandom.uuid}"
15
- @destroy_touch_method = "touch_#{SecureRandom.uuid}"
14
+ @before_commit_method = "touch_#{SecureRandom.uuid}"
15
+ @after_commit_method = "touch_#{SecureRandom.uuid}"
16
+ @before_destroy_method = "touch_#{SecureRandom.uuid}"
16
17
  end
17
18
 
18
19
  def define
19
- define_update_touch_method
20
- add_active_record_callback(:after_commit, @update_touch_method)
20
+ if @options[:touch_in_transaction]
21
+ define_before_commit_method
22
+ add_active_record_callback(:before_commit, @before_commit_method)
23
+ end
24
+
25
+ if !@options[:touch_in_transaction] || !@options[:after_touch].nil?
26
+ define_after_commit_method
27
+ add_active_record_callback(:after_commit, @after_commit_method)
28
+ end
21
29
 
22
- define_destroy_touch_method
23
- add_active_record_callback(:after_destroy, @destroy_touch_method)
30
+ define_before_destroy
31
+ add_active_record_callback(:after_destroy, @before_destroy_method)
24
32
 
25
33
  add_to_network
26
34
  end
27
35
 
28
- def define_update_touch_method
36
+ def define_before_commit_method
37
+ association = @association
38
+ options = @options
39
+
40
+ @klass.send :define_method, @before_commit_method do |*args|
41
+ changed_attributes = self.previous_changes.keys.map(&:to_sym)
42
+ watched_changes = (options[:watch] & changed_attributes)
43
+
44
+ # watched values changed and conditional procs evaluate to true
45
+ if watched_changes.any? && options[:if].call(self) && !options[:unless].call(self)
46
+ Rails.logger.debug "Touch Before Commit: #{self.class}(#{self.id}) => #{association} due to changes in #{watched_changes}"
47
+ TouchJob.perform_now(self, association.to_s, touch_updates: options[:touch_updates])
48
+ end
49
+ end
50
+ end
51
+
52
+ def define_after_commit_method
29
53
  association = @association
30
54
  options = @options
31
55
 
32
- @klass.send :define_method, @update_touch_method do |*args|
56
+ @klass.send :define_method, @after_commit_method do |*args|
33
57
  changed_attributes = self.previous_changes.keys.map(&:to_sym)
34
58
  watched_changes = (options[:watch] & changed_attributes)
35
59
 
36
60
  # watched values changed and conditional procs evaluate to true
37
61
  if watched_changes.any? && options[:if].call(self) && !options[:unless].call(self)
38
- Rails.logger.debug "Touch: #{self.class}(#{self.id}) => #{association} due to changes in #{watched_changes}"
62
+ Rails.logger.debug "Touch After Commit: #{self.class}(#{self.id}) => #{association} due to changes in #{watched_changes}"
39
63
 
40
64
  if options[:async]
41
65
  TouchJob
42
66
  .set(queue: ActiveTouch.configuration.queue)
43
- .perform_later(self, association.to_s, options[:after_touch].to_s)
67
+ .perform_later(self, association.to_s, {
68
+ after_touch: options[:after_touch].to_s,
69
+ is_destroy: false,
70
+ is_touched: options[:touch_in_transaction],
71
+ touch_updates: options[:touch_updates]
72
+ })
44
73
 
45
74
  else
46
- TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s)
75
+ TouchJob.perform_now(self, association.to_s, {
76
+ after_touch: options[:after_touch].to_s,
77
+ is_destroy: false,
78
+ is_touched: options[:touch_in_transaction],
79
+ touch_updates: options[:touch_updates]
80
+ })
47
81
  end
48
82
 
49
83
  end
50
84
  end
51
85
  end
52
86
 
53
- def define_destroy_touch_method
87
+ def define_before_destroy
54
88
  association = @association
55
89
  options = @options
56
90
 
57
- @klass.send :define_method, @destroy_touch_method do |*args|
58
- Rails.logger.debug "Touch: #{self.class}(#{self.id}) => #{association} due to destroy"
59
- TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s, true)
91
+ @klass.send :define_method, @before_destroy_method do |*args|
92
+ Rails.logger.debug "Touch Before Destroy: #{self.class}(#{self.id}) => #{association} due to destroy"
93
+ TouchJob.perform_now(self, association.to_s, {
94
+ after_touch: options[:after_touch].to_s,
95
+ is_destroy: true,
96
+ touch_updates: options[:touch_updates]
97
+ })
60
98
  end
61
99
  end
62
100
 
@@ -71,11 +109,13 @@ module ActiveTouch
71
109
 
72
110
  def default_options
73
111
  {
112
+ touch_in_transaction: ActiveTouch.configuration.touch_in_transaction,
74
113
  async: ActiveTouch.configuration.async,
75
114
  watch: @klass.column_names.map(&:to_sym) - ActiveTouch.configuration.ignored_attributes,
76
115
  after_touch: nil,
77
116
  if: Proc.new { true },
78
- unless: Proc.new { false }
117
+ unless: Proc.new { false },
118
+ touch_updates: ActiveTouch.configuration.touch_updates
79
119
  }
80
120
  end
81
121
 
@@ -1,28 +1,26 @@
1
1
  module ActiveTouch
2
2
  class Touch
3
3
 
4
- attr_accessor :record, :association, :after_touch, :is_destroy
4
+ attr_accessor :record, :association, :after_touch, :is_destroy, :is_touched, :touch_updates
5
5
 
6
- def initialize(record, association, after_touch, is_destroy = false)
6
+ def initialize(record, association, options = {})
7
7
  @record = record
8
8
  @association = association
9
- @after_touch = after_touch
10
- @is_destroy = is_destroy
9
+ @after_touch = options[:after_touch]
10
+ @is_destroy = options[:is_destroy]
11
+ @is_touched = options[:is_touched]
12
+ @touch_updates = (options[:touch_updates] || {})
11
13
  end
12
14
 
13
15
  def run
14
- if associated.is_a? ActiveRecord::Base
15
- unless ActiveTouch.configuration.timestamp_attribute.nil?
16
- associated.update_columns(ActiveTouch.configuration.timestamp_attribute => record.updated_at)
17
- end
16
+ touch_updates[ActiveTouch.configuration.timestamp_attribute] = record.updated_at
18
17
 
18
+ if associated.is_a? ActiveRecord::Base
19
+ associated.update_columns(touch_updates) if !ActiveTouch.configuration.timestamp_attribute.nil? && !is_touched
19
20
  associated.send(after_touch) unless after_touch.blank?
20
21
 
21
22
  elsif !associated.nil? && !associated.empty?
22
- unless ActiveTouch.configuration.timestamp_attribute.nil?
23
- associated.update_all(ActiveTouch.configuration.timestamp_attribute => record.updated_at)
24
- end
25
-
23
+ associated.update_all(touch_updates) if !ActiveTouch.configuration.timestamp_attribute.nil? && !is_touched
26
24
  associated.each { |associate| associate.send(after_touch) } unless after_touch.blank?
27
25
  end
28
26
  end
@@ -1,8 +1,8 @@
1
1
  module ActiveTouch
2
2
  class TouchJob < ActiveJob::Base
3
3
 
4
- def perform(record, association, after_touch, is_destroy = false)
5
- Touch.new(record, association, after_touch, is_destroy).run
4
+ def perform(record, association, options = {})
5
+ Touch.new(record, association, options).run
6
6
  end
7
7
 
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveTouch
2
- VERSION = '4.0.3'
2
+ VERSION = '4.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_touch
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.3
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Pheasey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-22 00:00:00.000000000 Z
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  requirements: []
108
108
  rubyforge_project:
109
- rubygems_version: 2.4.8
109
+ rubygems_version: 2.5.1
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: A more robust touch for ActiveRecord associations.