touch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 641e3fba3666224d044667ea2b78f2dc2df1d3e8
4
+ data.tar.gz: 05e12cee1964ff61671ab8fa1fe1c8a0c144148f
5
+ SHA512:
6
+ metadata.gz: 8623b9707ec162816d5cde98f87423ebce8aedc01ec5e3c931036fd5aef83d12df3c1779881eaad13481625b3b5d3b50f4036cc39bc5e3218516f0811ea68297
7
+ data.tar.gz: d8e4e4067703b56292a0bf997661b5ee4be534eecffc0e729c04de82d88a7248a9fd4be4c05da24bf0fa4820bd175eaeb0f20e11f6f8e91903114ed906a50709
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-touch.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ touch (0.0.1)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.0.0)
11
+ activesupport (= 4.0.0)
12
+ builder (~> 3.1.0)
13
+ activerecord (4.0.0)
14
+ activemodel (= 4.0.0)
15
+ activerecord-deprecated_finders (~> 1.0.2)
16
+ activesupport (= 4.0.0)
17
+ arel (~> 4.0.0)
18
+ activerecord-deprecated_finders (1.0.3)
19
+ activesupport (4.0.0)
20
+ i18n (~> 0.6, >= 0.6.4)
21
+ minitest (~> 4.2)
22
+ multi_json (~> 1.3)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 0.3.37)
25
+ arel (4.0.0)
26
+ atomic (1.1.10)
27
+ awesome_print (1.1.0)
28
+ builder (3.1.4)
29
+ coderay (1.0.9)
30
+ diff-lcs (1.2.4)
31
+ i18n (0.6.4)
32
+ method_source (0.8.1)
33
+ minitest (4.7.5)
34
+ multi_json (1.7.7)
35
+ pry (0.9.12.2)
36
+ coderay (~> 1.0.5)
37
+ method_source (~> 0.8)
38
+ slop (~> 3.4)
39
+ pry-meta (0.0.5)
40
+ awesome_print
41
+ pry
42
+ pry-nav
43
+ pry-remote
44
+ pry-nav (0.2.3)
45
+ pry (~> 0.9.10)
46
+ pry-remote (0.1.7)
47
+ pry (~> 0.9)
48
+ slop (~> 3.0)
49
+ rake (10.1.0)
50
+ rspec (2.14.1)
51
+ rspec-core (~> 2.14.0)
52
+ rspec-expectations (~> 2.14.0)
53
+ rspec-mocks (~> 2.14.0)
54
+ rspec-core (2.14.4)
55
+ rspec-expectations (2.14.0)
56
+ diff-lcs (>= 1.1.3, < 2.0)
57
+ rspec-mocks (2.14.1)
58
+ slop (3.4.5)
59
+ sqlite3 (1.3.7)
60
+ thread_safe (0.1.0)
61
+ atomic
62
+ tzinfo (0.3.37)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ bundler
69
+ pry-meta
70
+ rake
71
+ rspec
72
+ sqlite3
73
+ touch!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nando Vieira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Touch
2
+
3
+ Touch a parent's collection on ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "touch"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install touch
18
+
19
+ ## Usage
20
+
21
+ Imagine you have to models:
22
+
23
+ ```ruby
24
+ class User < ActiveRecord::Base
25
+ has_many :things
26
+ end
27
+
28
+ class Thing < ActiveRecord::Base
29
+ belongs_to :user
30
+ touch :user
31
+ end
32
+ ```
33
+
34
+ This new `ActiveRecord::Base.touch` method updates the `User#things_updated_at` attribute every time a new `Thing` is created, updated or removed.
35
+
36
+ It also updates the parent's `updated_at` field, just like `belongs_to :user, touch: true` configuration.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/touch.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "active_record"
2
+ require "touch/version"
3
+
4
+ module Touch
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def touch(association_name, options = {})
9
+ options = options.dup
10
+ options.reverse_merge!(on: %w[create update destroy])
11
+ options[:on].map!(&:to_s)
12
+
13
+ options[:on].each do |event|
14
+ add_collection_touch_for(association_name, event)
15
+ end
16
+ end
17
+
18
+ private
19
+ def add_collection_touch_for(association_name, event)
20
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
21
+ def collection_touch_event_for_#{association_name}_on_#{event}
22
+ parent = public_send(:#{association_name})
23
+ field_name = "\#{self.class.model_name.cache_key}_updated_at"
24
+
25
+ parent.try(:touch, field_name)
26
+ end
27
+
28
+ after_#{event} :collection_touch_event_for_#{association_name}_on_#{event}
29
+ RUBY
30
+ end
31
+ end
32
+ end
33
+
34
+ ActiveRecord::Base.send :include, Touch
@@ -0,0 +1,3 @@
1
+ module Touch
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+ Bundler.require
3
+
4
+ require "touch"
5
+
6
+ ActiveRecord::Base.establish_connection(
7
+ adapter: "sqlite3",
8
+ database: ":memory:"
9
+ )
10
+
11
+ require "support/models"
12
+
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+ config.order = "random"
18
+ end
@@ -0,0 +1,22 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users do |t|
3
+ t.datetime :tasks_updated_at
4
+ t.timestamps
5
+ end
6
+
7
+ create_table :tasks do |t|
8
+ t.string :title
9
+ t.belongs_to :user
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ class User < ActiveRecord::Base
15
+ has_many :tasks
16
+ has_many :things
17
+ end
18
+
19
+ class Task < ActiveRecord::Base
20
+ belongs_to :user
21
+ touch :user
22
+ end
@@ -0,0 +1,60 @@
1
+ require "spec_helper"
2
+
3
+ describe Touch do
4
+ let(:user) { User.create! }
5
+ let(:task) { user.tasks.create! }
6
+
7
+ context "on create" do
8
+ it "updates the parent's collection field" do
9
+ Task.touch(:user)
10
+
11
+ expect {
12
+ user.tasks.create!
13
+ }.to change { user.reload.tasks_updated_at }
14
+ end
15
+
16
+ it "updates the parent record" do
17
+ Task.touch(:user)
18
+
19
+ expect {
20
+ user.tasks.create!
21
+ }.to change { user.reload.updated_at }
22
+ end
23
+ end
24
+
25
+ context "on update" do
26
+ it "updates the parent's collection field" do
27
+ Task.touch(:user)
28
+
29
+ expect {
30
+ task.update_attributes title: "Some title"
31
+ }.to change { user.reload.tasks_updated_at }
32
+ end
33
+
34
+ it "updates the parent record" do
35
+ Task.touch(:user)
36
+
37
+ expect {
38
+ task.update_attributes title: "Some title"
39
+ }.to change { user.reload.updated_at }
40
+ end
41
+ end
42
+
43
+ context "on destroy" do
44
+ it "updates the parent's collection field" do
45
+ Task.touch(:user)
46
+
47
+ expect {
48
+ task.destroy!
49
+ }.to change { user.reload.tasks_updated_at }
50
+ end
51
+
52
+ it "updates the parent record" do
53
+ Task.touch(:user)
54
+
55
+ expect {
56
+ task.destroy!
57
+ }.to change { user.reload.updated_at }
58
+ end
59
+ end
60
+ end
data/touch.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ require "./lib/touch/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "touch"
5
+ spec.version = Touch::VERSION
6
+ spec.authors = ["Nando Vieira"]
7
+ spec.email = ["fnando.vieira@gmail.com"]
8
+ spec.description = "Touch a parent's collection on ActiveRecord."
9
+ spec.summary = spec.description
10
+ spec.homepage = "http://github.com/fnando/touch"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "activerecord"
19
+ spec.add_development_dependency "bundler"
20
+ spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "pry-meta"
23
+ spec.add_development_dependency "sqlite3"
24
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: touch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-meta
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Touch a parent's collection on ActiveRecord.
98
+ email:
99
+ - fnando.vieira@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/touch.rb
112
+ - lib/touch/version.rb
113
+ - spec/spec_helper.rb
114
+ - spec/support/models.rb
115
+ - spec/touch_spec.rb
116
+ - touch.gemspec
117
+ homepage: http://github.com/fnando/touch
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.0.3
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Touch a parent's collection on ActiveRecord.
141
+ test_files:
142
+ - spec/spec_helper.rb
143
+ - spec/support/models.rb
144
+ - spec/touch_spec.rb