scopes_for_associations 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
- Commentable.for_movie(Movie.first)
49
+ Comment.for_commentable_type('Movie')
50
50
  #=> [#<Comment ..., commentable_type: "Movie", commentable_id: 1>]
51
- Commentable.for(Movie.first)
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
@@ -1,3 +1,3 @@
1
1
  module ScopesForAssociations
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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
- scope :"for_#{name}", proc { |obj| where(:"#{name}_type" => obj.class.name, :"#{name}_id" => obj.id) }
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 "no scope for #{object.class}"
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(:comment) { Comment.create! :commentable_type => 'Movie', :commentable_id => movie.id }
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('scopes_for_associations')
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('for_director')
18
- Movie.should respond_to('for_production_company')
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 a "polymorphic" scope :for' do
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('for_commentable')
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 association as object' do
44
- Comment.for_commentable(movie)
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([comment])
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 a "polymorphic" scope :for' do
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
@@ -16,6 +16,7 @@ class Trailer < ActiveRecord::Base
16
16
  belongs_to :movie
17
17
  belongs_to :director
18
18
  belongs_to :production_company
19
+ has_many :comments, :as => :commentable
19
20
  scopes_for_associations
20
21
  end
21
22
 
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.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-08 00:00:00 -06:00
13
+ date: 2011-02-12 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency