7
7
class OrderTest < ActiveRecord ::TestCase
8
8
fixtures :authors , :author_addresses
9
9
10
+ def setup
11
+ Book . delete_all
12
+ end
13
+
10
14
def test_order_asc
11
- Book . destroy_all
12
15
z = Book . create! ( name : "Zulu" , author : authors ( :david ) )
13
16
y = Book . create! ( name : "Yankee" , author : authors ( :mary ) )
14
17
x = Book . create! ( name : "X-Ray" , author : authors ( :david ) )
@@ -26,7 +29,6 @@ def test_order_asc
26
29
end
27
30
28
31
def test_order_desc
29
- Book . destroy_all
30
32
z = Book . create! ( name : "Zulu" , author : authors ( :david ) )
31
33
y = Book . create! ( name : "Yankee" , author : authors ( :mary ) )
32
34
x = Book . create! ( name : "X-Ray" , author : authors ( :david ) )
@@ -44,7 +46,6 @@ def test_order_desc
44
46
end
45
47
46
48
def test_order_with_association
47
- Book . destroy_all
48
49
z = Book . create! ( name : "Zulu" , author : authors ( :david ) )
49
50
y = Book . create! ( name : "Yankee" , author : authors ( :mary ) )
50
51
x = Book . create! ( name : "X-Ray" , author : authors ( :david ) )
@@ -64,4 +65,26 @@ def test_order_with_association
64
65
assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( Author . arel_table [ :name ] . desc , books : { name : :asc } ) )
65
66
assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( { authors : { name : :desc } } , :name ) )
66
67
end
68
+
69
+ def test_order_with_association_alias
70
+ z = Book . create! ( name : "Zulu" , author : authors ( :david ) )
71
+ y = Book . create! ( name : "Yankee" , author : authors ( :mary ) )
72
+ x = Book . create! ( name : "X-Ray" , author : authors ( :david ) )
73
+
74
+ author_name = Author . arel_table . alias ( "author" ) [ :name ]
75
+
76
+ author_then_book_name = [ x , z , y ]
77
+
78
+ assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( author : { name : :asc } , books : { name : :asc } ) )
79
+ assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( "author.name" , books : { name : :asc } ) )
80
+ assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( { author : { name : :asc } } , :name ) )
81
+ assert_equal ( author_then_book_name , Book . includes ( :author ) . order ( author_name , :name ) )
82
+
83
+ author_desc_then_book_name = [ y , x , z ]
84
+
85
+ assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( author : { name : :desc } , books : { name : :asc } ) )
86
+ assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( "author.name desc" , books : { name : :asc } ) )
87
+ assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( { author : { name : :desc } } , :name ) )
88
+ assert_equal ( author_desc_then_book_name , Book . includes ( :author ) . order ( author_name . desc , :name ) )
89
+ end
67
90
end
0 commit comments