Skip to content

Commit

Permalink
refactor: Refactored code in functions of query_methods.
Browse files Browse the repository at this point in the history
This is done because concerns of respective steps can be separated,
which in turns leads to easier walkthrough and verbosity. Also
stretching a function to large horizontal bounds doesn't looks good.
  • Loading branch information
yashLadha committed Mar 4, 2020
1 parent 35c2620 commit 373e1fc
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -1334,21 +1334,15 @@ def validate_order_args(args)
end

def preprocess_order_args(order_args)
order_args.reject!(&:blank?)
order_args.map! do |arg|
klass.sanitize_sql_for_order(arg)
end
order_args.flatten!

order_args = sanitize_order_arguments(order_args)
@klass.disallow_raw_sql!(
order_args.flat_map { |a| a.is_a?(Hash) ? a.keys : a },
permit: connection.column_name_with_order_matcher
)

validate_order_args(order_args)

references = order_args.grep(String)
references.map! { |arg| arg =~ /^\W?(\w+)\W?\./ && $1 }.compact!
references = column_references(order_args)
references!(references) if references.any?

# if a symbol is given we prepend the quoted table name
Expand All @@ -1371,6 +1365,21 @@ def preprocess_order_args(order_args)
end.flatten!
end

def sanitize_order_arguments(order_args)
order_args.reject!(&:blank?)
order_args.map! do |arg|
klass.sanitize_sql_for_order(arg)
end
order_args.flatten!
order_args
end

def column_references(order_args)
references = order_args.grep(String)
references.map! { |arg| arg =~ /^\W?(\w+)\W?\./ && $1 }.compact!
references
end

def order_column(field)
arel_column(field) do |attr_name|
if attr_name == "count" && !group_values.empty?
Expand Down

0 comments on commit 373e1fc

Please sign in to comment.