Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for order clause #165

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/shoulda/active_record/matchers/association_matcher.rb
Expand Up @@ -67,6 +67,11 @@ def dependent(dependent)
@dependent = dependent @dependent = dependent
self self
end end

def order(order)
@order = order
self
end


def matches?(subject) def matches?(subject)
@subject = subject @subject = subject
Expand All @@ -75,6 +80,7 @@ def matches?(subject)
foreign_key_exists? && foreign_key_exists? &&
through_association_valid? && through_association_valid? &&
dependent_correct? && dependent_correct? &&
order_correct? &&
join_table_exists? join_table_exists?
end end


Expand All @@ -90,6 +96,7 @@ def description
description = "#{macro_description} #{@name}" description = "#{macro_description} #{@name}"
description += " through #{@through}" if @through description += " through #{@through}" if @through
description += " dependent => #{@dependent}" if @dependent description += " dependent => #{@dependent}" if @dependent
description += " order => #{@order}" if @order
description description
end end


Expand Down Expand Up @@ -158,6 +165,15 @@ def dependent_correct?
false false
end end
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? def join_table_exists?
if @macro != :has_and_belongs_to_many || if @macro != :has_and_belongs_to_many ||
Expand Down
32 changes: 32 additions & 0 deletions test/matchers/active_record/association_matcher_test.rb
Expand Up @@ -162,6 +162,22 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end end
assert_rejects @matcher.dependent(:destroy), Parent.new assert_rejects @matcher.dependent(:destroy), Parent.new
end 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 end


context "have_one" do context "have_one" do
Expand Down Expand Up @@ -218,6 +234,22 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end end
assert_rejects @matcher.dependent(:destroy), Person.new assert_rejects @matcher.dependent(:destroy), Person.new
end 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 end


context "have_and_belong_to_many" do context "have_and_belong_to_many" do
Expand Down