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

Use reflection table name when ordering on association columns #2450

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/administrate/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ def order_by_has_one(relation)
def order_by_attribute(relation)
relation.joins(
attribute.to_sym,
).reorder(Arel.sql(order_by_attribute_query))
).reorder(Arel.sql(order_by_attribute_query(relation)))
end

def order_by_id(relation)
relation.reorder(Arel.sql(order_by_id_query(relation)))
end

def order_by_association_id(relation)
relation.reorder(Arel.sql(order_by_association_id_query))
relation.reorder(Arel.sql(order_by_association_id_query(relation)))
end

def ordering_by_association_column?(relation)
Expand All @@ -118,12 +118,13 @@ def order_by_id_query(relation)
"#{relation.table_name}.#{foreign_key(relation)} #{direction}"
end

def order_by_association_id_query
"#{association_table_name}.id #{direction}"
def order_by_association_id_query(relation)
"#{association_table_name(relation)}.id #{direction}"
end

def order_by_attribute_query
"#{association_table_name}.#{association_attribute} #{direction}"
def order_by_attribute_query(relation)
table_name = association_table_name(relation)
"#{table_name}.#{association_attribute} #{direction}"
end

def relation_type(relation)
Expand All @@ -138,8 +139,8 @@ def foreign_key(relation)
reflect_association(relation).foreign_key
end

def association_table_name
attribute.tableize
def association_table_name(relation)
reflect_association(relation).klass.table_name
end
end
end
13 changes: 8 additions & 5 deletions spec/lib/administrate/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
context "when `order` argument valid" do
it "orders by the column" do
order = Administrate::Order.new(
double(to_sym: :user, tableize: "users"),
double(to_sym: :user),
nil,
association_attribute: "name",
)
Expand Down Expand Up @@ -156,9 +156,12 @@
context "when relation has has_one association" do
it "orders by id" do
order = Administrate::Order.new(
double(to_sym: :user, tableize: "users"),
double(to_sym: :user),
)
relation = relation_with_association(
:has_one,
klass: double(table_name: "users"),
)
relation = relation_with_association(:has_one)
allow(relation).to receive(:reorder).and_return(relation)

ordered = order.apply(relation)
Expand All @@ -172,7 +175,7 @@
context "when `order` argument valid" do
it "orders by the column" do
order = Administrate::Order.new(
double(to_sym: :user, tableize: "users"),
double(to_sym: :user),
nil,
association_attribute: "name",
)
Expand All @@ -197,7 +200,7 @@
context "when `order` argument invalid" do
it "orders by id" do
order = Administrate::Order.new(
double(to_sym: :user, tableize: "users"),
double(to_sym: :user),
nil,
association_attribute: "invalid_column_name",
)
Expand Down