active_touch 3.0.3 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2UzMGI0YmVlYTYxZjQxMzI4ZmIwZmEzMWViYTAwZjc1MjI1N2FmNA==
4
+ MTI4NzU0Y2UwMTAwM2ExYmE5ZjYwZmJmY2NkNzI5ZGEzNmExYjU3Nw==
5
5
  data.tar.gz: !binary |-
6
- MjBkMzE1ZGIwMTJkYmRhNDZlNzkzOTAxZTMxMTVkOTdlODA5ZmJhMA==
6
+ MzdmMzg2MGY1ZWNjZTViYTg2OWE2ZGZlNTU4OGRiNWE0Yzc2NTA0OQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MmFhYjkyYTAwYjIxYjNmODNkZjNiNGRhZjJiYzQ2N2EyMzVjNDAwNjUyNzFi
10
- NzA4OTRmODliYjQzMWI5NWZiOGYxN2QxOGZlYjdmNWJmODdmM2U1M2ZjYjdh
11
- OTg2MzA0YmE4NTA0ZmEwMmVlNWQ3MTBhN2FkYzJiYmQ5ZThlYzA=
9
+ ZWNkMDQ2YTFlYjE5MDdjODNhNWZjYWYyNDM2MTQ5Njg2MjIxMDQwMGU5NmNj
10
+ ZjMyYTJjN2YyMTI4MjkzNzI2NTRlYzUzMmU0MWNkMzI5NmY0NmQyZjI0Mzlm
11
+ YzY1NmJmYzQzYWI3MTE4NDRiZjk0MjkwYTA0N2NkMGRlNThkZjU=
12
12
  data.tar.gz: !binary |-
13
- MTI5ZGJiZWJiZjUzYmNjOTEwNDEzMzJhMmIzMzlkYzNjOWFjNGJhYjFkZTcw
14
- MWM4MjIyMzhjZmI0NWZkYTkxNGNlNWYwOTJiMWY3N2VjMTA1Y2FmZDEwMmE4
15
- NDVjNWIyNDRiODQ1YzU1OTFlODBiNmYzNzQzODllZTI5NTYxNTY=
13
+ OGNkMTVlM2U3ZDViYmIyZjEzMmMzYjljMzUxOTdiMTRlZDI2YWUwMzcyNjY3
14
+ YzgzODA2YzczMDA1MzkzMmZlNzFmYTdjNmNkYzIyOGQ0NDg3MzBjMjRhZWMz
15
+ ZjVjNTJiYTFiNWQzZDcwZWRlM2FkMGM2ZTA3ODdlZTdiMzhjN2Q=
data/README.md CHANGED
@@ -9,7 +9,7 @@ A more robust touch for ActiveRecord associations.
9
9
  Add the gem to your Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'activetouch', '~> 1.4.0'
12
+ gem 'activetouch', '~> 3.0.3'
13
13
  ```
14
14
 
15
15
  Then run the installer:
data/lib/active_touch.rb CHANGED
@@ -2,6 +2,7 @@ require 'active_touch/version'
2
2
  require 'active_touch/configuration'
3
3
  require 'active_touch/define_touch'
4
4
  require 'active_touch/network'
5
+ require 'active_touch/touch'
5
6
  require 'active_touch/touch_job'
6
7
 
7
8
  require 'active_support/concern'
@@ -11,20 +11,25 @@ module ActiveTouch
11
11
  @klass = klass
12
12
  @association = association
13
13
  @options = default_options.merge(options)
14
- @touch_method = "touch_#{SecureRandom.uuid}"
14
+ @update_touch_method = "touch_#{SecureRandom.uuid}"
15
+ @destroy_touch_method = "touch_#{SecureRandom.uuid}"
15
16
  end
16
17
 
17
18
  def define
18
- define_touch_method
19
- add_active_record_callback
19
+ define_update_touch_method
20
+ add_active_record_callback(@update_touch_method)
21
+
22
+ define_destroy_touch_method
23
+ add_active_record_callback(@destroy_touch_method)
24
+
20
25
  add_to_network
21
26
  end
22
27
 
23
- def define_touch_method
28
+ def define_update_touch_method
24
29
  association = @association
25
30
  options = @options
26
31
 
27
- @klass.send :define_method, @touch_method do |*args|
32
+ @klass.send :define_method, @update_touch_method do |*args|
28
33
  changed_attributes = self.previous_changes.keys.map(&:to_sym)
29
34
  watched_changes = (options[:watch] & changed_attributes)
30
35
 
@@ -35,19 +40,28 @@ module ActiveTouch
35
40
  if options[:async]
36
41
  TouchJob
37
42
  .set(queue: ActiveTouch.configuration.queue)
38
- .perform_later(self, association.to_s, options[:after_touch].to_s, true)
43
+ .perform_later(self, association.to_s, options[:after_touch].to_s)
39
44
 
40
45
  else
41
- TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s, false)
46
+ TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s)
42
47
  end
43
48
 
44
49
  end
45
50
  end
46
51
  end
47
52
 
48
- def add_active_record_callback
49
- touch_method = @touch_method
50
- @klass.send(:after_commit) { send(touch_method) }
53
+ def define_destroy_touch_method
54
+ association = @association
55
+ options = @options
56
+
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)
60
+ end
61
+ end
62
+
63
+ def add_active_record_callback(method)
64
+ @klass.send(:after_commit) { send(method) }
51
65
  end
52
66
 
53
67
  def add_to_network
@@ -0,0 +1,41 @@
1
+ module ActiveTouch
2
+ class Touch
3
+
4
+ attr_accessor :record, :association, :after_touch, :is_destroy
5
+
6
+ def initialize(record, association, after_touch, is_destroy = false)
7
+ @record = record
8
+ @association = association
9
+ @after_touch = after_touch
10
+ @is_destroy = is_destroy
11
+ end
12
+
13
+ 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
18
+
19
+ associated.send(after_touch) unless after_touch.blank?
20
+
21
+ 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
+
26
+ associated.each { |associate| associate.send(after_touch) } unless after_touch.blank?
27
+ end
28
+ end
29
+
30
+ def associated
31
+ @associated ||= begin
32
+ if association == 'self'
33
+ is_destroy ? nil : record
34
+ else
35
+ record.send(association)
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -1,23 +1,8 @@
1
1
  module ActiveTouch
2
2
  class TouchJob < ActiveJob::Base
3
3
 
4
- def perform(record, association, after_touch, is_async = ActiveTouch.configuration.async)
5
- associated = association == 'self' ? record : record.send(association)
6
-
7
- if associated.is_a? ActiveRecord::Base
8
- unless ActiveTouch.configuration.timestamp_attribute.nil?
9
- associated.update_columns(ActiveTouch.configuration.timestamp_attribute => record.updated_at)
10
- end
11
-
12
- associated.send(after_touch) unless after_touch.blank?
13
-
14
- elsif !associated.nil? && !associated.empty?
15
- unless ActiveTouch.configuration.timestamp_attribute.nil?
16
- associated.update_all(ActiveTouch.configuration.timestamp_attribute => record.updated_at)
17
- end
18
-
19
- associated.each { |associate| associate.send(after_touch) } unless after_touch.blank?
20
- end
4
+ def perform(record, association, after_touch, is_destroy = false)
5
+ Touch.new(record, association, after_touch, is_destroy).run
21
6
  end
22
7
 
23
8
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveTouch
2
- VERSION = '3.0.3'
2
+ VERSION = '4.0.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: 3.0.3
4
+ version: 4.0.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-02-09 00:00:00.000000000 Z
11
+ date: 2016-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,6 +74,7 @@ files:
74
74
  - lib/active_touch/configuration.rb
75
75
  - lib/active_touch/define_touch.rb
76
76
  - lib/active_touch/network.rb
77
+ - lib/active_touch/touch.rb
77
78
  - lib/active_touch/touch_job.rb
78
79
  - lib/active_touch/version.rb
79
80
  - lib/generators/active_touch/initializer_generator.rb