Skip to content

Commit

Permalink
Added support for should_have_one :through [#115 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed May 6, 2009
1 parent f03757e commit 79c7b87
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/shoulda/active_record/macros.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ def should_have_many(*associations)
# should_have_one :god # unless hindu # should_have_one :god # unless hindu
# #
def should_have_one(*associations) def should_have_one(*associations)
dependent = get_options!(associations, :dependent) dependent, through = get_options!(associations, :dependent, :through)
klass = model_class klass = model_class
associations.each do |association| associations.each do |association|
matcher = have_one(association).dependent(dependent) matcher = have_one(association).dependent(dependent).through(through)
should matcher.description do should matcher.description do
assert_accepts(matcher, klass.new) assert_accepts(matcher, klass.new)
end end
Expand Down
6 changes: 3 additions & 3 deletions lib/shoulda/active_record/matchers/association_matcher.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def through_association_valid?


def through_association_exists? def through_association_exists?
if through_reflection.nil? if through_reflection.nil?
"#{model_class.name} does not have any relationship to #{@through}" @missing = "#{model_class.name} does not have any relationship to #{@through}"
false false
else else
true true
Expand All @@ -142,10 +142,10 @@ def through_association_exists?


def through_association_correct? def through_association_correct?
if @through == reflection.options[:through] if @through == reflection.options[:through]
"Expected #{model_class.name} to have #{@name} through #{@through}, " <<
" but got it through #{reflection.options[:through]}"
true true
else else
@missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " <<
"but got it through #{reflection.options[:through]}"
false false
end end
end end
Expand Down
9 changes: 7 additions & 2 deletions test/matchers/active_record/association_matcher_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
define_model :parent do define_model :parent do
has_many :children has_many :children
end end
assert_rejects @matcher.through(:conceptions), Parent.new assert_rejects @matcher.through(:conceptions),
Parent.new,
:message => /does not have any relationship to conceptions/
end end


should "reject an association that has the wrong :through option" do should "reject an association that has the wrong :through option" do
Expand All @@ -137,9 +139,12 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end end
define_model :parent do define_model :parent do
has_many :conceptions has_many :conceptions
has_many :relationships
has_many :children, :through => :conceptions has_many :children, :through => :conceptions
end end
assert_rejects @matcher.through(:relationships), Parent.new assert_rejects @matcher.through(:relationships),
Parent.new,
:message => /through relationships, but got it through conceptions/
end end


should "accept an association with a valid :dependent option" do should "accept an association with a valid :dependent option" do
Expand Down
2 changes: 2 additions & 0 deletions test/rails_root/app/models/profile.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
class Profile < ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions test/rails_root/app/models/registration.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
class Registration < ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions test/rails_root/app/models/user.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class User < ActiveRecord::Base
has_many :friends, :through => :friendships has_many :friends, :through => :friendships


has_one :address, :as => :addressable, :dependent => :destroy has_one :address, :as => :addressable, :dependent => :destroy
has_one :registration
has_one :profile, :through => :registration


named_scope :old, :conditions => "age > 50" named_scope :old, :conditions => "age > 50"
named_scope :eighteen, :conditions => { :age => 18 } named_scope :eighteen, :conditions => { :age => 18 }
Expand Down
12 changes: 12 additions & 0 deletions test/rails_root/db/migrate/20090506203502_create_profiles.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateProfiles < ActiveRecord::Migration
def self.up
create_table :profiles do |t|

t.timestamps
end
end

def self.down
drop_table :profiles
end
end
14 changes: 14 additions & 0 deletions test/rails_root/db/migrate/20090506203536_create_registrations.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateRegistrations < ActiveRecord::Migration
def self.up
create_table :registrations do |t|
t.integer :user_id
t.integer :profile_id

t.timestamps
end
end

def self.down
drop_table :registrations
end
end
7 changes: 7 additions & 0 deletions test/unit/user_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ class UserTest < ActiveSupport::TestCase
should_fail do should_fail do
should_not_allow_mass_assignment_of :name, :age should_not_allow_mass_assignment_of :name, :age
end end

should_have_one :profile, :through => :registration

should_fail do
should_have_one :profile, :through => :interview
should_have_one :address, :through => :registration
end
end end

0 comments on commit 79c7b87

Please sign in to comment.