|
30 | 30 | end
|
31 | 31 |
|
32 | 32 | context "when `order` argument is valid" do
|
33 |
| - it "orders by the column" do |
| 33 | + it "orders by the column and tiebreaks by the primary key" do |
34 | 34 | order = Administrate::Order.new(:name, :asc)
|
35 | 35 | relation = relation_with_column(:name)
|
36 | 36 | allow(relation).to receive(:reorder).and_return(relation)
|
37 | 37 |
|
38 | 38 | ordered = order.apply(relation)
|
39 | 39 |
|
40 | 40 | expect(relation).to have_received(:reorder).with(
|
41 |
| - to_sql('"table_name"."name" ASC') |
| 41 | + to_sql('"table_name"."name" ASC'), |
| 42 | + to_sql('"table_name"."id" ASC') |
42 | 43 | )
|
43 | 44 | expect(ordered).to eq(relation)
|
44 | 45 | end
|
|
51 | 52 | ordered = order.apply(relation)
|
52 | 53 |
|
53 | 54 | expect(relation).to have_received(:reorder).with(
|
54 |
| - to_sql('"table_name"."name" DESC') |
| 55 | + to_sql('"table_name"."name" DESC'), |
| 56 | + to_sql('"table_name"."id" DESC') |
55 | 57 | )
|
56 | 58 | expect(ordered).to eq(relation)
|
57 | 59 | end
|
|
64 | 66 | ordered = order.apply(relation)
|
65 | 67 |
|
66 | 68 | expect(relation).to have_received(:reorder).with(
|
67 |
| - to_sql('"table_name"."name" ASC') |
| 69 | + to_sql('"table_name"."name" ASC'), |
| 70 | + to_sql('"table_name"."id" ASC') |
68 | 71 | )
|
69 | 72 | expect(ordered).to eq(relation)
|
70 | 73 | end
|
| 74 | + |
| 75 | + context "and same with own primary key" do |
| 76 | + it "orders by the primary key" do |
| 77 | + order = Administrate::Order.new(:id, :asc) |
| 78 | + relation = relation_with_column(:name) |
| 79 | + allow(relation).to receive(:reorder).and_return(relation) |
| 80 | + |
| 81 | + ordered = order.apply(relation) |
| 82 | + |
| 83 | + expect(relation).to have_received(:reorder).with( |
| 84 | + to_sql('"table_name"."id" ASC') |
| 85 | + ) |
| 86 | + expect(ordered).to eq(relation) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context "when the relation has no primary key" do |
| 91 | + it "orders by the column without tiebreaks" do |
| 92 | + order = Administrate::Order.new(:name, :asc) |
| 93 | + relation = double( |
| 94 | + klass: double(reflect_on_association: nil), |
| 95 | + columns_hash: {"name" => :column_info}, |
| 96 | + table_name: "table_name", |
| 97 | + arel_table: Arel::Table.new("table_name"), |
| 98 | + primary_key: nil |
| 99 | + ) |
| 100 | + allow(relation).to receive(:reorder).and_return(relation) |
| 101 | + |
| 102 | + ordered = order.apply(relation) |
| 103 | + |
| 104 | + expect(relation).to have_received(:reorder).with( |
| 105 | + to_sql('"table_name"."name" ASC') |
| 106 | + ) |
| 107 | + expect(ordered).to eq(relation) |
| 108 | + end |
| 109 | + end |
71 | 110 | end
|
72 | 111 |
|
73 | 112 | context "when relation has_many association" do
|
|
329 | 368 | def relation_with_column(column)
|
330 | 369 | double(
|
331 | 370 | klass: double(reflect_on_association: nil),
|
332 |
| - columns_hash: {column.to_s => :column_info}, |
| 371 | + columns_hash: {column.to_s => :column_info, "id" => :column_info}, |
333 | 372 | table_name: "table_name",
|
334 |
| - arel_table: Arel::Table.new("table_name") |
| 373 | + arel_table: Arel::Table.new("table_name"), |
| 374 | + primary_key: "id" |
335 | 375 | )
|
336 | 376 | end
|
337 | 377 |
|
|
0 commit comments