@@ -1,230 +1,230 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | module ApplicationHelper |
|
18 | module ApplicationHelper | |
19 |
|
19 | |||
20 | # Return current logged in user or nil |
|
20 | # Return current logged in user or nil | |
21 | def loggedin? |
|
21 | def loggedin? | |
22 | @logged_in_user |
|
22 | @logged_in_user | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | # Return true if user is logged in and is admin, otherwise false |
|
25 | # Return true if user is logged in and is admin, otherwise false | |
26 | def admin_loggedin? |
|
26 | def admin_loggedin? | |
27 | @logged_in_user and @logged_in_user.admin? |
|
27 | @logged_in_user and @logged_in_user.admin? | |
28 | end |
|
28 | end | |
29 |
|
29 | |||
30 | # Return true if user is authorized for controller/action, otherwise false |
|
30 | # Return true if user is authorized for controller/action, otherwise false | |
31 | def authorize_for(controller, action) |
|
31 | def authorize_for(controller, action) | |
32 | # check if action is allowed on public projects |
|
32 | # check if action is allowed on public projects | |
33 | if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ] |
|
33 | if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ] | |
34 | return true |
|
34 | return true | |
35 | end |
|
35 | end | |
36 | # check if user is authorized |
|
36 | # check if user is authorized | |
37 | if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project.id) ) ) |
|
37 | if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project.id) ) ) | |
38 | return true |
|
38 | return true | |
39 | end |
|
39 | end | |
40 | return false |
|
40 | return false | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | # Display a link if user is authorized |
|
43 | # Display a link if user is authorized | |
44 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
44 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) | |
45 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller], options[:action]) |
|
45 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller], options[:action]) | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | # Display a link to user's account page |
|
48 | # Display a link to user's account page | |
49 | def link_to_user(user) |
|
49 | def link_to_user(user) | |
50 | link_to user.display_name, :controller => 'account', :action => 'show', :id => user |
|
50 | link_to user.display_name, :controller => 'account', :action => 'show', :id => user | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | def image_to_function(name, function, html_options = {}) |
|
53 | def image_to_function(name, function, html_options = {}) | |
54 | html_options.symbolize_keys! |
|
54 | html_options.symbolize_keys! | |
55 | tag(:input, html_options.merge({ |
|
55 | tag(:input, html_options.merge({ | |
56 | :type => "image", :src => image_path(name), |
|
56 | :type => "image", :src => image_path(name), | |
57 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" |
|
57 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" | |
58 | })) |
|
58 | })) | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def format_date(date) |
|
61 | def format_date(date) | |
62 | l_date(date) if date |
|
62 | l_date(date) if date | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def format_time(time) |
|
65 | def format_time(time) | |
66 | l_datetime((time.is_a? String) ? time.to_time : time) if time |
|
66 | l_datetime((time.is_a? String) ? time.to_time : time) if time | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def day_name(day) |
|
69 | def day_name(day) | |
70 | l(:general_day_names).split(',')[day-1] |
|
70 | l(:general_day_names).split(',')[day-1] | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | def month_name(month) |
|
73 | def month_name(month) | |
74 | l(:actionview_datehelper_select_month_names).split(',')[month-1] |
|
74 | l(:actionview_datehelper_select_month_names).split(',')[month-1] | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def pagination_links_full(paginator, options={}, html_options={}) |
|
77 | def pagination_links_full(paginator, options={}, html_options={}) | |
78 | html = '' |
|
78 | html = '' | |
79 | html << link_to_remote(('« ' + l(:label_previous)), |
|
79 | html << link_to_remote(('« ' + l(:label_previous)), | |
80 |
{:update => "content", :url => |
|
80 | {:update => "content", :url => { :page => paginator.current.previous }}, | |
81 |
{:href => url_for(:action => 'list', :params => |
|
81 | {:href => url_for(:action => 'list', :params =>{:page => paginator.current.previous})}) + ' ' if paginator.current.previous | |
82 |
|
82 | |||
83 | html << (pagination_links_each(paginator, options) do |n| |
|
83 | html << (pagination_links_each(paginator, options) do |n| | |
84 | link_to_remote(n.to_s, |
|
84 | link_to_remote(n.to_s, | |
85 |
{:url => {:action => 'list', :params => |
|
85 | {:url => {:action => 'list', :params => {:page => n}}, :update => 'content'}, | |
86 |
{:href => url_for(:action => 'list', :params => |
|
86 | {:href => url_for(:action => 'list', :params => {:page => n})}) | |
87 | end || '') |
|
87 | end || '') | |
88 |
|
88 | |||
89 | html << ' ' + link_to_remote((l(:label_next) + ' »'), |
|
89 | html << ' ' + link_to_remote((l(:label_next) + ' »'), | |
90 |
{:update => "content", :url => |
|
90 | {:update => "content", :url => { :page => paginator.current.next }}, | |
91 |
{:href => url_for(:action => 'list', :params => |
|
91 | {:href => url_for(:action => 'list', :params => {:page => paginator.current.next})}) if paginator.current.next | |
92 | html |
|
92 | html | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | # textilize text according to system settings and RedCloth availability |
|
95 | # textilize text according to system settings and RedCloth availability | |
96 | def textilizable(text, options = {}) |
|
96 | def textilizable(text, options = {}) | |
97 | # different methods for formatting wiki links |
|
97 | # different methods for formatting wiki links | |
98 | case options[:wiki_links] |
|
98 | case options[:wiki_links] | |
99 | when :local |
|
99 | when :local | |
100 | # used for local links to html files |
|
100 | # used for local links to html files | |
101 | format_wiki_link = Proc.new {|title| "#{title}.html" } |
|
101 | format_wiki_link = Proc.new {|title| "#{title}.html" } | |
102 | when :anchor |
|
102 | when :anchor | |
103 | # used for single-file wiki export |
|
103 | # used for single-file wiki export | |
104 | format_wiki_link = Proc.new {|title| "##{title}" } |
|
104 | format_wiki_link = Proc.new {|title| "##{title}" } | |
105 | else |
|
105 | else | |
106 | if @project |
|
106 | if @project | |
107 | format_wiki_link = Proc.new {|title| url_for :controller => 'wiki', :action => 'index', :id => @project, :page => title } |
|
107 | format_wiki_link = Proc.new {|title| url_for :controller => 'wiki', :action => 'index', :id => @project, :page => title } | |
108 | else |
|
108 | else | |
109 | format_wiki_link = Proc.new {|title| title } |
|
109 | format_wiki_link = Proc.new {|title| title } | |
110 | end |
|
110 | end | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
113 | # turn wiki links into textile links: |
|
113 | # turn wiki links into textile links: | |
114 | # example: |
|
114 | # example: | |
115 | # [[link]] -> "link":link |
|
115 | # [[link]] -> "link":link | |
116 | # [[link|title]] -> "title":link |
|
116 | # [[link|title]] -> "title":link | |
117 | text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) {|m| "\"#{$3 || $1}\":" + format_wiki_link.call(Wiki.titleize($1)) } |
|
117 | text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) {|m| "\"#{$3 || $1}\":" + format_wiki_link.call(Wiki.titleize($1)) } | |
118 |
|
118 | |||
119 | # turn issue ids to textile links |
|
119 | # turn issue ids to textile links | |
120 | # example: |
|
120 | # example: | |
121 | # #52 -> "#52":/issues/show/52 |
|
121 | # #52 -> "#52":/issues/show/52 | |
122 | text = text.gsub(/#(\d+)(?=\b)/) {|m| "\"##{$1}\":" + url_for(:controller => 'issues', :action => 'show', :id => $1) } |
|
122 | text = text.gsub(/#(\d+)(?=\b)/) {|m| "\"##{$1}\":" + url_for(:controller => 'issues', :action => 'show', :id => $1) } | |
123 |
|
123 | |||
124 | # turn revision ids to textile links (@project needed) |
|
124 | # turn revision ids to textile links (@project needed) | |
125 | # example: |
|
125 | # example: | |
126 | # r52 -> "r52":/repositories/revision/6?rev=52 (@project.id is 6) |
|
126 | # r52 -> "r52":/repositories/revision/6?rev=52 (@project.id is 6) | |
127 | text = text.gsub(/r(\d+)(?=\b)/) {|m| "\"r#{$1}\":" + url_for(:controller => 'repositories', :action => 'revision', :id => @project.id, :rev => $1) } if @project |
|
127 | text = text.gsub(/r(\d+)(?=\b)/) {|m| "\"r#{$1}\":" + url_for(:controller => 'repositories', :action => 'revision', :id => @project.id, :rev => $1) } if @project | |
128 |
|
128 | |||
129 | # finally textilize text |
|
129 | # finally textilize text | |
130 | @do_textilize ||= (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize") |
|
130 | @do_textilize ||= (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize") | |
131 | text = @do_textilize ? auto_link(RedCloth.new(text).to_html) : simple_format(auto_link(h(text))) |
|
131 | text = @do_textilize ? auto_link(RedCloth.new(text).to_html) : simple_format(auto_link(h(text))) | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | def error_messages_for(object_name, options = {}) |
|
134 | def error_messages_for(object_name, options = {}) | |
135 | options = options.symbolize_keys |
|
135 | options = options.symbolize_keys | |
136 | object = instance_variable_get("@#{object_name}") |
|
136 | object = instance_variable_get("@#{object_name}") | |
137 | if object && !object.errors.empty? |
|
137 | if object && !object.errors.empty? | |
138 | # build full_messages here with controller current language |
|
138 | # build full_messages here with controller current language | |
139 | full_messages = [] |
|
139 | full_messages = [] | |
140 | object.errors.each do |attr, msg| |
|
140 | object.errors.each do |attr, msg| | |
141 | next if msg.nil? |
|
141 | next if msg.nil? | |
142 | msg = msg.first if msg.is_a? Array |
|
142 | msg = msg.first if msg.is_a? Array | |
143 | if attr == "base" |
|
143 | if attr == "base" | |
144 | full_messages << l(msg) |
|
144 | full_messages << l(msg) | |
145 | else |
|
145 | else | |
146 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" |
|
146 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" | |
147 | end |
|
147 | end | |
148 | end |
|
148 | end | |
149 | # retrieve custom values error messages |
|
149 | # retrieve custom values error messages | |
150 | if object.errors[:custom_values] |
|
150 | if object.errors[:custom_values] | |
151 | object.custom_values.each do |v| |
|
151 | object.custom_values.each do |v| | |
152 | v.errors.each do |attr, msg| |
|
152 | v.errors.each do |attr, msg| | |
153 | next if msg.nil? |
|
153 | next if msg.nil? | |
154 | msg = msg.first if msg.is_a? Array |
|
154 | msg = msg.first if msg.is_a? Array | |
155 | full_messages << "« " + v.custom_field.name + " » " + l(msg) |
|
155 | full_messages << "« " + v.custom_field.name + " » " + l(msg) | |
156 | end |
|
156 | end | |
157 | end |
|
157 | end | |
158 | end |
|
158 | end | |
159 | content_tag("div", |
|
159 | content_tag("div", | |
160 | content_tag( |
|
160 | content_tag( | |
161 | options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :" |
|
161 | options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :" | |
162 | ) + |
|
162 | ) + | |
163 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), |
|
163 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), | |
164 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" |
|
164 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" | |
165 | ) |
|
165 | ) | |
166 | else |
|
166 | else | |
167 | "" |
|
167 | "" | |
168 | end |
|
168 | end | |
169 | end |
|
169 | end | |
170 |
|
170 | |||
171 | def lang_options_for_select(blank=true) |
|
171 | def lang_options_for_select(blank=true) | |
172 | (blank ? [["(auto)", ""]] : []) + |
|
172 | (blank ? [["(auto)", ""]] : []) + | |
173 | (GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]} |
|
173 | (GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]} | |
174 | end |
|
174 | end | |
175 |
|
175 | |||
176 | def label_tag_for(name, option_tags = nil, options = {}) |
|
176 | def label_tag_for(name, option_tags = nil, options = {}) | |
177 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
177 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
178 | content_tag("label", label_text) |
|
178 | content_tag("label", label_text) | |
179 | end |
|
179 | end | |
180 |
|
180 | |||
181 | def labelled_tabular_form_for(name, object, options, &proc) |
|
181 | def labelled_tabular_form_for(name, object, options, &proc) | |
182 | options[:html] ||= {} |
|
182 | options[:html] ||= {} | |
183 | options[:html].store :class, "tabular" |
|
183 | options[:html].store :class, "tabular" | |
184 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) |
|
184 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) | |
185 | end |
|
185 | end | |
186 |
|
186 | |||
187 | def check_all_links(form_name) |
|
187 | def check_all_links(form_name) | |
188 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + |
|
188 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + | |
189 | " | " + |
|
189 | " | " + | |
190 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") |
|
190 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") | |
191 | end |
|
191 | end | |
192 |
|
192 | |||
193 | def calendar_for(field_id) |
|
193 | def calendar_for(field_id) | |
194 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + |
|
194 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + | |
195 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") |
|
195 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") | |
196 | end |
|
196 | end | |
197 | end |
|
197 | end | |
198 |
|
198 | |||
199 | class TabularFormBuilder < ActionView::Helpers::FormBuilder |
|
199 | class TabularFormBuilder < ActionView::Helpers::FormBuilder | |
200 | include GLoc |
|
200 | include GLoc | |
201 |
|
201 | |||
202 | def initialize(object_name, object, template, options, proc) |
|
202 | def initialize(object_name, object, template, options, proc) | |
203 | set_language_if_valid options.delete(:lang) |
|
203 | set_language_if_valid options.delete(:lang) | |
204 | @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc |
|
204 | @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc | |
205 | end |
|
205 | end | |
206 |
|
206 | |||
207 | (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector| |
|
207 | (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector| | |
208 | src = <<-END_SRC |
|
208 | src = <<-END_SRC | |
209 | def #{selector}(field, options = {}) |
|
209 | def #{selector}(field, options = {}) | |
210 | return super if options.delete :no_label |
|
210 | return super if options.delete :no_label | |
211 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
211 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
212 | label = @template.content_tag("label", label_text, |
|
212 | label = @template.content_tag("label", label_text, | |
213 | :class => (@object && @object.errors[field] ? "error" : nil), |
|
213 | :class => (@object && @object.errors[field] ? "error" : nil), | |
214 | :for => (@object_name.to_s + "_" + field.to_s)) |
|
214 | :for => (@object_name.to_s + "_" + field.to_s)) | |
215 | label + super |
|
215 | label + super | |
216 | end |
|
216 | end | |
217 | END_SRC |
|
217 | END_SRC | |
218 | class_eval src, __FILE__, __LINE__ |
|
218 | class_eval src, __FILE__, __LINE__ | |
219 | end |
|
219 | end | |
220 |
|
220 | |||
221 | def select(field, choices, options = {}, html_options = {}) |
|
221 | def select(field, choices, options = {}, html_options = {}) | |
222 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
222 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
223 | label = @template.content_tag("label", label_text, |
|
223 | label = @template.content_tag("label", label_text, | |
224 | :class => (@object && @object.errors[field] ? "error" : nil), |
|
224 | :class => (@object && @object.errors[field] ? "error" : nil), | |
225 | :for => (@object_name.to_s + "_" + field.to_s)) |
|
225 | :for => (@object_name.to_s + "_" + field.to_s)) | |
226 | label + super |
|
226 | label + super | |
227 | end |
|
227 | end | |
228 |
|
228 | |||
229 | end |
|
229 | end | |
230 |
|
230 |
@@ -1,160 +1,162 | |||||
1 | # Helpers to sort tables using clickable column headers. |
|
1 | # Helpers to sort tables using clickable column headers. | |
2 | # |
|
2 | # | |
3 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. |
|
3 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. | |
4 | # License: This source code is released under the MIT license. |
|
4 | # License: This source code is released under the MIT license. | |
5 | # |
|
5 | # | |
6 | # - Consecutive clicks toggle the column's sort order. |
|
6 | # - Consecutive clicks toggle the column's sort order. | |
7 | # - Sort state is maintained by a session hash entry. |
|
7 | # - Sort state is maintained by a session hash entry. | |
8 | # - Icon image identifies sort column and state. |
|
8 | # - Icon image identifies sort column and state. | |
9 | # - Typically used in conjunction with the Pagination module. |
|
9 | # - Typically used in conjunction with the Pagination module. | |
10 | # |
|
10 | # | |
11 | # Example code snippets: |
|
11 | # Example code snippets: | |
12 | # |
|
12 | # | |
13 | # Controller: |
|
13 | # Controller: | |
14 | # |
|
14 | # | |
15 | # helper :sort |
|
15 | # helper :sort | |
16 | # include SortHelper |
|
16 | # include SortHelper | |
17 | # |
|
17 | # | |
18 | # def list |
|
18 | # def list | |
19 | # sort_init 'last_name' |
|
19 | # sort_init 'last_name' | |
20 | # sort_update |
|
20 | # sort_update | |
21 | # @items = Contact.find_all nil, sort_clause |
|
21 | # @items = Contact.find_all nil, sort_clause | |
22 | # end |
|
22 | # end | |
23 | # |
|
23 | # | |
24 | # Controller (using Pagination module): |
|
24 | # Controller (using Pagination module): | |
25 | # |
|
25 | # | |
26 | # helper :sort |
|
26 | # helper :sort | |
27 | # include SortHelper |
|
27 | # include SortHelper | |
28 | # |
|
28 | # | |
29 | # def list |
|
29 | # def list | |
30 | # sort_init 'last_name' |
|
30 | # sort_init 'last_name' | |
31 | # sort_update |
|
31 | # sort_update | |
32 | # @contact_pages, @items = paginate :contacts, |
|
32 | # @contact_pages, @items = paginate :contacts, | |
33 | # :order_by => sort_clause, |
|
33 | # :order_by => sort_clause, | |
34 | # :per_page => 10 |
|
34 | # :per_page => 10 | |
35 | # end |
|
35 | # end | |
36 | # |
|
36 | # | |
37 | # View (table header in list.rhtml): |
|
37 | # View (table header in list.rhtml): | |
38 | # |
|
38 | # | |
39 | # <thead> |
|
39 | # <thead> | |
40 | # <tr> |
|
40 | # <tr> | |
41 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> |
|
41 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> | |
42 | # <%= sort_header_tag('last_name', :caption => 'Name') %> |
|
42 | # <%= sort_header_tag('last_name', :caption => 'Name') %> | |
43 | # <%= sort_header_tag('phone') %> |
|
43 | # <%= sort_header_tag('phone') %> | |
44 | # <%= sort_header_tag('address', :width => 200) %> |
|
44 | # <%= sort_header_tag('address', :width => 200) %> | |
45 | # </tr> |
|
45 | # </tr> | |
46 | # </thead> |
|
46 | # </thead> | |
47 | # |
|
47 | # | |
48 | # - The ascending and descending sort icon images are sort_asc.png and |
|
48 | # - The ascending and descending sort icon images are sort_asc.png and | |
49 | # sort_desc.png and reside in the application's images directory. |
|
49 | # sort_desc.png and reside in the application's images directory. | |
50 | # - Introduces instance variables: @sort_name, @sort_default. |
|
50 | # - Introduces instance variables: @sort_name, @sort_default. | |
51 | # - Introduces params :sort_key and :sort_order. |
|
51 | # - Introduces params :sort_key and :sort_order. | |
52 | # |
|
52 | # | |
53 | module SortHelper |
|
53 | module SortHelper | |
54 |
|
54 | |||
55 | # Initializes the default sort column (default_key) and sort order |
|
55 | # Initializes the default sort column (default_key) and sort order | |
56 | # (default_order). |
|
56 | # (default_order). | |
57 | # |
|
57 | # | |
58 | # - default_key is a column attribute name. |
|
58 | # - default_key is a column attribute name. | |
59 | # - default_order is 'asc' or 'desc'. |
|
59 | # - default_order is 'asc' or 'desc'. | |
60 | # - name is the name of the session hash entry that stores the sort state, |
|
60 | # - name is the name of the session hash entry that stores the sort state, | |
61 | # defaults to '<controller_name>_sort'. |
|
61 | # defaults to '<controller_name>_sort'. | |
62 | # |
|
62 | # | |
63 | def sort_init(default_key, default_order='asc', name=nil) |
|
63 | def sort_init(default_key, default_order='asc', name=nil) | |
64 | @sort_name = name || params[:controller] + params[:action] + '_sort' |
|
64 | @sort_name = name || params[:controller] + params[:action] + '_sort' | |
65 | @sort_default = {:key => default_key, :order => default_order} |
|
65 | @sort_default = {:key => default_key, :order => default_order} | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | # Updates the sort state. Call this in the controller prior to calling |
|
68 | # Updates the sort state. Call this in the controller prior to calling | |
69 | # sort_clause. |
|
69 | # sort_clause. | |
70 | # |
|
70 | # | |
71 | def sort_update() |
|
71 | def sort_update() | |
72 | if params[:sort_key] |
|
72 | if params[:sort_key] | |
73 | sort = {:key => params[:sort_key], :order => params[:sort_order]} |
|
73 | sort = {:key => params[:sort_key], :order => params[:sort_order]} | |
74 | elsif session[@sort_name] |
|
74 | elsif session[@sort_name] | |
75 | sort = session[@sort_name] # Previous sort. |
|
75 | sort = session[@sort_name] # Previous sort. | |
76 | else |
|
76 | else | |
77 | sort = @sort_default |
|
77 | sort = @sort_default | |
78 | end |
|
78 | end | |
79 | session[@sort_name] = sort |
|
79 | session[@sort_name] = sort | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | # Returns an SQL sort clause corresponding to the current sort state. |
|
82 | # Returns an SQL sort clause corresponding to the current sort state. | |
83 | # Use this to sort the controller's table items collection. |
|
83 | # Use this to sort the controller's table items collection. | |
84 | # |
|
84 | # | |
85 | def sort_clause() |
|
85 | def sort_clause() | |
86 | session[@sort_name][:key] + ' ' + session[@sort_name][:order] |
|
86 | session[@sort_name][:key] + ' ' + session[@sort_name][:order] | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | # Returns a link which sorts by the named column. |
|
89 | # Returns a link which sorts by the named column. | |
90 | # |
|
90 | # | |
91 | # - column is the name of an attribute in the sorted record collection. |
|
91 | # - column is the name of an attribute in the sorted record collection. | |
92 | # - The optional caption explicitly specifies the displayed link text. |
|
92 | # - The optional caption explicitly specifies the displayed link text. | |
93 | # - A sort icon image is positioned to the right of the sort link. |
|
93 | # - A sort icon image is positioned to the right of the sort link. | |
94 | # |
|
94 | # | |
95 | def sort_link(column, caption=nil) |
|
95 | def sort_link(column, caption=nil) | |
96 | key, order = session[@sort_name][:key], session[@sort_name][:order] |
|
96 | key, order = session[@sort_name][:key], session[@sort_name][:order] | |
97 | if key == column |
|
97 | if key == column | |
98 | if order.downcase == 'asc' |
|
98 | if order.downcase == 'asc' | |
99 | icon = 'sort_asc.png' |
|
99 | icon = 'sort_asc.png' | |
100 | order = 'desc' |
|
100 | order = 'desc' | |
101 | else |
|
101 | else | |
102 | icon = 'sort_desc.png' |
|
102 | icon = 'sort_desc.png' | |
103 | order = 'asc' |
|
103 | order = 'asc' | |
104 | end |
|
104 | end | |
105 | else |
|
105 | else | |
106 | icon = nil |
|
106 | icon = nil | |
107 | order = 'desc' # changed for desc order by default |
|
107 | order = 'desc' # changed for desc order by default | |
108 | end |
|
108 | end | |
109 | caption = titleize(Inflector::humanize(column)) unless caption |
|
109 | caption = titleize(Inflector::humanize(column)) unless caption | |
110 | #params = {:params => {:sort_key => column, :sort_order => order}} |
|
110 | ||
|
111 | url = {:sort_key => column, :sort_order => order, :issue_id => params[:issue_id], :project_id => params[:project_id]} | |||
|
112 | ||||
111 | link_to_remote(caption, |
|
113 | link_to_remote(caption, | |
112 |
{:update => "content", :url => |
|
114 | {:update => "content", :url => url}, | |
113 | {:href => url_for(:params => params.update(:sort_key => column, :sort_order => order))}) + |
|
115 | {:href => url_for(url)}) + | |
114 | (icon ? nbsp(2) + image_tag(icon) : '') |
|
116 | (icon ? nbsp(2) + image_tag(icon) : '') | |
115 | end |
|
117 | end | |
116 |
|
118 | |||
117 | # Returns a table header <th> tag with a sort link for the named column |
|
119 | # Returns a table header <th> tag with a sort link for the named column | |
118 | # attribute. |
|
120 | # attribute. | |
119 | # |
|
121 | # | |
120 | # Options: |
|
122 | # Options: | |
121 | # :caption The displayed link name (defaults to titleized column name). |
|
123 | # :caption The displayed link name (defaults to titleized column name). | |
122 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). |
|
124 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). | |
123 | # |
|
125 | # | |
124 | # Other options hash entries generate additional table header tag attributes. |
|
126 | # Other options hash entries generate additional table header tag attributes. | |
125 | # |
|
127 | # | |
126 | # Example: |
|
128 | # Example: | |
127 | # |
|
129 | # | |
128 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> |
|
130 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> | |
129 | # |
|
131 | # | |
130 | # Renders: |
|
132 | # Renders: | |
131 | # |
|
133 | # | |
132 | # <th title="Sort by contact ID" width="40"> |
|
134 | # <th title="Sort by contact ID" width="40"> | |
133 | # <a href="/contact/list?sort_order=desc&sort_key=id">Id</a> |
|
135 | # <a href="/contact/list?sort_order=desc&sort_key=id">Id</a> | |
134 | # <img alt="Sort_asc" src="/images/sort_asc.png" /> |
|
136 | # <img alt="Sort_asc" src="/images/sort_asc.png" /> | |
135 | # </th> |
|
137 | # </th> | |
136 | # |
|
138 | # | |
137 | def sort_header_tag(column, options = {}) |
|
139 | def sort_header_tag(column, options = {}) | |
138 | if options[:caption] |
|
140 | if options[:caption] | |
139 | caption = options[:caption] |
|
141 | caption = options[:caption] | |
140 | options.delete(:caption) |
|
142 | options.delete(:caption) | |
141 | else |
|
143 | else | |
142 | caption = titleize(Inflector::humanize(column)) |
|
144 | caption = titleize(Inflector::humanize(column)) | |
143 | end |
|
145 | end | |
144 | options[:title]= "Sort by #{caption}" unless options[:title] |
|
146 | options[:title]= "Sort by #{caption}" unless options[:title] | |
145 | content_tag('th', sort_link(column, caption), options) |
|
147 | content_tag('th', sort_link(column, caption), options) | |
146 | end |
|
148 | end | |
147 |
|
149 | |||
148 | private |
|
150 | private | |
149 |
|
151 | |||
150 | # Return n non-breaking spaces. |
|
152 | # Return n non-breaking spaces. | |
151 | def nbsp(n) |
|
153 | def nbsp(n) | |
152 | ' ' * n |
|
154 | ' ' * n | |
153 | end |
|
155 | end | |
154 |
|
156 | |||
155 | # Return capitalized title. |
|
157 | # Return capitalized title. | |
156 | def titleize(title) |
|
158 | def titleize(title) | |
157 | title.split.map {|w| w.capitalize }.join(' ') |
|
159 | title.split.map {|w| w.capitalize }.join(' ') | |
158 | end |
|
160 | end | |
159 |
|
161 | |||
160 | end |
|
162 | end |
General Comments 0
You need to be logged in to leave comments.
Login now