mirror of https://github.com/rails/rails
expanding the test to include both type of order declaration
while declaring default_scope Also added test for unscoped using block style with four different combinations Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
8ae913664f
commit
9401fa0b9c
|
@ -680,19 +680,35 @@ class RelationTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_order_with_find_with_order
|
||||
assert_equal 'zyke', Car.order('name desc').find(:first, :order => 'id').name
|
||||
assert_equal 'zyke', CoolCar.order('name desc').find(:first, :order => 'id').name
|
||||
assert_equal 'zyke', FastCar.order('name desc').find(:first, :order => 'id').name
|
||||
end
|
||||
|
||||
def test_default_scope_order_with_named_scope_order
|
||||
assert_equal 'zyke', Car.order_using_new_style.limit(1).first.name
|
||||
assert_equal 'zyke', Car.order_using_old_style.limit(1).first.name
|
||||
assert_equal 'zyke', CoolCar.order_using_new_style.limit(1).first.name
|
||||
assert_equal 'zyke', CoolCar.order_using_old_style.limit(1).first.name
|
||||
assert_equal 'zyke', FastCar.order_using_new_style.limit(1).first.name
|
||||
assert_equal 'zyke', FastCar.order_using_old_style.limit(1).first.name
|
||||
end
|
||||
|
||||
def test_order_using_scoping
|
||||
car = Car.order('id DESC').scoping do
|
||||
Car.find(:first, :order => 'id asc')
|
||||
car1 = CoolCar.order('id DESC').scoping do
|
||||
CoolCar.find(:first, :order => 'id asc')
|
||||
end
|
||||
assert_equal 'zyke', car.name
|
||||
assert_equal 'zyke', car1.name
|
||||
|
||||
car2 = FastCar.order('id DESC').scoping do
|
||||
FastCar.find(:first, :order => 'id asc')
|
||||
end
|
||||
assert_equal 'zyke', car2.name
|
||||
end
|
||||
|
||||
def test_unscoped_block_style
|
||||
assert_equal 'honda', CoolCar.unscoped { CoolCar.order_using_new_style.limit(1).first.name}
|
||||
assert_equal 'honda', CoolCar.unscoped { CoolCar.order_using_old_style.limit(1).first.name}
|
||||
|
||||
assert_equal 'honda', FastCar.unscoped { FastCar.order_using_new_style.limit(1).first.name}
|
||||
assert_equal 'honda', FastCar.unscoped { FastCar.order_using_old_style.limit(1).first.name}
|
||||
end
|
||||
|
||||
def test_intersection_with_array
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
class Car < ActiveRecord::Base
|
||||
|
||||
has_many :bulbs
|
||||
has_many :tyres
|
||||
has_many :engines
|
||||
|
@ -7,9 +8,15 @@ class Car < ActiveRecord::Base
|
|||
scope :incl_tyres, includes(:tyres)
|
||||
scope :incl_engines, includes(:engines)
|
||||
|
||||
default_scope :order => 'name desc'
|
||||
|
||||
scope :order_using_new_style, order('name asc')
|
||||
scope :order_using_old_style, :order => 'name asc'
|
||||
|
||||
end
|
||||
|
||||
class CoolCar < Car
|
||||
default_scope :order => 'name desc'
|
||||
end
|
||||
|
||||
class FastCar < Car
|
||||
default_scope order('name desc')
|
||||
end
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
class Engine < ActiveRecord::Base
|
||||
belongs_to :my_car, :class_name => 'Car', :foreign_key => 'car_id', :counter_cache => :engines_count
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue