Skip to content

Commit

Permalink
Fold the two methods to avoid doing the join for nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickgramatowski committed Jul 8, 2022
1 parent 5e65f9d commit bd045e7
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/administrate/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,17 @@ def order_by_id(relation)
end

def order_by_attribute(relation)
return order_by_id(relation) unless field

relation.joins(
attribute.to_sym,
).reorder(Arel.sql(form_query(relation)))
if ordering_by_association_column?(relation)
relation.joins(
attribute.to_sym
).reorder(Arel.sql(order_by_attribute_query))
else
order_by_id(relation)
end
end

def form_query(relation)
return order_by_attribute_query if
column_exist?(reflect_association(relation).klass, field.to_sym)

order_by_id_query(relation)
def ordering_by_association_column?(relation)
association_attribute && column_exist?(reflect_association(relation).klass, association_attribute.to_sym)
end

def column_exist?(table, column_name)
Expand Down

0 comments on commit bd045e7

Please sign in to comment.