add order clause
This commit is contained in:
parent
85d3e5ea00
commit
7e999d73a6
|
@ -68,6 +68,11 @@ module Shoulda # :nodoc:
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def order(order)
|
||||||
|
@order = order
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def matches?(subject)
|
def matches?(subject)
|
||||||
@subject = subject
|
@subject = subject
|
||||||
association_exists? &&
|
association_exists? &&
|
||||||
|
@ -75,6 +80,7 @@ module Shoulda # :nodoc:
|
||||||
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
|
||||||
|
|
||||||
|
@ -90,6 +96,7 @@ module Shoulda # :nodoc:
|
||||||
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
|
||||||
|
|
||||||
|
@ -159,6 +166,15 @@ module Shoulda # :nodoc:
|
||||||
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 ||
|
||||||
::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
|
::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
|
||||||
|
|
|
@ -160,6 +160,23 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
end
|
end
|
||||||
Parent.new.should_not @matcher.dependent(:destroy)
|
Parent.new.should_not @matcher.dependent(:destroy)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "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
|
||||||
|
Parent.new.should @matcher.order(:id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should reject an association with a bad :order option" do
|
||||||
|
define_model :child, :parent_id => :integer
|
||||||
|
define_model :parent do
|
||||||
|
has_many :children
|
||||||
|
end
|
||||||
|
Parent.new.should_not @matcher.order(:id)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "have_one" do
|
context "have_one" do
|
||||||
|
@ -216,6 +233,23 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
end
|
end
|
||||||
Person.new.should_not @matcher.dependent(:destroy)
|
Person.new.should_not @matcher.dependent(:destroy)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "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
|
||||||
|
Person.new.should @matcher.order(:id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should reject an association with a bad :order option" do
|
||||||
|
define_model :detail, :person_id => :integer
|
||||||
|
define_model :person do
|
||||||
|
has_one :detail
|
||||||
|
end
|
||||||
|
Person.new.should_not @matcher.order(:id)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "have_and_belong_to_many" do
|
context "have_and_belong_to_many" do
|
||||||
|
|
Loading…
Reference in New Issue