scopes_for_associations 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +7 -1
- data/lib/scopes_for_associations/version.rb +1 -1
- data/lib/scopes_for_associations.rb +7 -1
- data/spec/scopes_for_associations_spec.rb +41 -21
- data/spec/support/matchers.rb +13 -0
- metadata +33 -2
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -11,6 +11,8 @@ Define very basic, commonly used scopes for ActiveRecord associations.
|
|
11
11
|
scopes_for_associations
|
12
12
|
end
|
13
13
|
|
14
|
+
Movie.for_director_id(Director.first.id)
|
15
|
+
#=> [#<Movie ..., director_id: 1>]
|
14
16
|
Movie.for_director(Director.first)
|
15
17
|
#=> [#<Movie ..., director_id: 1>]
|
16
18
|
Movie.for(Director.first)
|
@@ -31,6 +33,8 @@ The examples here are basically reproductions of the tests.
|
|
31
33
|
scopes_for_associations
|
32
34
|
end
|
33
35
|
|
36
|
+
Movie.for_director_id(Director.first.id)
|
37
|
+
#=> [#<Movie ..., director_id: 1>]
|
34
38
|
Movie.for_director(Director.first)
|
35
39
|
#=> [#<Movie ..., director_id: 1>]
|
36
40
|
Movie.for(Director.first)
|
@@ -46,8 +50,10 @@ The examples here are basically reproductions of the tests.
|
|
46
50
|
belongs_to :commentable, :polymorphic => true
|
47
51
|
end
|
48
52
|
|
49
|
-
Comment.
|
53
|
+
Comment.for_commentable_id(Movie.first.id)
|
50
54
|
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
|
55
|
+
Comment.for_commentable_type('Movie')
|
56
|
+
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>, ...]
|
51
57
|
Comment.for_commentable(movie)
|
52
58
|
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
|
53
59
|
Comment.for(Movie.first)
|
@@ -19,7 +19,10 @@ module ScopesForAssociations
|
|
19
19
|
def define_scopes_for_non_polymorpic_belongs_to_associations
|
20
20
|
non_polymorphic_belongs_to_reflections.each do |reflection|
|
21
21
|
name = reflection.name
|
22
|
-
|
22
|
+
attribute_name = :"#{name}_id"
|
23
|
+
scope_name_for_attribute_id = :"for_#{attribute_name}"
|
24
|
+
scope scope_name_for_attribute_id, proc { |id| where(attribute_name => id) }
|
25
|
+
scope :"for_#{name}", proc { |obj| send(scope_name_for_attribute_id, obj.id) }
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
@@ -27,7 +30,10 @@ module ScopesForAssociations
|
|
27
30
|
polymorphic_belongs_to_reflections.each do |reflection|
|
28
31
|
name = reflection.name
|
29
32
|
attribute_name_for_polymorphic_type = :"#{name}_type"
|
33
|
+
attribute_name_for_polymorphic_id = :"#{name}_id"
|
34
|
+
scope_name_for_polymorphic_id = :"for_#{attribute_name_for_polymorphic_id}"
|
30
35
|
scope_name_for_polymorphic_type = :"for_#{attribute_name_for_polymorphic_type}"
|
36
|
+
scope scope_name_for_polymorphic_id, proc { |id| where(attribute_name_for_polymorphic_id => id) }
|
31
37
|
scope scope_name_for_polymorphic_type, proc { |string| where(attribute_name_for_polymorphic_type => string) }
|
32
38
|
scope :"for_#{name}", proc { |obj| send(scope_name_for_polymorphic_type, obj.class.name).where(:"#{name}_id" => obj.id) }
|
33
39
|
end
|
@@ -15,22 +15,33 @@ describe ScopesForAssociations do
|
|
15
15
|
|
16
16
|
context 'for non-polymorphic belongs_to associations' do
|
17
17
|
|
18
|
-
it 'should define
|
19
|
-
Movie.should
|
20
|
-
|
18
|
+
it 'should define useful scopes' do
|
19
|
+
Movie.should define_scopes(
|
20
|
+
:for_director_id,
|
21
|
+
:for_director,
|
22
|
+
:for_production_company_id,
|
23
|
+
:for_production_company,
|
24
|
+
:for
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should scope for association id' do
|
29
|
+
Movie.for_director_id(director.id).should query_the_same_as(Movie.where(:director_id => director.id))
|
30
|
+
Movie.for_production_company_id(production_company.id).should query_the_same_as(Movie.where(:production_company_id => production_company.id))
|
21
31
|
end
|
22
32
|
|
23
33
|
it 'should scope for association as object' do
|
24
|
-
Movie.for_director(director).
|
25
|
-
Movie.
|
26
|
-
Movie.for_production_company(production_company).to_sql.should eq(Movie.where(:production_company_id => production_company.id).to_sql)
|
27
|
-
Movie.for_production_company(director).should eq([movie])
|
34
|
+
Movie.for_director(director).should query_the_same_as(Movie.for_director_id(director.id))
|
35
|
+
Movie.for_production_company(production_company).should query_the_same_as(Movie.for_production_company_id(production_company.id))
|
28
36
|
end
|
29
37
|
|
30
38
|
it 'should have an all encompassing scope :for' do
|
31
|
-
Movie.for(director).
|
39
|
+
Movie.for(director).should query_the_same_as(Movie.for_director(director))
|
40
|
+
Movie.for(production_company).should query_the_same_as(Movie.for_production_company(production_company))
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should return correct results' do
|
32
44
|
Movie.for(director).should eq([movie])
|
33
|
-
Movie.for(production_company).to_sql.should eq(Movie.for_production_company(production_company).to_sql)
|
34
45
|
Movie.for(production_company).should eq([movie])
|
35
46
|
end
|
36
47
|
|
@@ -38,27 +49,36 @@ describe ScopesForAssociations do
|
|
38
49
|
|
39
50
|
context 'for polymorphic belongs_to associations' do
|
40
51
|
|
41
|
-
it 'should define
|
42
|
-
Comment.should
|
43
|
-
|
44
|
-
|
52
|
+
it 'should define uesful scopes' do
|
53
|
+
Comment.should define_scopes(
|
54
|
+
:for_commentable_id,
|
55
|
+
:for_commentable_type,
|
56
|
+
:for_commentable,
|
57
|
+
:for
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should scope for polymorphic id' do
|
62
|
+
Comment.for_commentable_type('Movie').should query_the_same_as(Comment.where(:commentable_type => 'Movie'))
|
45
63
|
end
|
46
64
|
|
47
65
|
it 'should scope for polymorphic type' do
|
48
|
-
Comment.
|
49
|
-
Comment.for_commentable_type('Movie').should eq([movie_comment])
|
66
|
+
Comment.for_commentable_id(movie.id).should query_the_same_as(Comment.where(:commentable_id => movie.id))
|
50
67
|
end
|
51
68
|
|
52
69
|
it 'should scope for association as object' do
|
53
|
-
Comment.for_commentable(movie).
|
54
|
-
Comment.for_commentable(
|
55
|
-
Comment.for_commentable(trailer).to_sql.should eq(Comment.where(:commentable_type => 'Trailer', :commentable_id => trailer.id).to_sql)
|
56
|
-
Comment.for_commentable(trailer).should eq([trailer_comment])
|
70
|
+
Comment.for_commentable(movie).should query_the_same_as(Comment.where(:commentable_type => 'Movie', :commentable_id => movie.id))
|
71
|
+
Comment.for_commentable(trailer).should query_the_same_as(Comment.where(:commentable_type => 'Trailer', :commentable_id => trailer.id))
|
57
72
|
end
|
58
73
|
|
59
74
|
it 'should have an all encompassing scope :for' do
|
60
|
-
Comment.for(movie).
|
61
|
-
Comment.for(trailer).
|
75
|
+
Comment.for(movie).should query_the_same_as(Comment.for_commentable(movie))
|
76
|
+
Comment.for(trailer).should query_the_same_as(Comment.for_commentable(trailer))
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should return correct results' do
|
80
|
+
Comment.for(movie).should eq([movie_comment])
|
81
|
+
Comment.for(trailer).should eq([trailer_comment])
|
62
82
|
end
|
63
83
|
|
64
84
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
RSpec::Matchers.define :define_scopes do |*scopes|
|
2
|
+
match do |model_class|
|
3
|
+
scopes.each do |scope|
|
4
|
+
model_class.scopes.keys.should include(scope)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec::Matchers.define :query_the_same_as do |expected_relation|
|
10
|
+
match do |example_relation|
|
11
|
+
example_relation.to_sql.should eq(expected_relation.to_sql)
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopes_for_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Jared Ning
|
@@ -10,7 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-13 00:00:00 -06:00
|
14
19
|
default_executable:
|
15
20
|
dependencies:
|
16
21
|
- !ruby/object:Gem::Dependency
|
@@ -21,6 +26,11 @@ dependencies:
|
|
21
26
|
requirements:
|
22
27
|
- - ">="
|
23
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
24
34
|
version: 3.0.0
|
25
35
|
type: :runtime
|
26
36
|
version_requirements: *id001
|
@@ -32,6 +42,9 @@ dependencies:
|
|
32
42
|
requirements:
|
33
43
|
- - ">="
|
34
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
35
48
|
version: "0"
|
36
49
|
type: :development
|
37
50
|
version_requirements: *id002
|
@@ -43,6 +56,11 @@ dependencies:
|
|
43
56
|
requirements:
|
44
57
|
- - "="
|
45
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 27
|
60
|
+
segments:
|
61
|
+
- 2
|
62
|
+
- 5
|
63
|
+
- 0
|
46
64
|
version: 2.5.0
|
47
65
|
type: :development
|
48
66
|
version_requirements: *id003
|
@@ -54,6 +72,11 @@ dependencies:
|
|
54
72
|
requirements:
|
55
73
|
- - "="
|
56
74
|
- !ruby/object:Gem::Version
|
75
|
+
hash: 21
|
76
|
+
segments:
|
77
|
+
- 1
|
78
|
+
- 2
|
79
|
+
- 5
|
57
80
|
version: 1.2.5
|
58
81
|
type: :development
|
59
82
|
version_requirements: *id004
|
@@ -79,6 +102,7 @@ files:
|
|
79
102
|
- scopes_for_associations.gemspec
|
80
103
|
- spec/scopes_for_associations_spec.rb
|
81
104
|
- spec/spec_helper.rb
|
105
|
+
- spec/support/matchers.rb
|
82
106
|
- spec/support/models.rb
|
83
107
|
- spec/support/schema.rb
|
84
108
|
has_rdoc: true
|
@@ -95,12 +119,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
119
|
requirements:
|
96
120
|
- - ">="
|
97
121
|
- !ruby/object:Gem::Version
|
122
|
+
hash: 3
|
123
|
+
segments:
|
124
|
+
- 0
|
98
125
|
version: "0"
|
99
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
127
|
none: false
|
101
128
|
requirements:
|
102
129
|
- - ">="
|
103
130
|
- !ruby/object:Gem::Version
|
131
|
+
hash: 3
|
132
|
+
segments:
|
133
|
+
- 0
|
104
134
|
version: "0"
|
105
135
|
requirements: []
|
106
136
|
|
@@ -112,5 +142,6 @@ summary: Define very basic, commonly used scopes for ActiveRecord associations
|
|
112
142
|
test_files:
|
113
143
|
- spec/scopes_for_associations_spec.rb
|
114
144
|
- spec/spec_helper.rb
|
145
|
+
- spec/support/matchers.rb
|
115
146
|
- spec/support/models.rb
|
116
147
|
- spec/support/schema.rb
|