Created
March 4, 2009 21:33
-
-
Save rsl/74019 to your computer and use it in GitHub Desktop.
This file contains 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
## Helper | |
# Used for the column that is the default sort | |
def default_sortable_header(column, order, text = nil) | |
column = column.to_s.downcase | |
order = order.to_s.downcase | |
opposite = order == "desc" ? "asc" : "desc" | |
text ||= column.humanize | |
query_string = "" | |
if params[:sort] | |
if params[:sort] == column | |
text << direction_span(params[:direction] == "desc" ? "↓" : "↑") | |
end | |
else | |
query_string << "sort=#{column}" | |
query_string << "&direction=" | |
query_string << (params[:direction] == order ? opposite : order) | |
text << direction_span(opposite == "desc" ? "↓" : "↑") | |
end | |
link_to text, query_string.blank? ? request.path : "#{request.path}?#{query_string}" | |
end | |
# Used for other columns | |
def sortable_header(column, order, text = nil) | |
column = column.to_s | |
order = order.to_s | |
opposite = order == "desc" ? "asc" : "desc" | |
text ||= column.humanize | |
query_string = "sort=#{column}" | |
query_string << "&direction=" | |
query_string << (params[:sort] == column && params[:direction] == order ? opposite : order) | |
if params[:sort] == column | |
text << direction_span(params[:direction] == "desc" ? "↓" : "↑") | |
end | |
link_to text, query_string.blank? ? request.path : "#{request.path}?#{query_string}" | |
end | |
private | |
def direction_span(content) | |
content_tag :span, content, :class => :direction | |
end | |
## Relevant snippet from the template [Haml, obvs] | |
%tr | |
%th.attachment= # Attachment column | |
%th.main= sortable_header :title, :asc | |
- if current_site.show_categories_on_menu? | |
%th.tight Categories | |
- if current_site.show_tags_on_menu? | |
%th.tight Tags | |
%th.timestamp= default_sortable_header :created_at, :asc, "Created" | |
%th.timestamp= sortable_header :updated_at, :desc, "Updated" | |
%th.delete= # 'Delete' link column |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment