def order(search, options = {}, html_options = {})
options[:params_scope] ||= :search
if !options[:as]
id = options[:by].to_s.downcase == "id"
options[:as] = id ? options[:by].to_s.upcase : options[:by].to_s.humanize
end
options[:ascend_scope] ||= "ascend_by_#{options[:by]}"
options[:descend_scope] ||= "descend_by_#{options[:by]}"
ascending = search.order.to_s == options[:ascend_scope]
new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
selected = [options[:ascend_scope], options[:descend_scope]].include?(search.order.to_s)
if selected
css_classes = html_options[:class] ? html_options[:class].split(" ") : []
if ascending
options[:as] = "▲ #{options[:as]}"
css_classes << "ascending"
else
options[:as] = "▼ #{options[:as]}"
css_classes << "descending"
end
html_options[:class] = css_classes.join(" ")
end
url_options = {
options[:params_scope] => search.conditions.merge( { :order => new_scope } )
}.deep_merge(options[:params] || {})
options[:as] = raw(options[:as]) if defined?(RailsXss)
link_to options[:as], url_for(url_options), html_options
end