scopes_for_associations 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -2
- data/lib/scopes_for_associations/version.rb +1 -1
- data/lib/scopes_for_associations.rb +5 -2
- data/spec/scopes_for_associations_spec.rb +18 -10
- data/spec/support/models.rb +1 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -46,9 +46,11 @@ The examples here are basically reproductions of the tests.
|
|
46
46
|
belongs_to :commentable, :polymorphic => true
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
Comment.for_commentable_type('Movie')
|
50
50
|
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
|
51
|
-
|
51
|
+
Comment.for_commentable(movie)
|
52
|
+
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
|
53
|
+
Comment.for(Movie.first)
|
52
54
|
#=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
|
53
55
|
|
54
56
|
== Installation
|
@@ -26,7 +26,10 @@ module ScopesForAssociations
|
|
26
26
|
def define_scopes_for_polymorpic_belongs_to_associations
|
27
27
|
polymorphic_belongs_to_reflections.each do |reflection|
|
28
28
|
name = reflection.name
|
29
|
-
|
29
|
+
attribute_name_for_polymorphic_type = :"#{name}_type"
|
30
|
+
scope_name_for_polymorphic_type = :"for_#{attribute_name_for_polymorphic_type}"
|
31
|
+
scope scope_name_for_polymorphic_type, proc { |string| where(attribute_name_for_polymorphic_type => string) }
|
32
|
+
scope :"for_#{name}", proc { |obj| send(scope_name_for_polymorphic_type, obj.class.name).where(:"#{name}_id" => obj.id) }
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
@@ -48,7 +51,7 @@ module ScopesForAssociations
|
|
48
51
|
if reflection
|
49
52
|
send(:"for_#{reflection.options[:as]}", object)
|
50
53
|
else
|
51
|
-
raise "
|
54
|
+
raise "No scope for #{object.class}. If the associaton is polymorphic, make sure #{object.class} defines something like 'has_many :#{self.name.underscore.pluralize}, :as => :#{self.name.underscore + 'able'}'."
|
52
55
|
end
|
53
56
|
end
|
54
57
|
}
|
@@ -5,17 +5,19 @@ describe ScopesForAssociations do
|
|
5
5
|
let(:director) { Director.create! }
|
6
6
|
let(:production_company) { ProductionCompany.create! }
|
7
7
|
let(:movie) { Movie.create!(:director => director, :production_company => production_company) }
|
8
|
-
let(:
|
8
|
+
let(:trailer) { Trailer.create!(:director => director, :production_company => production_company, :movie => movie) }
|
9
|
+
let(:movie_comment) { Comment.create! :commentable_type => 'Movie', :commentable_id => movie.id }
|
10
|
+
let(:trailer_comment) { Comment.create! :commentable_type => 'Trailer', :commentable_id => trailer.id }
|
9
11
|
|
10
12
|
it 'should be included in ActiveRecord::Base' do
|
11
|
-
ActiveRecord::Base.should respond_to(
|
13
|
+
ActiveRecord::Base.should respond_to(:scopes_for_associations)
|
12
14
|
end
|
13
15
|
|
14
16
|
context 'for non-polymorphic belongs_to associations' do
|
15
17
|
|
16
18
|
it 'should define for_x scopes' do
|
17
|
-
Movie.should respond_to(
|
18
|
-
Movie.should respond_to(
|
19
|
+
Movie.should respond_to(:for_director)
|
20
|
+
Movie.should respond_to(:for_production_company)
|
19
21
|
end
|
20
22
|
|
21
23
|
it 'should scope for association as object' do
|
@@ -25,7 +27,7 @@ describe ScopesForAssociations do
|
|
25
27
|
Movie.for_production_company(director).should eq([movie])
|
26
28
|
end
|
27
29
|
|
28
|
-
it 'should have
|
30
|
+
it 'should have an all encompassing scope :for' do
|
29
31
|
Movie.for(director).to_sql.should eq(Movie.for_director(director).to_sql)
|
30
32
|
Movie.for(director).should eq([movie])
|
31
33
|
Movie.for(production_company).to_sql.should eq(Movie.for_production_company(production_company).to_sql)
|
@@ -37,20 +39,26 @@ describe ScopesForAssociations do
|
|
37
39
|
context 'for polymorphic belongs_to associations' do
|
38
40
|
|
39
41
|
it 'should define for_x scopes' do
|
40
|
-
Comment.should respond_to(
|
42
|
+
Comment.should respond_to(:for_commentable)
|
43
|
+
Comment.should respond_to(:for_commentable_type)
|
44
|
+
Comment.should respond_to(:for)
|
41
45
|
end
|
42
46
|
|
43
|
-
it 'should scope for
|
44
|
-
Comment.
|
47
|
+
it 'should scope for polymorphic type' do
|
48
|
+
Comment.for_commentable_type('Movie').to_sql.should eq(Comment.where(:commentable_type => 'Movie').to_sql)
|
49
|
+
Comment.for_commentable_type('Movie').should eq([movie_comment])
|
45
50
|
end
|
46
51
|
|
47
52
|
it 'should scope for association as object' do
|
48
53
|
Comment.for_commentable(movie).to_sql.should eq(Comment.where(:commentable_type => 'Movie', :commentable_id => movie.id).to_sql)
|
49
|
-
Comment.for_commentable(movie).should eq([
|
54
|
+
Comment.for_commentable(movie).should eq([movie_comment])
|
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])
|
50
57
|
end
|
51
58
|
|
52
|
-
it 'should have
|
59
|
+
it 'should have an all encompassing scope :for' do
|
53
60
|
Comment.for(movie).to_sql.should eq(Comment.for_commentable(movie).to_sql)
|
61
|
+
Comment.for(trailer).to_sql.should eq(Comment.for_commentable(trailer).to_sql)
|
54
62
|
end
|
55
63
|
|
56
64
|
end
|
data/spec/support/models.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: scopes_for_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jared Ning
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-12 00:00:00 -06:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|