Created
February 20, 2010 19:12
-
-
Save ebryn/309845 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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