##// END OF EJS Templates
split QueriesHelper filters_options_for_select method and add new unit test (#12018)...
Toshi MARUYAMA -
r10339:b571c3dbc945
parent child
Show More
@@ -0,0 +1,41
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.expand_path('../../../test_helper', __FILE__)
19
20 class QueriesHelperTest < ActionView::TestCase
21 include QueriesHelper
22
23 fixtures :projects, :enabled_modules, :users, :members,
24 :member_roles, :roles, :trackers, :issue_statuses,
25 :issue_categories, :enumerations, :issues,
26 :watchers, :custom_fields, :custom_values, :versions,
27 :queries,
28 :projects_trackers,
29 :custom_fields_trackers
30
31 def test_order
32 User.current = User.find_by_login('admin')
33 query = Query.new(:project => nil, :name => '_')
34 assert_equal 30, query.available_filters.size
35 fo = filters_options(query)
36 assert_equal 31, fo.size
37 assert_equal [], fo[0]
38 assert_equal "tracker_id", fo[3][1]
39 assert_equal "priority_id", fo[4][1]
40 end
41 end
@@ -1,139 +1,142
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2012 Jean-Philippe Lang
4 # Copyright (C) 2006-2012 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 module QueriesHelper
20 module QueriesHelper
21 def filters_options_for_select(query)
21 def filters_options_for_select(query)
22 options_for_select(filters_options(query))
23 end
24
25 def filters_options(query)
22 options = [[]]
26 options = [[]]
23 options += query.available_filters.sort {|a,b| a[1][:order] <=> b[1][:order]}.map do |field, field_options|
27 options += query.available_filters.sort {|a,b| a[1][:order] <=> b[1][:order]}.map do |field, field_options|
24 [field_options[:name], field]
28 [field_options[:name], field]
25 end
29 end
26 options_for_select(options)
27 end
30 end
28
31
29 def column_header(column)
32 def column_header(column)
30 column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption,
33 column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption,
31 :default_order => column.default_order) :
34 :default_order => column.default_order) :
32 content_tag('th', h(column.caption))
35 content_tag('th', h(column.caption))
33 end
36 end
34
37
35 def column_content(column, issue)
38 def column_content(column, issue)
36 value = column.value(issue)
39 value = column.value(issue)
37 if value.is_a?(Array)
40 if value.is_a?(Array)
38 value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe
41 value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe
39 else
42 else
40 column_value(column, issue, value)
43 column_value(column, issue, value)
41 end
44 end
42 end
45 end
43
46
44 def column_value(column, issue, value)
47 def column_value(column, issue, value)
45 case value.class.name
48 case value.class.name
46 when 'String'
49 when 'String'
47 if column.name == :subject
50 if column.name == :subject
48 link_to(h(value), :controller => 'issues', :action => 'show', :id => issue)
51 link_to(h(value), :controller => 'issues', :action => 'show', :id => issue)
49 else
52 else
50 h(value)
53 h(value)
51 end
54 end
52 when 'Time'
55 when 'Time'
53 format_time(value)
56 format_time(value)
54 when 'Date'
57 when 'Date'
55 format_date(value)
58 format_date(value)
56 when 'Fixnum', 'Float'
59 when 'Fixnum', 'Float'
57 if column.name == :done_ratio
60 if column.name == :done_ratio
58 progress_bar(value, :width => '80px')
61 progress_bar(value, :width => '80px')
59 elsif column.name == :spent_hours
62 elsif column.name == :spent_hours
60 sprintf "%.2f", value
63 sprintf "%.2f", value
61 else
64 else
62 h(value.to_s)
65 h(value.to_s)
63 end
66 end
64 when 'User'
67 when 'User'
65 link_to_user value
68 link_to_user value
66 when 'Project'
69 when 'Project'
67 link_to_project value
70 link_to_project value
68 when 'Version'
71 when 'Version'
69 link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
72 link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
70 when 'TrueClass'
73 when 'TrueClass'
71 l(:general_text_Yes)
74 l(:general_text_Yes)
72 when 'FalseClass'
75 when 'FalseClass'
73 l(:general_text_No)
76 l(:general_text_No)
74 when 'Issue'
77 when 'Issue'
75 link_to_issue(value, :subject => false)
78 link_to_issue(value, :subject => false)
76 when 'IssueRelation'
79 when 'IssueRelation'
77 other = value.other_issue(issue)
80 other = value.other_issue(issue)
78 content_tag('span',
81 content_tag('span',
79 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe,
82 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe,
80 :class => value.css_classes_for(issue))
83 :class => value.css_classes_for(issue))
81 else
84 else
82 h(value)
85 h(value)
83 end
86 end
84 end
87 end
85
88
86 # Retrieve query from session or build a new query
89 # Retrieve query from session or build a new query
87 def retrieve_query
90 def retrieve_query
88 if !params[:query_id].blank?
91 if !params[:query_id].blank?
89 cond = "project_id IS NULL"
92 cond = "project_id IS NULL"
90 cond << " OR project_id = #{@project.id}" if @project
93 cond << " OR project_id = #{@project.id}" if @project
91 @query = Query.find(params[:query_id], :conditions => cond)
94 @query = Query.find(params[:query_id], :conditions => cond)
92 raise ::Unauthorized unless @query.visible?
95 raise ::Unauthorized unless @query.visible?
93 @query.project = @project
96 @query.project = @project
94 session[:query] = {:id => @query.id, :project_id => @query.project_id}
97 session[:query] = {:id => @query.id, :project_id => @query.project_id}
95 sort_clear
98 sort_clear
96 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
99 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
97 # Give it a name, required to be valid
100 # Give it a name, required to be valid
98 @query = Query.new(:name => "_")
101 @query = Query.new(:name => "_")
99 @query.project = @project
102 @query.project = @project
100 build_query_from_params
103 build_query_from_params
101 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
104 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
102 else
105 else
103 # retrieve from session
106 # retrieve from session
104 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
107 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
105 @query ||= Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
108 @query ||= Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
106 @query.project = @project
109 @query.project = @project
107 end
110 end
108 end
111 end
109
112
110 def retrieve_query_from_session
113 def retrieve_query_from_session
111 if session[:query]
114 if session[:query]
112 if session[:query][:id]
115 if session[:query][:id]
113 @query = Query.find_by_id(session[:query][:id])
116 @query = Query.find_by_id(session[:query][:id])
114 return unless @query
117 return unless @query
115 else
118 else
116 @query = Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
119 @query = Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
117 end
120 end
118 if session[:query].has_key?(:project_id)
121 if session[:query].has_key?(:project_id)
119 @query.project_id = session[:query][:project_id]
122 @query.project_id = session[:query][:project_id]
120 else
123 else
121 @query.project = @project
124 @query.project = @project
122 end
125 end
123 @query
126 @query
124 end
127 end
125 end
128 end
126
129
127 def build_query_from_params
130 def build_query_from_params
128 if params[:fields] || params[:f]
131 if params[:fields] || params[:f]
129 @query.filters = {}
132 @query.filters = {}
130 @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
133 @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
131 else
134 else
132 @query.available_filters.keys.each do |field|
135 @query.available_filters.keys.each do |field|
133 @query.add_short_filter(field, params[field]) if params[field]
136 @query.add_short_filter(field, params[field]) if params[field]
134 end
137 end
135 end
138 end
136 @query.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
139 @query.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
137 @query.column_names = params[:c] || (params[:query] && params[:query][:column_names])
140 @query.column_names = params[:c] || (params[:query] && params[:query][:column_names])
138 end
141 end
139 end
142 end
General Comments 0
You need to be logged in to leave comments. Login now