Skip to content

Instantly share code, notes, and snippets.

@ebryn
Created February 20, 2010 19:12
Show Gist options
  • Save ebryn/309845 to your computer and use it in GitHub Desktop.
Save ebryn/309845 to your computer and use it in GitHub Desktop.
# From within a Arel compiler we'd like to call #invert_directions(orders)
# to reverse the order of all the order clauses
def invert_direction_value(str)
case str
when /\bDESC\b/i then str.gsub!(/\bDESC\b/i, "ASC")
when /\bASC\b/i then str.gsub!(/\bASC\b/i, "DESC")
end
str
end
def invert_direction(ordering)
if ordering.is_a?(Arel::Value)
invert_direction_value(ordering.value)
elsif ordering.is_a?(Arel::SqlLiteral)
invert_direction_value(ordering)
elsif ordering.is_a? Arel::Ordering
ordering.is_a?(Arel::Ascending) ? Arel::Descending.new(ordering.attribute) : Arel::Ascending.new(ordering.attribute)
end
end
def invert_directions(orderings)
orderings.map do |ordering|
invert_direction(ordering)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment