##// END OF EJS Templates
Merged r15890 (#24014)....
Jean-Philippe Lang -
r15511:6501e1451a43
parent child
Show More
@@ -1,141 +1,151
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 class TimeEntryQuery < Query
18 class TimeEntryQuery < Query
19
19
20 self.queried_class = TimeEntry
20 self.queried_class = TimeEntry
21
21
22 self.available_columns = [
22 self.available_columns = [
23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
25 QueryColumn.new(:tweek, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :caption => l(:label_week)),
25 QueryColumn.new(:tweek, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :caption => l(:label_week)),
26 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
26 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
27 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
27 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
28 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
28 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
29 QueryColumn.new(:comments),
29 QueryColumn.new(:comments),
30 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
30 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
31 ]
31 ]
32
32
33 def initialize(attributes=nil, *args)
33 def initialize(attributes=nil, *args)
34 super attributes
34 super attributes
35 self.filters ||= {}
35 self.filters ||= {}
36 add_filter('spent_on', '*') unless filters.present?
36 add_filter('spent_on', '*') unless filters.present?
37 end
37 end
38
38
39 def initialize_available_filters
39 def initialize_available_filters
40 add_available_filter "spent_on", :type => :date_past
40 add_available_filter "spent_on", :type => :date_past
41
41
42 principals = []
42 principals = []
43 if project
43 if project
44 principals += project.principals.visible.sort
44 principals += project.principals.visible.sort
45 unless project.leaf?
45 unless project.leaf?
46 subprojects = project.descendants.visible.to_a
46 subprojects = project.descendants.visible.to_a
47 if subprojects.any?
47 if subprojects.any?
48 add_available_filter "subproject_id",
48 add_available_filter "subproject_id",
49 :type => :list_subprojects,
49 :type => :list_subprojects,
50 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
50 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
51 principals += Principal.member_of(subprojects).visible
51 principals += Principal.member_of(subprojects).visible
52 end
52 end
53 end
53 end
54 else
54 else
55 if all_projects.any?
55 if all_projects.any?
56 # members of visible projects
56 # members of visible projects
57 principals += Principal.member_of(all_projects).visible
57 principals += Principal.member_of(all_projects).visible
58 # project filter
58 # project filter
59 project_values = []
59 project_values = []
60 if User.current.logged? && User.current.memberships.any?
60 if User.current.logged? && User.current.memberships.any?
61 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
61 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
62 end
62 end
63 project_values += all_projects_values
63 project_values += all_projects_values
64 add_available_filter("project_id",
64 add_available_filter("project_id",
65 :type => :list, :values => project_values
65 :type => :list, :values => project_values
66 ) unless project_values.empty?
66 ) unless project_values.empty?
67 end
67 end
68 end
68 end
69 principals.uniq!
69 principals.uniq!
70 principals.sort!
70 principals.sort!
71 users = principals.select {|p| p.is_a?(User)}
71 users = principals.select {|p| p.is_a?(User)}
72
72
73 users_values = []
73 users_values = []
74 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
74 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
75 users_values += users.collect{|s| [s.name, s.id.to_s] }
75 users_values += users.collect{|s| [s.name, s.id.to_s] }
76 add_available_filter("user_id",
76 add_available_filter("user_id",
77 :type => :list_optional, :values => users_values
77 :type => :list_optional, :values => users_values
78 ) unless users_values.empty?
78 ) unless users_values.empty?
79
79
80 activities = (project ? project.activities : TimeEntryActivity.shared)
80 activities = (project ? project.activities : TimeEntryActivity.shared)
81 add_available_filter("activity_id",
81 add_available_filter("activity_id",
82 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
82 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
83 ) unless activities.empty?
83 ) unless activities.empty?
84
84
85 add_available_filter "comments", :type => :text
85 add_available_filter "comments", :type => :text
86 add_available_filter "hours", :type => :float
86 add_available_filter "hours", :type => :float
87
87
88 add_custom_fields_filters(TimeEntryCustomField)
88 add_custom_fields_filters(TimeEntryCustomField)
89 add_associations_custom_fields_filters :project, :issue, :user
89 add_associations_custom_fields_filters :project
90 add_custom_fields_filters(issue_custom_fields, :issue)
91 add_associations_custom_fields_filters :user
90 end
92 end
91
93
92 def available_columns
94 def available_columns
93 return @available_columns if @available_columns
95 return @available_columns if @available_columns
94 @available_columns = self.class.available_columns.dup
96 @available_columns = self.class.available_columns.dup
95 @available_columns += TimeEntryCustomField.visible.
97 @available_columns += TimeEntryCustomField.visible.
96 map {|cf| QueryCustomFieldColumn.new(cf) }
98 map {|cf| QueryCustomFieldColumn.new(cf) }
97 @available_columns += IssueCustomField.visible.
99 @available_columns += issue_custom_fields.visible.
98 map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
100 map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
99 @available_columns
101 @available_columns
100 end
102 end
101
103
102 def default_columns_names
104 def default_columns_names
103 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
105 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
104 end
106 end
105
107
106 def results_scope(options={})
108 def results_scope(options={})
107 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
109 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
108
110
109 TimeEntry.visible.
111 TimeEntry.visible.
110 where(statement).
112 where(statement).
111 order(order_option).
113 order(order_option).
112 joins(joins_for_order_statement(order_option.join(','))).
114 joins(joins_for_order_statement(order_option.join(','))).
113 includes(:activity).
115 includes(:activity).
114 references(:activity)
116 references(:activity)
115 end
117 end
116
118
117 def sql_for_activity_id_field(field, operator, value)
119 def sql_for_activity_id_field(field, operator, value)
118 condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id')
120 condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id')
119 condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id')
121 condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id')
120 ids = value.map(&:to_i).join(',')
122 ids = value.map(&:to_i).join(',')
121 table_name = Enumeration.table_name
123 table_name = Enumeration.table_name
122 if operator == '='
124 if operator == '='
123 "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))"
125 "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))"
124 else
126 else
125 "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))"
127 "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))"
126 end
128 end
127 end
129 end
128
130
129 # Accepts :from/:to params as shortcut filters
131 # Accepts :from/:to params as shortcut filters
130 def build_from_params(params)
132 def build_from_params(params)
131 super
133 super
132 if params[:from].present? && params[:to].present?
134 if params[:from].present? && params[:to].present?
133 add_filter('spent_on', '><', [params[:from], params[:to]])
135 add_filter('spent_on', '><', [params[:from], params[:to]])
134 elsif params[:from].present?
136 elsif params[:from].present?
135 add_filter('spent_on', '>=', [params[:from]])
137 add_filter('spent_on', '>=', [params[:from]])
136 elsif params[:to].present?
138 elsif params[:to].present?
137 add_filter('spent_on', '<=', [params[:to]])
139 add_filter('spent_on', '<=', [params[:to]])
138 end
140 end
139 self
141 self
140 end
142 end
143
144 def issue_custom_fields
145 if project
146 project.all_issue_custom_fields
147 else
148 IssueCustomField.where(:is_for_all => true)
149 end
150 end
141 end
151 end
@@ -1,57 +1,81
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class TimeEntryQueryTest < ActiveSupport::TestCase
20 class TimeEntryQueryTest < ActiveSupport::TestCase
21 fixtures :issues, :projects, :users,
21 fixtures :issues, :projects, :users,
22 :members, :roles, :member_roles,
22 :members, :roles, :member_roles,
23 :trackers, :issue_statuses,
23 :trackers, :issue_statuses,
24 :projects_trackers,
24 :projects_trackers,
25 :journals, :journal_details,
25 :journals, :journal_details,
26 :issue_categories, :enumerations,
26 :issue_categories, :enumerations,
27 :groups_users,
27 :groups_users,
28 :enabled_modules
28 :enabled_modules
29
29
30 def test_cross_project_activity_filter_should_propose_non_active_activities
30 def test_cross_project_activity_filter_should_propose_non_active_activities
31 activity = TimeEntryActivity.create!(:name => 'Disabled', :active => false)
31 activity = TimeEntryActivity.create!(:name => 'Disabled', :active => false)
32 assert !activity.active?
32 assert !activity.active?
33
33
34 query = TimeEntryQuery.new(:name => '_')
34 query = TimeEntryQuery.new(:name => '_')
35 assert options = query.available_filters['activity_id']
35 assert options = query.available_filters['activity_id']
36 assert values = options[:values]
36 assert values = options[:values]
37 assert_include ["Disabled", activity.id.to_s], values
37 assert_include ["Disabled", activity.id.to_s], values
38 end
38 end
39
39
40 def test_activity_filter_should_consider_system_and_project_activities
40 def test_activity_filter_should_consider_system_and_project_activities
41 TimeEntry.delete_all
41 TimeEntry.delete_all
42 system = TimeEntryActivity.create!(:name => 'Foo')
42 system = TimeEntryActivity.create!(:name => 'Foo')
43 TimeEntry.generate!(:activity => system, :hours => 1.0)
43 TimeEntry.generate!(:activity => system, :hours => 1.0)
44 override = TimeEntryActivity.create!(:name => 'Foo', :parent_id => system.id, :project_id => 1)
44 override = TimeEntryActivity.create!(:name => 'Foo', :parent_id => system.id, :project_id => 1)
45 other = TimeEntryActivity.create!(:name => 'Bar')
45 other = TimeEntryActivity.create!(:name => 'Bar')
46 TimeEntry.generate!(:activity => override, :hours => 2.0)
46 TimeEntry.generate!(:activity => override, :hours => 2.0)
47 TimeEntry.generate!(:activity => other, :hours => 4.0)
47 TimeEntry.generate!(:activity => other, :hours => 4.0)
48
48
49 query = TimeEntryQuery.new(:name => '_')
49 query = TimeEntryQuery.new(:name => '_')
50 query.add_filter('activity_id', '=', [system.id.to_s])
50 query.add_filter('activity_id', '=', [system.id.to_s])
51 assert_equal 3.0, query.results_scope.sum(:hours)
51 assert_equal 3.0, query.results_scope.sum(:hours)
52
52
53 query = TimeEntryQuery.new(:name => '_')
53 query = TimeEntryQuery.new(:name => '_')
54 query.add_filter('activity_id', '!', [system.id.to_s])
54 query.add_filter('activity_id', '!', [system.id.to_s])
55 assert_equal 4.0, query.results_scope.sum(:hours)
55 assert_equal 4.0, query.results_scope.sum(:hours)
56 end
56 end
57
58 def test_project_query_should_include_project_issue_custom_fields_only_as_filters
59 global = IssueCustomField.generate!(:is_for_all => true, :is_filter => true)
60 field_on_project = IssueCustomField.generate!(:is_for_all => false, :project_ids => [3], :is_filter => true)
61 field_not_on_project = IssueCustomField.generate!(:is_for_all => false, :project_ids => [1,2], :is_filter => true)
62
63 query = TimeEntryQuery.new(:project => Project.find(3))
64
65 assert_include "issue.cf_#{global.id}", query.available_filters.keys
66 assert_include "issue.cf_#{field_on_project.id}", query.available_filters.keys
67 assert_not_include "issue.cf_#{field_not_on_project.id}", query.available_filters.keys
68 end
69
70 def test_project_query_should_include_project_issue_custom_fields_only_as_columns
71 global = IssueCustomField.generate!(:is_for_all => true, :is_filter => true)
72 field_on_project = IssueCustomField.generate!(:is_for_all => false, :project_ids => [3], :is_filter => true)
73 field_not_on_project = IssueCustomField.generate!(:is_for_all => false, :project_ids => [1,2], :is_filter => true)
74
75 query = TimeEntryQuery.new(:project => Project.find(3))
76
77 assert_include "issue.cf_#{global.id}", query.available_columns.map(&:name).map(&:to_s)
78 assert_include "issue.cf_#{field_on_project.id}", query.available_columns.map(&:name).map(&:to_s)
79 assert_not_include "issue.cf_#{field_not_on_project.id}", query.available_columns.map(&:name).map(&:to_s)
80 end
57 end
81 end
General Comments 0
You need to be logged in to leave comments. Login now