From e0cbcdbd3038aa8b1954c8d211f1033957a4a023 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 19 Jan 2011 14:57:46 +0100 Subject: [PATCH] support for order clause --- .../matchers/association_matcher.rb | 16 ++++++++++ .../active_record/association_matcher_test.rb | 32 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/shoulda/active_record/matchers/association_matcher.rb b/lib/shoulda/active_record/matchers/association_matcher.rb index 8396591..ae89587 100644 --- a/lib/shoulda/active_record/matchers/association_matcher.rb +++ b/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 || diff --git a/test/matchers/active_record/association_matcher_test.rb b/test/matchers/active_record/association_matcher_test.rb index 61b9c17..d645c45 100644 --- a/test/matchers/active_record/association_matcher_test.rb +++ b/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