Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support for order clause

  • Loading branch information...
commit e0cbcdbd3038aa8b1954c8d211f1033957a4a023 1 parent acb71a6
@oscardelben authored
View
16 lib/shoulda/active_record/matchers/association_matcher.rb
@@ -67,6 +67,11 @@ def dependent(dependent)
@dependent = dependent
self
end
+
+ def order(order)
+ @order = order
+ self
+ end
def matches?(subject)
@subject = subject
@@ -75,6 +80,7 @@ def matches?(subject)
foreign_key_exists? &&
through_association_valid? &&
dependent_correct? &&
+ order_correct? &&
join_table_exists?
end
@@ -90,6 +96,7 @@ def description
description = "#{macro_description} #{@name}"
description += " through #{@through}" if @through
description += " dependent => #{@dependent}" if @dependent
+ description += " order => #{@order}" if @order
description
end
@@ -158,6 +165,15 @@ def dependent_correct?
false
end
end
+
+ def order_correct?
+ if @order.nil? || @order.to_s == reflection.options[:order].to_s
+ true
+ else
+ @missing = "#{@name} should be ordered by #{@order}"
+ false
+ end
+ end
def join_table_exists?
if @macro != :has_and_belongs_to_many ||
View
32 test/matchers/active_record/association_matcher_test.rb
@@ -162,6 +162,22 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end
assert_rejects @matcher.dependent(:destroy), Parent.new
end
+
+ should "accept an association with a valid :order option" do
+ define_model :child, :parent_id => :integer
+ define_model :parent do
+ has_many :children, :order => :id
+ end
+ assert_accepts @matcher.order(:id), Parent.new
+ end
+
+ should "reject an association with a bad :order option" do
+ define_model :child, :parent_id => :integer
+ define_model :parent do
+ has_many :children
+ end
+ assert_rejects @matcher.order(:id), Parent.new
+ end
end
context "have_one" do
@@ -218,6 +234,22 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end
assert_rejects @matcher.dependent(:destroy), Person.new
end
+
+ should "accept an association with a valid :order option" do
+ define_model :detail, :person_id => :integer
+ define_model :person do
+ has_one :detail, :order => :id
+ end
+ assert_accepts @matcher.order(:id), Person.new
+ end
+
+ should "reject an association with a bad :order option" do
+ define_model :detail, :person_id => :integer
+ define_model :person do
+ has_one :detail
+ end
+ assert_rejects @matcher.order(:id), Person.new
+ end
end
context "have_and_belong_to_many" do
Please sign in to comment.
Something went wrong with that request. Please try again.