Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 24
- Log:
Initial import of Collaboa 0.5.6 from downloaded Tarball. Collaboa is
a Ruby On Rails based bug tracker and SVN repository browsing tool.
- Author:
- adh
- Date:
- Mon Jul 24 21:54:39 +0100 2006
- Size:
- 4730 Bytes
1 | # Some smaller tweaks has been made to this helper. |
2 | |
3 | # Helpers to sort tables using clickable column headers. |
4 | # |
5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. |
6 | # |
7 | # - Consecutive clicks toggle the column's sort order. |
8 | # - Sort state is maintained by a session hash entry. |
9 | # - Icon image identifies sort column and state. |
10 | # - Typically used in conjunction with the Pagination module. |
11 | # |
12 | # Example code snippets: |
13 | # |
14 | # Controller: |
15 | # |
16 | # helper :sort |
17 | # include SortHelper |
18 | # |
19 | # def list |
20 | # sort_init 'last_name' |
21 | # sort_update |
22 | # @items = Contact.find_all nil, sort_clause |
23 | # end |
24 | # |
25 | # Controller (using Pagination module): |
26 | # |
27 | # helper :sort |
28 | # include SortHelper |
29 | # |
30 | # def list |
31 | # sort_init 'last_name' |
32 | # sort_update |
33 | # @contact_pages, @items = paginate :contacts, |
34 | # :order_by => sort_clause, |
35 | # :per_page => 10 |
36 | # end |
37 | # |
38 | # View (table header in list.rhtml): |
39 | # |
40 | # <thead> |
41 | # <tr> |
42 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> |
43 | # <%= sort_header_tag('last_name', :caption => 'Name') %> |
44 | # <%= sort_header_tag('phone') %> |
45 | # <%= sort_header_tag('address', :width => 200) %> |
46 | # </tr> |
47 | # </thead> |
48 | # |
49 | # - The ascending and descending sort icon images are sort_asc.png and |
50 | # sort_desc.png and reside in the application's images directory. |
51 | # - Introduces instance variables: @sort_name, @sort_default. |
52 | # - Introduces params :sort_key and :sort_order. |
53 | # |
54 | module SortHelper |
55 | |
56 | # Initializes the default sort column (default_key) and sort order |
57 | # (default_order). |
58 | # |
59 | # - default_key is a column attribute name. |
60 | # - default_order is 'asc' or 'desc'. |
61 | # - name is the name of the session hash entry that stores the sort state, |
62 | # defaults to '<controller_name>_sort'. |
63 | # |
64 | def sort_init(default_key, default_order='asc', name=nil) |
65 | @sort_name = name || params[:controller] + '_sort' |
66 | @sort_default = {:key => default_key, :order => default_order} |
67 | end |
68 | |
69 | # Updates the sort state. Call this in the controller prior to calling |
70 | # sort_clause. |
71 | # |
72 | def sort_update() |
73 | if params[:sort_key] |
74 | sort = {:key => params[:sort_key], :order => params[:sort_order]} |
75 | elsif session[@sort_name] |
76 | sort = session[@sort_name] # Previous sort. |
77 | else |
78 | sort = @sort_default |
79 | end |
80 | session[@sort_name] = sort |
81 | end |
82 | |
83 | # Returns an SQL sort clause corresponding to the current sort state. |
84 | # Use this to sort the controller's table items collection. |
85 | # |
86 | def sort_clause() |
87 | session[@sort_name][:key] + ' ' + session[@sort_name][:order] |
88 | end |
89 | |
90 | # Returns a link which sorts by the named column. |
91 | # |
92 | # - column is the name of an attribute in the sorted record collection. |
93 | # - The optional caption explicitly specifies the displayed link text. |
94 | # - A sort icon image is positioned to the right of the sort link. |
95 | # |
96 | def sort_link(column, caption=nil) |
97 | key, order = session[@sort_name][:key], session[@sort_name][:order] |
98 | if key == column |
99 | if order.downcase == 'asc' |
100 | icon = 'sort_asc' |
101 | order = 'desc' |
102 | else |
103 | icon = 'sort_desc' |
104 | order = 'asc' |
105 | end |
106 | else |
107 | icon = nil |
108 | order = 'asc' |
109 | end |
110 | caption = titleize(Inflector::humanize(column)) unless caption |
111 | params = @params.merge({:params => {:sort_key => column, :sort_order => order}}) |
112 | #link_to(caption, params) + (icon ? nbsp(2) + image_tag(icon) : '') |
113 | css_order_class = icon ? order : '' |
114 | link_to(caption, params, { :class => css_order_class }) |
115 | end |
116 | |
117 | # Returns a table header <th> tag with a sort link for the named column |
118 | # attribute. |
119 | # |
120 | # Options: |
121 | # :caption The displayed link name (defaults to titleized column name). |
122 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). |
123 | # |
124 | # Other options hash entries generate additional table header tag attributes. |
125 | # |
126 | # Example: |
127 | # |
128 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> |
129 | # |
130 | # Renders: |
131 | # |
132 | # <th title="Sort by contact ID" width="40"> |
133 | # <a href="/contact/list?sort_order=desc&sort_key=id">Id</a> |
134 | # <img alt="Sort_asc" src="/images/sort_asc.png" /> |
135 | # </th> |
136 | # |
137 | def sort_header_tag(column, options = {}) |
138 | if options[:caption] |
139 | caption = options[:caption] |
140 | options.delete(:caption) |
141 | else |
142 | caption = titleize(Inflector::humanize(column)) |
143 | end |
144 | options[:title]= "Sort by #{caption}" unless options[:title] |
145 | content_tag('th', sort_link(column, caption), options) |
146 | end |
147 | |
148 | private |
149 | |
150 | # Return n non-breaking spaces. |
151 | def nbsp(n) |
152 | ' ' * n |
153 | end |
154 | |
155 | # Return capitalized title. |
156 | def titleize(title) |
157 | title.split.map {|w| w.capitalize }.join(' ') |
158 | end |
159 | |
160 | end |