annotate app/models/time_entry_query.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class TimeEntryQuery < Query
Chris@1295 19
Chris@1295 20 self.queried_class = TimeEntry
Chris@1295 21
Chris@1295 22 self.available_columns = [
Chris@1295 23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
Chris@1295 24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
Chris@1295 25 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
Chris@1295 26 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
Chris@1295 27 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
Chris@1295 28 QueryColumn.new(:comments),
Chris@1295 29 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
Chris@1295 30 ]
Chris@1295 31
Chris@1295 32 def initialize(attributes=nil, *args)
Chris@1295 33 super attributes
Chris@1295 34 self.filters ||= {}
Chris@1295 35 add_filter('spent_on', '*') unless filters.present?
Chris@1295 36 end
Chris@1295 37
Chris@1295 38 def initialize_available_filters
Chris@1295 39 add_available_filter "spent_on", :type => :date_past
Chris@1295 40
Chris@1295 41 principals = []
Chris@1295 42 if project
Chris@1295 43 principals += project.principals.sort
Chris@1295 44 unless project.leaf?
Chris@1295 45 subprojects = project.descendants.visible.all
Chris@1295 46 if subprojects.any?
Chris@1295 47 add_available_filter "subproject_id",
Chris@1295 48 :type => :list_subprojects,
Chris@1295 49 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
Chris@1295 50 principals += Principal.member_of(subprojects)
Chris@1295 51 end
Chris@1295 52 end
Chris@1295 53 else
Chris@1295 54 if all_projects.any?
Chris@1295 55 # members of visible projects
Chris@1295 56 principals += Principal.member_of(all_projects)
Chris@1295 57 # project filter
Chris@1295 58 project_values = []
Chris@1295 59 if User.current.logged? && User.current.memberships.any?
Chris@1295 60 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
Chris@1295 61 end
Chris@1295 62 project_values += all_projects_values
Chris@1295 63 add_available_filter("project_id",
Chris@1295 64 :type => :list, :values => project_values
Chris@1295 65 ) unless project_values.empty?
Chris@1295 66 end
Chris@1295 67 end
Chris@1295 68 principals.uniq!
Chris@1295 69 principals.sort!
Chris@1295 70 users = principals.select {|p| p.is_a?(User)}
Chris@1295 71
Chris@1295 72 users_values = []
Chris@1295 73 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1295 74 users_values += users.collect{|s| [s.name, s.id.to_s] }
Chris@1295 75 add_available_filter("user_id",
Chris@1295 76 :type => :list_optional, :values => users_values
Chris@1295 77 ) unless users_values.empty?
Chris@1295 78
Chris@1295 79 activities = (project ? project.activities : TimeEntryActivity.shared.active)
Chris@1295 80 add_available_filter("activity_id",
Chris@1295 81 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
Chris@1295 82 ) unless activities.empty?
Chris@1295 83
Chris@1295 84 add_available_filter "comments", :type => :text
Chris@1295 85 add_available_filter "hours", :type => :float
Chris@1295 86
Chris@1295 87 add_custom_fields_filters(TimeEntryCustomField.where(:is_filter => true).all)
Chris@1295 88 add_associations_custom_fields_filters :project, :issue, :user
Chris@1295 89 end
Chris@1295 90
Chris@1295 91 def available_columns
Chris@1295 92 return @available_columns if @available_columns
Chris@1295 93 @available_columns = self.class.available_columns.dup
Chris@1295 94 @available_columns += TimeEntryCustomField.all.map {|cf| QueryCustomFieldColumn.new(cf) }
Chris@1295 95 @available_columns += IssueCustomField.all.map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
Chris@1295 96 @available_columns
Chris@1295 97 end
Chris@1295 98
Chris@1295 99 def default_columns_names
Chris@1295 100 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
Chris@1295 101 end
Chris@1295 102
Chris@1295 103 # Accepts :from/:to params as shortcut filters
Chris@1295 104 def build_from_params(params)
Chris@1295 105 super
Chris@1295 106 if params[:from].present? && params[:to].present?
Chris@1295 107 add_filter('spent_on', '><', [params[:from], params[:to]])
Chris@1295 108 elsif params[:from].present?
Chris@1295 109 add_filter('spent_on', '>=', [params[:from]])
Chris@1295 110 elsif params[:to].present?
Chris@1295 111 add_filter('spent_on', '<=', [params[:to]])
Chris@1295 112 end
Chris@1295 113 self
Chris@1295 114 end
Chris@1295 115 end