cached_belongs_to 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -4
- data/cached_belongs_to.gemspec +1 -1
- data/lib/cached_belongs_to.rb +19 -12
- data/lib/cached_belongs_to/version.rb +1 -1
- data/spec/cached_belongs_to_spec.rb +11 -11
- data/spec/integration/callbacks_spec.rb +35 -0
- metadata +6 -32
- data/features/step_definitions/.gitkeep +0 -0
- data/features/step_definitions/active_record_model_steps.rb +0 -39
- data/features/step_definitions/cached_belongs_to_steps.rb +0 -7
- data/features/support/env.rb +0 -6
- data/features/update_cached_attributes_after_save_child_record.feature +0 -20
- data/features/update_cached_attributes_after_save_parent_record.feature +0 -21
data/Rakefile
CHANGED
data/cached_belongs_to.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |gem|
|
|
19
19
|
|
20
20
|
gem.add_development_dependency 'autotest'
|
21
21
|
gem.add_development_dependency 'autotest-growl'
|
22
|
-
gem.add_development_dependency 'cucumber'
|
23
22
|
gem.add_development_dependency 'sqlite3'
|
23
|
+
|
24
24
|
gem.add_development_dependency 'rake'
|
25
25
|
gem.add_development_dependency 'rspec'
|
26
26
|
end
|
data/lib/cached_belongs_to.rb
CHANGED
@@ -16,29 +16,36 @@ module CachedBelongsTo
|
|
16
16
|
#
|
17
17
|
def cached_belongs_to(*args)
|
18
18
|
caches = Array(args[1].delete(:caches))
|
19
|
-
klass = args[0]
|
20
19
|
|
21
|
-
belongs_to(*args)
|
22
|
-
|
23
|
-
|
24
|
-
create_cached_belongs_to_parent_callbacks(caches, klass, children_callback_name)
|
20
|
+
association = belongs_to(*args)
|
21
|
+
create_cached_belongs_to_child_callbacks(caches, association)
|
22
|
+
create_cached_belongs_to_parent_callbacks(caches, association)
|
25
23
|
end
|
26
24
|
|
27
25
|
private
|
28
|
-
|
29
|
-
|
26
|
+
|
27
|
+
def cached_belongs_to_child_callback_name(association)
|
28
|
+
"cached_belongs_to_#{association.name}_child_callback"
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_cached_belongs_to_child_callbacks(caches, association)
|
32
|
+
klass = association.name
|
33
|
+
define_method cached_belongs_to_child_callback_name(association) do
|
30
34
|
caches.each do |attr|
|
31
35
|
send("#{klass}_#{attr}=", send(klass).send(attr)) if send(klass)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
35
|
-
before_save
|
39
|
+
before_save cached_belongs_to_child_callback_name(association)
|
36
40
|
end
|
37
41
|
|
38
|
-
def create_cached_belongs_to_parent_callbacks(caches,
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
def create_cached_belongs_to_parent_callbacks(caches, association)
|
43
|
+
parent_class_name = association.name
|
44
|
+
method_name = "cached_belongs_to_#{parent_class_name}_parent_callback".to_sym
|
45
|
+
has_many_association = self.name.demodulize.underscore.pluralize.to_sym
|
46
|
+
children_callback_name = cached_belongs_to_child_callback_name(association)
|
47
|
+
# What is this? I don't even...
|
48
|
+
parent_class = association.klass.name.constantize
|
42
49
|
|
43
50
|
parent_class.send(:define_method, method_name) do
|
44
51
|
send(has_many_association).reload.each do |child|
|
@@ -17,7 +17,7 @@ describe CachedBelongsTo do
|
|
17
17
|
author_class
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
specify do
|
21
21
|
book_class.should respond_to(:cached_belongs_to)
|
22
22
|
end
|
23
23
|
|
@@ -26,20 +26,20 @@ describe CachedBelongsTo do
|
|
26
26
|
book_class.send(:cached_belongs_to, :author, { :caches => :name })
|
27
27
|
end
|
28
28
|
|
29
|
-
|
29
|
+
specify do
|
30
30
|
book_class.new.should respond_to(:author)
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
book_class.new.should respond_to(:
|
33
|
+
specify do
|
34
|
+
book_class.new.should respond_to(:cached_belongs_to_author_child_callback)
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
author_class.new.should respond_to(:
|
37
|
+
specify do
|
38
|
+
author_class.new.should respond_to(:cached_belongs_to_author_parent_callback)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
context "
|
42
|
+
context "cached_belongs_to_author_child_callback" do
|
43
43
|
before do
|
44
44
|
book_class.send(:cached_belongs_to, :author, { :caches => :name })
|
45
45
|
end
|
@@ -51,12 +51,12 @@ describe CachedBelongsTo do
|
|
51
51
|
|
52
52
|
book.stub(:author).and_return author
|
53
53
|
|
54
|
-
book.
|
54
|
+
book.cached_belongs_to_author_child_callback
|
55
55
|
book.author_name.should eq author.name
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
context "
|
59
|
+
context "cached_belongs_to_author_parent_callback" do
|
60
60
|
before do
|
61
61
|
book_class.send(:cached_belongs_to, :author, { :caches => :name })
|
62
62
|
author_class.send(:has_many, :books)
|
@@ -67,10 +67,10 @@ describe CachedBelongsTo do
|
|
67
67
|
book = book_class.new
|
68
68
|
|
69
69
|
author.stub_chain(:books, :reload).and_return([ book ])
|
70
|
-
book.should_receive(:
|
70
|
+
book.should_receive(:cached_belongs_to_author_child_callback)
|
71
71
|
book.should_receive :save
|
72
72
|
|
73
|
-
author.
|
73
|
+
author.cached_belongs_to_author_parent_callback
|
74
74
|
end
|
75
75
|
end
|
76
76
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Rankat
|
4
|
+
class Author < ActiveRecord::Base
|
5
|
+
has_many :books
|
6
|
+
end
|
7
|
+
|
8
|
+
class Book < ActiveRecord::Base
|
9
|
+
cached_belongs_to :author, :caches => :name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "Callbacks" do
|
14
|
+
before do
|
15
|
+
@author = Rankat::Author.create :name => 'John Mellencamp'
|
16
|
+
@book = Rankat::Book.new :title => 'Treasure Island'
|
17
|
+
@book.author = @author
|
18
|
+
@book.save!
|
19
|
+
|
20
|
+
@author.books << @book
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "child callback" do
|
24
|
+
specify do
|
25
|
+
@book.reload.author_name.should == 'John Mellencamp'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "parent callback" do
|
30
|
+
specify do
|
31
|
+
@author.name = 'Dick Tracy'
|
32
|
+
expect { @author.save; @book.reload }.to change(@book, :author_name).to('Dick Tracy')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cached_belongs_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -59,22 +59,6 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: cucumber
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ! '>='
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: '0'
|
70
|
-
type: :development
|
71
|
-
prerelease: false
|
72
|
-
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
|
-
requirements:
|
75
|
-
- - ! '>='
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: '0'
|
78
62
|
- !ruby/object:Gem::Dependency
|
79
63
|
name: sqlite3
|
80
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -140,15 +124,10 @@ files:
|
|
140
124
|
- Rakefile
|
141
125
|
- cached_belongs_to.gemspec
|
142
126
|
- config/cucumber.yml
|
143
|
-
- features/step_definitions/.gitkeep
|
144
|
-
- features/step_definitions/active_record_model_steps.rb
|
145
|
-
- features/step_definitions/cached_belongs_to_steps.rb
|
146
|
-
- features/support/env.rb
|
147
|
-
- features/update_cached_attributes_after_save_child_record.feature
|
148
|
-
- features/update_cached_attributes_after_save_parent_record.feature
|
149
127
|
- lib/cached_belongs_to.rb
|
150
128
|
- lib/cached_belongs_to/version.rb
|
151
129
|
- spec/cached_belongs_to_spec.rb
|
130
|
+
- spec/integration/callbacks_spec.rb
|
152
131
|
- spec/spec_helper.rb
|
153
132
|
- spec/support/warnings.rb
|
154
133
|
homepage: ''
|
@@ -165,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
144
|
version: '0'
|
166
145
|
segments:
|
167
146
|
- 0
|
168
|
-
hash: -
|
147
|
+
hash: -168795518375412283
|
169
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
149
|
none: false
|
171
150
|
requirements:
|
@@ -174,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
153
|
version: '0'
|
175
154
|
segments:
|
176
155
|
- 0
|
177
|
-
hash: -
|
156
|
+
hash: -168795518375412283
|
178
157
|
requirements: []
|
179
158
|
rubyforge_project:
|
180
159
|
rubygems_version: 1.8.23
|
@@ -182,12 +161,7 @@ signing_key:
|
|
182
161
|
specification_version: 3
|
183
162
|
summary: Denormalize your belongs_to associations
|
184
163
|
test_files:
|
185
|
-
- features/step_definitions/.gitkeep
|
186
|
-
- features/step_definitions/active_record_model_steps.rb
|
187
|
-
- features/step_definitions/cached_belongs_to_steps.rb
|
188
|
-
- features/support/env.rb
|
189
|
-
- features/update_cached_attributes_after_save_child_record.feature
|
190
|
-
- features/update_cached_attributes_after_save_parent_record.feature
|
191
164
|
- spec/cached_belongs_to_spec.rb
|
165
|
+
- spec/integration/callbacks_spec.rb
|
192
166
|
- spec/spec_helper.rb
|
193
167
|
- spec/support/warnings.rb
|
File without changes
|
@@ -1,39 +0,0 @@
|
|
1
|
-
Given /^model "(.*?)" exists with attributes:$/ do |model_class_name, attributes|
|
2
|
-
klass = Class.new(ActiveRecord::Base)
|
3
|
-
suppress_warnings { Object.const_set model_class_name, klass }
|
4
|
-
|
5
|
-
ActiveRecord::Schema.define(:version => 0) do
|
6
|
-
create_table model_class_name.tableize, :force => true do |t|
|
7
|
-
attributes.hashes.each do |row|
|
8
|
-
t.send(row["type"], row["attribute"])
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
Given /^a (.*?) exists:$/ do |model_name, table|
|
15
|
-
@models ||= {}
|
16
|
-
@models[model_name] = model_name.constantize.new
|
17
|
-
table.rows_hash.each do |attribute, value|
|
18
|
-
@models[model_name].send("#{attribute}=", value)
|
19
|
-
end
|
20
|
-
@models[model_name].save!
|
21
|
-
end
|
22
|
-
|
23
|
-
Given /^that (.*?) belongs to that (.*?)$/ do |child, parent|
|
24
|
-
@models[parent].send(child.underscore.pluralize).send(:push, @models[child])
|
25
|
-
|
26
|
-
#@models[child].send("#{parent.underscore}=", @models[parent])
|
27
|
-
end
|
28
|
-
|
29
|
-
When /^I save the (.*?)$/ do |model_name|
|
30
|
-
@models[model_name].save!
|
31
|
-
end
|
32
|
-
|
33
|
-
When /^I change the (.*?)'s name to "(.*?)"$/ do |model_name, new_name|
|
34
|
-
@models[model_name].name = new_name
|
35
|
-
end
|
36
|
-
|
37
|
-
Given /^(.*?) has many (.*?)$/ do |parent, child|
|
38
|
-
parent.constantize.send(:has_many, child.underscore)
|
39
|
-
end
|
@@ -1,7 +0,0 @@
|
|
1
|
-
Given /^(.*?) cached_belongs_to (.*?) with cached: "(.*?)"$/ do |model1, model2, cached_attribute|
|
2
|
-
model1.constantize.send(:cached_belongs_to, model2.underscore.to_sym, :caches => cached_attribute)
|
3
|
-
end
|
4
|
-
|
5
|
-
Then /^(.*?)'s author name should be "(.*?)"$/ do |model_name, author_name|
|
6
|
-
@models[model_name].reload.author_name.should == author_name
|
7
|
-
end
|
data/features/support/env.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Feature: Update cached attributes after saving the child record
|
2
|
-
|
3
|
-
Scenario:
|
4
|
-
Given model "Author" exists with attributes:
|
5
|
-
| attribute | type |
|
6
|
-
| name | string |
|
7
|
-
And model "Book" exists with attributes:
|
8
|
-
| attribute | type |
|
9
|
-
| title | string |
|
10
|
-
| author_id | integer |
|
11
|
-
| author_name | string |
|
12
|
-
And Book cached_belongs_to Author with cached: "name"
|
13
|
-
And Author has many Books
|
14
|
-
And a Author exists:
|
15
|
-
| name | John Mellencamp |
|
16
|
-
And a Book exists:
|
17
|
-
| title | Treasure Island |
|
18
|
-
And that Book belongs to that Author
|
19
|
-
When I save the Book
|
20
|
-
Then Book's author name should be "John Mellencamp"
|
@@ -1,21 +0,0 @@
|
|
1
|
-
Feature: Update cached attributes after saving the parent record
|
2
|
-
|
3
|
-
Scenario:
|
4
|
-
Given model "Author" exists with attributes:
|
5
|
-
| attribute | type |
|
6
|
-
| name | string |
|
7
|
-
And model "Book" exists with attributes:
|
8
|
-
| attribute | type |
|
9
|
-
| title | string |
|
10
|
-
| author_id | integer |
|
11
|
-
| author_name | string |
|
12
|
-
And Book cached_belongs_to Author with cached: "name"
|
13
|
-
And Author has many Books
|
14
|
-
And a Author exists:
|
15
|
-
| name | John Mellencamp |
|
16
|
-
And a Book exists:
|
17
|
-
| title | Treasure Island |
|
18
|
-
And that Book belongs to that Author
|
19
|
-
When I change the Author's name to "Deeprak Chopa"
|
20
|
-
And I save the Author
|
21
|
-
Then Book's author name should be "Deeprak Chopa"
|