Skip to content

Instantly share code, notes, and snippets.

@rsl
Created March 4, 2009 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsl/74019 to your computer and use it in GitHub Desktop.
Save rsl/74019 to your computer and use it in GitHub Desktop.
## 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" ? "&#8595;" : "&#8593;")
end
else
query_string << "sort=#{column}"
query_string << "&amp;direction="
query_string << (params[:direction] == order ? opposite : order)
text << direction_span(opposite == "desc" ? "&#8595;" : "&#8593;")
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 << "&amp;direction="
query_string << (params[:sort] == column && params[:direction] == order ? opposite : order)
if params[:sort] == column
text << direction_span(params[:direction] == "desc" ? "&#8595;" : "&#8593;")
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