active_touch 1.1.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/README.md +25 -1
- data/lib/active_touch.rb +1 -0
- data/lib/active_touch/configuration.rb +25 -0
- data/lib/active_touch/define_touch.rb +13 -5
- data/lib/active_touch/touch_job.rb +1 -1
- data/lib/active_touch/version.rb +1 -1
- data/lib/generators/active_touch/initializer_generator.rb +6 -0
- data/lib/generators/active_touch/install/install_generator.rb +12 -0
- data/lib/generators/active_touch/install/templates/active_touch.rb +13 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWFlZTI2MzdlMmFiOTQ1MTI0ZDg0YmFiMWViYjNhZGE5MWQ0YmNiNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2RlYTU2MGNmODZjMTBlZTFmYTIwMGI5MTU0NDg1YzI1OWQyMmFjMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
M2JjNTcwMTQ2ZDI3ZWM3ZWUxZjEwZDhlZjJhM2I4YWQ4N2FiYWYzNzM1Njhi
|
10
|
+
MjVjMWFlODNjYTQ3NzcwZjAxNGUxMmJlMzM1ZjYzNGI4NTdlNWNkMjhhZTFl
|
11
|
+
MjYzNDNkOTg0YzQwMWIyZjhlNjZmZDE2ZmI1N2QyN2RmNzNmYTU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjM5Y2UwYzUxZWIwNmMyMmJiOWJiM2MzYTZiODczMzFhNzlkMjU1NjgyNDI0
|
14
|
+
Y2JlNmI4YmUxY2YxOTcwMDBjNDMwZGZhMGI0MTdmNzk0Mjg0NmU3ZjFjNjRi
|
15
|
+
N2YzMzdlZDJmMTg0NTA2MmU1YTNhNzZiZjYzMWNhNWRkYzcyYzU=
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,13 @@ A more robust touch for ActiveRecord associations.
|
|
9
9
|
Add the gem to your Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'activetouch', '~> 1.
|
12
|
+
gem 'activetouch', '~> 1.4.0'
|
13
|
+
```
|
14
|
+
|
15
|
+
Then run the installer:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
rails g active_touch:install
|
13
19
|
```
|
14
20
|
|
15
21
|
## Usage
|
@@ -26,6 +32,9 @@ end
|
|
26
32
|
|
27
33
|
NOTE: It doesn't matter what type of association is given. The association can even reference an instance method that returns an `ActiveRecord` collection or record.
|
28
34
|
|
35
|
+
|
36
|
+
### Attribute specific touches
|
37
|
+
|
29
38
|
To only call the touch when specific attributes are changed, supply an array of attributes with `:watch`. The following example will only touch `:relations` when `:name` or `:code` changes.
|
30
39
|
|
31
40
|
```ruby
|
@@ -36,6 +45,9 @@ class Model < ActiveRecord::Base
|
|
36
45
|
end
|
37
46
|
```
|
38
47
|
|
48
|
+
|
49
|
+
### After Touch Callback
|
50
|
+
|
39
51
|
To call a method on the touched records, use `:after_touch`. The following example will call `:do_something` on the associated records after a tocuh.
|
40
52
|
|
41
53
|
```ruby
|
@@ -48,6 +60,9 @@ end
|
|
48
60
|
|
49
61
|
NOTE: The `after_touch` method must be an instance method defined on the associated Class.
|
50
62
|
|
63
|
+
|
64
|
+
### Asynchronous touch
|
65
|
+
|
51
66
|
The touch can also be queued and run in the background using `ActiveJob` by setting the `:async` flag. The following example will run a touch in the background.
|
52
67
|
|
53
68
|
```ruby
|
@@ -59,3 +74,12 @@ end
|
|
59
74
|
```
|
60
75
|
|
61
76
|
NOTE: The default is `async: false`
|
77
|
+
|
78
|
+
|
79
|
+
## Options
|
80
|
+
|
81
|
+
There are a few options that you can change by updating `config/initializers/active_touch.rb`.
|
82
|
+
|
83
|
+
- `async`: Define a default for all touches to run asynchronously by setting this to true.
|
84
|
+
- `ignored_attributes`: When no `watch` argument is supplied, all attribute changes can trigger a touch. Define a default list of ignored attributes here. Default is `:updated_at`.
|
85
|
+
- `queue`: Specify which queue to put asynchronous jobs in.
|
data/lib/active_touch.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActiveTouch
|
2
|
+
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield(configuration)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Configuration
|
16
|
+
attr_accessor :async, :ignored_attributes, :queue
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@async = false
|
20
|
+
@ignored_attributes = [:updated_at]
|
21
|
+
@queue = 'default'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -23,11 +23,17 @@ module ActiveTouch
|
|
23
23
|
|
24
24
|
@klass.send :define_method, @touch_method do |*args|
|
25
25
|
changed_attributes = self.previous_changes.keys.map(&:to_sym)
|
26
|
+
watched_changes = (options[:watch] & changed_attributes)
|
26
27
|
|
27
|
-
|
28
|
+
# watched values changed and conditional procs evaluate to true
|
29
|
+
if watched_changes.any? && options[:if].call(self) && !options[:unless].call(self)
|
30
|
+
Rails.logger.debug "Touch: #{self.class}(#{self.id}) => #{association} due to changes in #{watched_changes}"
|
28
31
|
|
29
32
|
if options[:async]
|
30
|
-
TouchJob
|
33
|
+
TouchJob
|
34
|
+
.set(queue: ActiveTouch.configuration.queue)
|
35
|
+
.perform_later(self, association.to_s, options[:after_touch].to_s)
|
36
|
+
|
31
37
|
else
|
32
38
|
TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s)
|
33
39
|
end
|
@@ -43,9 +49,11 @@ module ActiveTouch
|
|
43
49
|
|
44
50
|
def default_options
|
45
51
|
{
|
46
|
-
async:
|
47
|
-
watch: @klass.column_names.map(&:to_sym),
|
48
|
-
after_touch: nil
|
52
|
+
async: ActiveTouch.configuration.async,
|
53
|
+
watch: @klass.column_names.map(&:to_sym) - ActiveTouch.configuration.ignored_attributes,
|
54
|
+
after_touch: nil,
|
55
|
+
if: Proc.new { true },
|
56
|
+
unless: Proc.new { false }
|
49
57
|
}
|
50
58
|
end
|
51
59
|
|
@@ -12,7 +12,7 @@ module ActiveTouch
|
|
12
12
|
associated.update_columns(updated_at: record.updated_at)
|
13
13
|
associated.send(after_touch) unless after_touch.blank?
|
14
14
|
|
15
|
-
|
15
|
+
elsif !associated.nil?
|
16
16
|
associated.update_all(updated_at: record.updated_at)
|
17
17
|
associated.each { |associate| associate.send(after_touch) } unless after_touch.blank?
|
18
18
|
end
|
data/lib/active_touch/version.rb
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
module ActiveTouch
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
def copy_files
|
7
|
+
template 'active_touch.rb', 'config/initializers/active_touch.rb'
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveTouch.configure do |config|
|
2
|
+
# All touches to run synchronously or asynchronously, unless specified
|
3
|
+
# Default is false
|
4
|
+
# config.async = false
|
5
|
+
|
6
|
+
# When :watch is not specified, ignore the following attributes.
|
7
|
+
# Default is [:updated_at]
|
8
|
+
# config.ignored_attributes = [:updated_at]
|
9
|
+
|
10
|
+
# Job queue for asynchronous jobs.
|
11
|
+
# Default is 'default'
|
12
|
+
# config.queue = 'default'
|
13
|
+
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: 1.
|
4
|
+
version: 1.4.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: 2015-12-
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,9 +71,13 @@ files:
|
|
71
71
|
- bin/console
|
72
72
|
- bin/setup
|
73
73
|
- lib/active_touch.rb
|
74
|
+
- lib/active_touch/configuration.rb
|
74
75
|
- lib/active_touch/define_touch.rb
|
75
76
|
- lib/active_touch/touch_job.rb
|
76
77
|
- lib/active_touch/version.rb
|
78
|
+
- lib/generators/active_touch/initializer_generator.rb
|
79
|
+
- lib/generators/active_touch/install/install_generator.rb
|
80
|
+
- lib/generators/active_touch/install/templates/active_touch.rb
|
77
81
|
homepage: https://github.com/kpheasey/active_touch
|
78
82
|
licenses:
|
79
83
|
- MIT
|