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