active_touch 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjllZTYxOGYxODMwMmZjMGRjZmRlODMxOTc4MDkyY2MyZDYzOTJkOA==
5
+ data.tar.gz: !binary |-
6
+ YzJlM2ExNjBiMTBhNGI4MWIwMjJlNWE5NDdhNDMwNTU5OGRkYjFhYQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmNkNWM1MGFlZjMxOTRiMDgwZTc3ZTQ4MGE4ZDFmMTNjYjQzNTFhYTI4MjNj
10
+ ZTc0MGU0M2RmNTE3MzJmOTkwMjAxZDk5OGQ1MTBiZGJmZWVkMWQ5NmRmNDU1
11
+ OGI4NDdkY2FjOTE1NGM0MGMzOTcwYzUxYmJmNTgwZjBhZmFjN2M=
12
+ data.tar.gz: !binary |-
13
+ ZmYxOTZkZmI2NGM4ZTY2MGZkM2VhODllNGE1ZGE1OTczYjk3YTg1NmRiNDc3
14
+ ZDZjYmM3NDNkNzFiYzQ1M2I5YThiZjQ5NGE1ZDdjYThhYzEyMmI3YWQ4ZWI5
15
+ ZTBmODcwZDI2ZDkzYzZhZDY2MmVlYmI5YjgwM2M2MGM4NDVkNmI=
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
36
+
37
+ .idea
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_touch.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_touch (1.0.0)
5
+ rails (~> 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (4.2.5)
11
+ actionpack (= 4.2.5)
12
+ actionview (= 4.2.5)
13
+ activejob (= 4.2.5)
14
+ mail (~> 2.5, >= 2.5.4)
15
+ rails-dom-testing (~> 1.0, >= 1.0.5)
16
+ actionpack (4.2.5)
17
+ actionview (= 4.2.5)
18
+ activesupport (= 4.2.5)
19
+ rack (~> 1.6)
20
+ rack-test (~> 0.6.2)
21
+ rails-dom-testing (~> 1.0, >= 1.0.5)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
+ actionview (4.2.5)
24
+ activesupport (= 4.2.5)
25
+ builder (~> 3.1)
26
+ erubis (~> 2.7.0)
27
+ rails-dom-testing (~> 1.0, >= 1.0.5)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
+ activejob (4.2.5)
30
+ activesupport (= 4.2.5)
31
+ globalid (>= 0.3.0)
32
+ activemodel (4.2.5)
33
+ activesupport (= 4.2.5)
34
+ builder (~> 3.1)
35
+ activerecord (4.2.5)
36
+ activemodel (= 4.2.5)
37
+ activesupport (= 4.2.5)
38
+ arel (~> 6.0)
39
+ activesupport (4.2.5)
40
+ i18n (~> 0.7)
41
+ json (~> 1.7, >= 1.7.7)
42
+ minitest (~> 5.1)
43
+ thread_safe (~> 0.3, >= 0.3.4)
44
+ tzinfo (~> 1.1)
45
+ arel (6.0.3)
46
+ builder (3.2.2)
47
+ concurrent-ruby (1.0.0)
48
+ erubis (2.7.0)
49
+ globalid (0.3.6)
50
+ activesupport (>= 4.1.0)
51
+ i18n (0.7.0)
52
+ json (1.8.3)
53
+ loofah (2.0.3)
54
+ nokogiri (>= 1.5.9)
55
+ mail (2.6.3)
56
+ mime-types (>= 1.16, < 3)
57
+ mime-types (2.99)
58
+ mini_portile2 (2.0.0)
59
+ minitest (5.8.3)
60
+ nokogiri (1.6.7)
61
+ mini_portile2 (~> 2.0.0.rc2)
62
+ rack (1.6.4)
63
+ rack-test (0.6.3)
64
+ rack (>= 1.0)
65
+ rails (4.2.5)
66
+ actionmailer (= 4.2.5)
67
+ actionpack (= 4.2.5)
68
+ actionview (= 4.2.5)
69
+ activejob (= 4.2.5)
70
+ activemodel (= 4.2.5)
71
+ activerecord (= 4.2.5)
72
+ activesupport (= 4.2.5)
73
+ bundler (>= 1.3.0, < 2.0)
74
+ railties (= 4.2.5)
75
+ sprockets-rails
76
+ rails-deprecated_sanitizer (1.0.3)
77
+ activesupport (>= 4.2.0.alpha)
78
+ rails-dom-testing (1.0.7)
79
+ activesupport (>= 4.2.0.beta, < 5.0)
80
+ nokogiri (~> 1.6.0)
81
+ rails-deprecated_sanitizer (>= 1.0.1)
82
+ rails-html-sanitizer (1.0.2)
83
+ loofah (~> 2.0)
84
+ railties (4.2.5)
85
+ actionpack (= 4.2.5)
86
+ activesupport (= 4.2.5)
87
+ rake (>= 0.8.7)
88
+ thor (>= 0.18.1, < 2.0)
89
+ rake (10.4.2)
90
+ sprockets (3.5.2)
91
+ concurrent-ruby (~> 1.0)
92
+ rack (> 1, < 3)
93
+ sprockets-rails (2.3.3)
94
+ actionpack (>= 3.0)
95
+ activesupport (>= 3.0)
96
+ sprockets (>= 2.8, < 4.0)
97
+ thor (0.19.1)
98
+ thread_safe (0.3.5)
99
+ tzinfo (1.2.2)
100
+ thread_safe (~> 0.1)
101
+
102
+ PLATFORMS
103
+ ruby
104
+
105
+ DEPENDENCIES
106
+ active_touch!
107
+ bundler (~> 1.10)
108
+ rake (~> 10.0)
109
+
110
+ BUNDLED WITH
111
+ 1.10.6
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kevin Pheasey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # ActiveTouch
2
+ A more robust touch for ActiveRecord associations.
3
+ - Touch specific associations when specific attributes change
4
+ - Call an optional method on those touched records
5
+ - Perform the touch synchronously or asynchronously
6
+
7
+ ## Installation
8
+
9
+ Add the gem to your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'activetouch', '~> 1.0.0'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Basic touch that runs `after_commit`. This will update the association's updated_at.
18
+
19
+ ```ruby
20
+ class Model < ActiveRecord::Base
21
+ has_many :relations
22
+
23
+ touch :relations
24
+ end
25
+ ```
26
+
27
+ 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
+
29
+ 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
+
31
+ ```ruby
32
+ class Model < ActiveRecord::Base
33
+ has_many :relations
34
+
35
+ touch :relations, watch: [:name, :code]
36
+ end
37
+ ```
38
+
39
+ 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
+
41
+ ```ruby
42
+ class Model < ActiveRecord::Base
43
+ has_many :relations
44
+
45
+ touch :relations, after_touch: :do_something
46
+ end
47
+ ```
48
+
49
+ NOTE: The `after_touch` method must be an instance method defined on the associated Class.
50
+
51
+ 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
+
53
+ ```ruby
54
+ class Model < ActiveRecord::Base
55
+ has_many :relations
56
+
57
+ touch :relations, async: true
58
+ end
59
+ ```
60
+
61
+ NOTE: The default is `async: false`
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_touch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'active_touch'
8
+ spec.version = ActiveTouch::VERSION
9
+ spec.authors = ['Kevin Pheasey']
10
+ spec.email = ['kevin@kpheasey.com']
11
+ spec.licenses = ['MIT']
12
+
13
+ spec.summary = %q{A more robust touch for ActiveRecord associations.}
14
+ spec.description = %q{Touch specific associations when specific attributes change. Call an optional method on those touched records. Perform the touch synchronously or asynchronously.}
15
+ spec.homepage = 'https://github.com/kpheasey/active_touch'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+
25
+ spec.add_dependency 'rails', '~> 4.2'
26
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_touch"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ require 'active_touch/version'
2
+ require 'active_touch/define_touch'
3
+ require 'active_touch/touch_job'
4
+
5
+ require 'active_support/concern'
6
+ require 'active_job/base'
7
+ require 'active_record/base'
8
+
9
+ module ActiveTouch
10
+ extend ActiveSupport::Concern
11
+
12
+ module ClassMethods
13
+
14
+ def touch(association, options = {})
15
+ DefineTouch.on(self, association, options)
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ class ActiveRecord::Base
22
+ include ActiveTouch
23
+ end
@@ -0,0 +1,53 @@
1
+ module ActiveTouch
2
+ class DefineTouch
3
+
4
+ def self.on(klass, association, options)
5
+ new(klass, association, options).define
6
+ end
7
+
8
+ def initialize(klass, association, options)
9
+ @klass = klass
10
+ @association = association
11
+ @options = default_options.merge(options)
12
+ @touch_method = "touch_#{SecureRandom.uuid}"
13
+ end
14
+
15
+ def define
16
+ define_touch_method
17
+ add_active_record_callback
18
+ end
19
+
20
+ def define_touch_method
21
+ association = @association
22
+ options = @options
23
+
24
+ @klass.send :define_method, @touch_method do |*args|
25
+ changed_attributes = self.previous_changes.keys.map(&:to_sym)
26
+
27
+ if (options[:watch] & changed_attributes).any?
28
+
29
+ if options[:async]
30
+ TouchJob.perform_later(self, association.to_s, options[:after_touch].to_s)
31
+ else
32
+ TouchJob.perform_now(self, association.to_s, options[:after_touch].to_s)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+
39
+ def add_active_record_callback
40
+ touch_method = @touch_method
41
+ @klass.send(:after_commit) { send(touch_method) }
42
+ end
43
+
44
+ def default_options
45
+ {
46
+ async: false,
47
+ watch: @klass.column_names.map(&:to_sym),
48
+ after_touch: nil
49
+ }
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,22 @@
1
+ module ActiveTouch
2
+ class TouchJob < ActiveJob::Base
3
+
4
+ def perform(record, association, after_touch)
5
+ if association == 'self'
6
+ associated = record
7
+ else
8
+ associated = record.send(association)
9
+ end
10
+
11
+ if associated.is_a? ActiveRecord::Base
12
+ associated.update_columns(updated_at: record.updated_at)
13
+ associated.send(after_touch) unless after_touch.blank?
14
+
15
+ else
16
+ associated.update_all(updated_at: record.updated_at)
17
+ associated.each { |associate| associate.send(after_touch) } unless after_touch.blank?
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveTouch
2
+ VERSION = '1.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_touch
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Pheasey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ description: Touch specific associations when specific attributes change. Call an
56
+ optional method on those touched records. Perform the touch synchronously or asynchronously.
57
+ email:
58
+ - kevin@kpheasey.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .travis.yml
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - active_touch.gemspec
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/active_touch.rb
74
+ - lib/active_touch/define_touch.rb
75
+ - lib/active_touch/touch_job.rb
76
+ - lib/active_touch/version.rb
77
+ homepage: https://github.com/kpheasey/active_touch
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.8
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A more robust touch for ActiveRecord associations.
101
+ test_files: []