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