annotate .svn/pristine/4a/4ac586745b87f9cfbbc66aeda458b5ccce4557cc.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 class IssueQuery < Query
Chris@1494 19
Chris@1494 20 self.queried_class = Issue
Chris@1494 21
Chris@1494 22 self.available_columns = [
Chris@1494 23 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true),
Chris@1494 24 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
Chris@1494 25 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
Chris@1494 26 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
Chris@1494 27 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
Chris@1494 28 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
Chris@1494 29 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
Chris@1494 30 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
Chris@1494 31 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
Chris@1494 32 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
Chris@1494 33 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
Chris@1494 34 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
Chris@1494 35 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
Chris@1494 36 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
Chris@1494 37 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
Chris@1494 38 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
Chris@1494 39 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
Chris@1494 40 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'),
Chris@1494 41 QueryColumn.new(:relations, :caption => :label_related_issues),
Chris@1494 42 QueryColumn.new(:description, :inline => false)
Chris@1494 43 ]
Chris@1494 44
Chris@1494 45 scope :visible, lambda {|*args|
Chris@1494 46 user = args.shift || User.current
Chris@1494 47 base = Project.allowed_to_condition(user, :view_issues, *args)
Chris@1494 48 scope = includes(:project).where("#{table_name}.project_id IS NULL OR (#{base})")
Chris@1494 49
Chris@1494 50 if user.admin?
Chris@1494 51 scope.where("#{table_name}.visibility <> ? OR #{table_name}.user_id = ?", VISIBILITY_PRIVATE, user.id)
Chris@1494 52 elsif user.memberships.any?
Chris@1494 53 scope.where("#{table_name}.visibility = ?" +
Chris@1494 54 " OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" +
Chris@1494 55 "SELECT DISTINCT q.id FROM #{table_name} q" +
Chris@1494 56 " INNER JOIN #{table_name_prefix}queries_roles#{table_name_suffix} qr on qr.query_id = q.id" +
Chris@1494 57 " INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = qr.role_id" +
Chris@1494 58 " INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" +
Chris@1494 59 " WHERE q.project_id IS NULL OR q.project_id = m.project_id))" +
Chris@1494 60 " OR #{table_name}.user_id = ?",
Chris@1494 61 VISIBILITY_PUBLIC, VISIBILITY_ROLES, user.id, user.id)
Chris@1494 62 elsif user.logged?
Chris@1494 63 scope.where("#{table_name}.visibility = ? OR #{table_name}.user_id = ?", VISIBILITY_PUBLIC, user.id)
Chris@1494 64 else
Chris@1494 65 scope.where("#{table_name}.visibility = ?", VISIBILITY_PUBLIC)
Chris@1494 66 end
Chris@1494 67 }
Chris@1494 68
Chris@1494 69 def initialize(attributes=nil, *args)
Chris@1494 70 super attributes
Chris@1494 71 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
Chris@1494 72 end
Chris@1494 73
Chris@1494 74 # Returns true if the query is visible to +user+ or the current user.
Chris@1494 75 def visible?(user=User.current)
Chris@1494 76 return true if user.admin?
Chris@1494 77 return false unless project.nil? || user.allowed_to?(:view_issues, project)
Chris@1494 78 case visibility
Chris@1494 79 when VISIBILITY_PUBLIC
Chris@1494 80 true
Chris@1494 81 when VISIBILITY_ROLES
Chris@1494 82 if project
Chris@1494 83 (user.roles_for_project(project) & roles).any?
Chris@1494 84 else
Chris@1494 85 Member.where(:user_id => user.id).joins(:roles).where(:member_roles => {:role_id => roles.map(&:id)}).any?
Chris@1494 86 end
Chris@1494 87 else
Chris@1494 88 user == self.user
Chris@1494 89 end
Chris@1494 90 end
Chris@1494 91
Chris@1494 92 def is_private?
Chris@1494 93 visibility == VISIBILITY_PRIVATE
Chris@1494 94 end
Chris@1494 95
Chris@1494 96 def is_public?
Chris@1494 97 !is_private?
Chris@1494 98 end
Chris@1494 99
Chris@1494 100 def draw_relations
Chris@1494 101 r = options[:draw_relations]
Chris@1494 102 r.nil? || r == '1'
Chris@1494 103 end
Chris@1494 104
Chris@1494 105 def draw_relations=(arg)
Chris@1494 106 options[:draw_relations] = (arg == '0' ? '0' : nil)
Chris@1494 107 end
Chris@1494 108
Chris@1494 109 def draw_progress_line
Chris@1494 110 r = options[:draw_progress_line]
Chris@1494 111 r == '1'
Chris@1494 112 end
Chris@1494 113
Chris@1494 114 def draw_progress_line=(arg)
Chris@1494 115 options[:draw_progress_line] = (arg == '1' ? '1' : nil)
Chris@1494 116 end
Chris@1494 117
Chris@1494 118 def build_from_params(params)
Chris@1494 119 super
Chris@1494 120 self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations])
Chris@1494 121 self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line])
Chris@1494 122 self
Chris@1494 123 end
Chris@1494 124
Chris@1494 125 def initialize_available_filters
Chris@1494 126 principals = []
Chris@1494 127 subprojects = []
Chris@1494 128 versions = []
Chris@1494 129 categories = []
Chris@1494 130 issue_custom_fields = []
Chris@1494 131
Chris@1494 132 if project
Chris@1494 133 principals += project.principals.sort
Chris@1494 134 unless project.leaf?
Chris@1494 135 subprojects = project.descendants.visible.all
Chris@1494 136 principals += Principal.member_of(subprojects)
Chris@1494 137 end
Chris@1494 138 versions = project.shared_versions.all
Chris@1494 139 categories = project.issue_categories.all
Chris@1494 140 issue_custom_fields = project.all_issue_custom_fields
Chris@1494 141 else
Chris@1494 142 if all_projects.any?
Chris@1494 143 principals += Principal.member_of(all_projects)
Chris@1494 144 end
Chris@1494 145 versions = Version.visible.find_all_by_sharing('system')
Chris@1494 146 issue_custom_fields = IssueCustomField.where(:is_for_all => true)
Chris@1494 147 end
Chris@1494 148 principals.uniq!
Chris@1494 149 principals.sort!
Chris@1494 150 users = principals.select {|p| p.is_a?(User)}
Chris@1494 151
Chris@1494 152 add_available_filter "status_id",
Chris@1494 153 :type => :list_status, :values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] }
Chris@1494 154
Chris@1494 155 if project.nil?
Chris@1494 156 project_values = []
Chris@1494 157 if User.current.logged? && User.current.memberships.any?
Chris@1494 158 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
Chris@1494 159 end
Chris@1494 160 project_values += all_projects_values
Chris@1494 161 add_available_filter("project_id",
Chris@1494 162 :type => :list, :values => project_values
Chris@1494 163 ) unless project_values.empty?
Chris@1494 164 end
Chris@1494 165
Chris@1494 166 add_available_filter "tracker_id",
Chris@1494 167 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
Chris@1494 168 add_available_filter "priority_id",
Chris@1494 169 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
Chris@1494 170
Chris@1494 171 author_values = []
Chris@1494 172 author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1494 173 author_values += users.collect{|s| [s.name, s.id.to_s] }
Chris@1494 174 add_available_filter("author_id",
Chris@1494 175 :type => :list, :values => author_values
Chris@1494 176 ) unless author_values.empty?
Chris@1494 177
Chris@1494 178 assigned_to_values = []
Chris@1494 179 assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1494 180 assigned_to_values += (Setting.issue_group_assignment? ?
Chris@1494 181 principals : users).collect{|s| [s.name, s.id.to_s] }
Chris@1494 182 add_available_filter("assigned_to_id",
Chris@1494 183 :type => :list_optional, :values => assigned_to_values
Chris@1494 184 ) unless assigned_to_values.empty?
Chris@1494 185
Chris@1494 186 group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
Chris@1494 187 add_available_filter("member_of_group",
Chris@1494 188 :type => :list_optional, :values => group_values
Chris@1494 189 ) unless group_values.empty?
Chris@1494 190
Chris@1494 191 role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
Chris@1494 192 add_available_filter("assigned_to_role",
Chris@1494 193 :type => :list_optional, :values => role_values
Chris@1494 194 ) unless role_values.empty?
Chris@1494 195
Chris@1494 196 if versions.any?
Chris@1494 197 add_available_filter "fixed_version_id",
Chris@1494 198 :type => :list_optional,
Chris@1494 199 :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
Chris@1494 200 end
Chris@1494 201
Chris@1494 202 if categories.any?
Chris@1494 203 add_available_filter "category_id",
Chris@1494 204 :type => :list_optional,
Chris@1494 205 :values => categories.collect{|s| [s.name, s.id.to_s] }
Chris@1494 206 end
Chris@1494 207
Chris@1494 208 add_available_filter "subject", :type => :text
Chris@1494 209 add_available_filter "created_on", :type => :date_past
Chris@1494 210 add_available_filter "updated_on", :type => :date_past
Chris@1494 211 add_available_filter "closed_on", :type => :date_past
Chris@1494 212 add_available_filter "start_date", :type => :date
Chris@1494 213 add_available_filter "due_date", :type => :date
Chris@1494 214 add_available_filter "estimated_hours", :type => :float
Chris@1494 215 add_available_filter "done_ratio", :type => :integer
Chris@1494 216
Chris@1494 217 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
Chris@1494 218 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
Chris@1494 219 add_available_filter "is_private",
Chris@1494 220 :type => :list,
Chris@1494 221 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
Chris@1494 222 end
Chris@1494 223
Chris@1494 224 if User.current.logged?
Chris@1494 225 add_available_filter "watcher_id",
Chris@1494 226 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]]
Chris@1494 227 end
Chris@1494 228
Chris@1494 229 if subprojects.any?
Chris@1494 230 add_available_filter "subproject_id",
Chris@1494 231 :type => :list_subprojects,
Chris@1494 232 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
Chris@1494 233 end
Chris@1494 234
Chris@1494 235 add_custom_fields_filters(issue_custom_fields)
Chris@1494 236
Chris@1494 237 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
Chris@1494 238
Chris@1494 239 IssueRelation::TYPES.each do |relation_type, options|
Chris@1494 240 add_available_filter relation_type, :type => :relation, :label => options[:name]
Chris@1494 241 end
Chris@1494 242
Chris@1494 243 Tracker.disabled_core_fields(trackers).each {|field|
Chris@1494 244 delete_available_filter field
Chris@1494 245 }
Chris@1494 246 end
Chris@1494 247
Chris@1494 248 def available_columns
Chris@1494 249 return @available_columns if @available_columns
Chris@1494 250 @available_columns = self.class.available_columns.dup
Chris@1494 251 @available_columns += (project ?
Chris@1494 252 project.all_issue_custom_fields :
Chris@1494 253 IssueCustomField
Chris@1494 254 ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) }
Chris@1494 255
Chris@1494 256 if User.current.allowed_to?(:view_time_entries, project, :global => true)
Chris@1494 257 index = nil
Chris@1494 258 @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
Chris@1494 259 index = (index ? index + 1 : -1)
Chris@1494 260 # insert the column after estimated_hours or at the end
Chris@1494 261 @available_columns.insert index, QueryColumn.new(:spent_hours,
Chris@1494 262 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
Chris@1494 263 :default_order => 'desc',
Chris@1494 264 :caption => :label_spent_time
Chris@1494 265 )
Chris@1494 266 end
Chris@1494 267
Chris@1494 268 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
Chris@1494 269 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
Chris@1494 270 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
Chris@1494 271 end
Chris@1494 272
Chris@1494 273 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
Chris@1494 274 @available_columns.reject! {|column|
Chris@1494 275 disabled_fields.include?(column.name.to_s)
Chris@1494 276 }
Chris@1494 277
Chris@1494 278 @available_columns
Chris@1494 279 end
Chris@1494 280
Chris@1494 281 def default_columns_names
Chris@1494 282 @default_columns_names ||= begin
Chris@1494 283 default_columns = Setting.issue_list_default_columns.map(&:to_sym)
Chris@1494 284
Chris@1494 285 project.present? ? default_columns : [:project] | default_columns
Chris@1494 286 end
Chris@1494 287 end
Chris@1494 288
Chris@1494 289 # Returns the issue count
Chris@1494 290 def issue_count
Chris@1494 291 Issue.visible.joins(:status, :project).where(statement).count
Chris@1494 292 rescue ::ActiveRecord::StatementInvalid => e
Chris@1494 293 raise StatementInvalid.new(e.message)
Chris@1494 294 end
Chris@1494 295
Chris@1494 296 # Returns the issue count by group or nil if query is not grouped
Chris@1494 297 def issue_count_by_group
Chris@1494 298 r = nil
Chris@1494 299 if grouped?
Chris@1494 300 begin
Chris@1494 301 # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
Chris@1494 302 r = Issue.visible.
Chris@1494 303 joins(:status, :project).
Chris@1494 304 where(statement).
Chris@1494 305 joins(joins_for_order_statement(group_by_statement)).
Chris@1494 306 group(group_by_statement).
Chris@1494 307 count
Chris@1494 308 rescue ActiveRecord::RecordNotFound
Chris@1494 309 r = {nil => issue_count}
Chris@1494 310 end
Chris@1494 311 c = group_by_column
Chris@1494 312 if c.is_a?(QueryCustomFieldColumn)
Chris@1494 313 r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
Chris@1494 314 end
Chris@1494 315 end
Chris@1494 316 r
Chris@1494 317 rescue ::ActiveRecord::StatementInvalid => e
Chris@1494 318 raise StatementInvalid.new(e.message)
Chris@1494 319 end
Chris@1494 320
Chris@1494 321 # Returns the issues
Chris@1494 322 # Valid options are :order, :offset, :limit, :include, :conditions
Chris@1494 323 def issues(options={})
Chris@1494 324 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1494 325
Chris@1494 326 scope = Issue.visible.
Chris@1494 327 joins(:status, :project).
Chris@1494 328 where(statement).
Chris@1494 329 includes(([:status, :project] + (options[:include] || [])).uniq).
Chris@1494 330 where(options[:conditions]).
Chris@1494 331 order(order_option).
Chris@1494 332 joins(joins_for_order_statement(order_option.join(','))).
Chris@1494 333 limit(options[:limit]).
Chris@1494 334 offset(options[:offset])
Chris@1494 335
Chris@1494 336 if has_custom_field_column?
Chris@1494 337 scope = scope.preload(:custom_values)
Chris@1494 338 end
Chris@1494 339
Chris@1494 340 issues = scope.all
Chris@1494 341
Chris@1494 342 if has_column?(:spent_hours)
Chris@1494 343 Issue.load_visible_spent_hours(issues)
Chris@1494 344 end
Chris@1494 345 if has_column?(:relations)
Chris@1494 346 Issue.load_visible_relations(issues)
Chris@1494 347 end
Chris@1494 348 issues
Chris@1494 349 rescue ::ActiveRecord::StatementInvalid => e
Chris@1494 350 raise StatementInvalid.new(e.message)
Chris@1494 351 end
Chris@1494 352
Chris@1494 353 # Returns the issues ids
Chris@1494 354 def issue_ids(options={})
Chris@1494 355 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1494 356
Chris@1494 357 Issue.visible.
Chris@1494 358 joins(:status, :project).
Chris@1494 359 where(statement).
Chris@1494 360 includes(([:status, :project] + (options[:include] || [])).uniq).
Chris@1494 361 where(options[:conditions]).
Chris@1494 362 order(order_option).
Chris@1494 363 joins(joins_for_order_statement(order_option.join(','))).
Chris@1494 364 limit(options[:limit]).
Chris@1494 365 offset(options[:offset]).
Chris@1494 366 find_ids
Chris@1494 367 rescue ::ActiveRecord::StatementInvalid => e
Chris@1494 368 raise StatementInvalid.new(e.message)
Chris@1494 369 end
Chris@1494 370
Chris@1494 371 # Returns the journals
Chris@1494 372 # Valid options are :order, :offset, :limit
Chris@1494 373 def journals(options={})
Chris@1494 374 Journal.visible.
Chris@1494 375 joins(:issue => [:project, :status]).
Chris@1494 376 where(statement).
Chris@1494 377 order(options[:order]).
Chris@1494 378 limit(options[:limit]).
Chris@1494 379 offset(options[:offset]).
Chris@1494 380 preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}).
Chris@1494 381 all
Chris@1494 382 rescue ::ActiveRecord::StatementInvalid => e
Chris@1494 383 raise StatementInvalid.new(e.message)
Chris@1494 384 end
Chris@1494 385
Chris@1494 386 # Returns the versions
Chris@1494 387 # Valid options are :conditions
Chris@1494 388 def versions(options={})
Chris@1494 389 Version.visible.
Chris@1494 390 where(project_statement).
Chris@1494 391 where(options[:conditions]).
Chris@1494 392 includes(:project).
Chris@1494 393 all
Chris@1494 394 rescue ::ActiveRecord::StatementInvalid => e
Chris@1494 395 raise StatementInvalid.new(e.message)
Chris@1494 396 end
Chris@1494 397
Chris@1494 398 def sql_for_watcher_id_field(field, operator, value)
Chris@1494 399 db_table = Watcher.table_name
Chris@1494 400 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
Chris@1494 401 sql_for_field(field, '=', value, db_table, 'user_id') + ')'
Chris@1494 402 end
Chris@1494 403
Chris@1494 404 def sql_for_member_of_group_field(field, operator, value)
Chris@1494 405 if operator == '*' # Any group
Chris@1494 406 groups = Group.all
Chris@1494 407 operator = '=' # Override the operator since we want to find by assigned_to
Chris@1494 408 elsif operator == "!*"
Chris@1494 409 groups = Group.all
Chris@1494 410 operator = '!' # Override the operator since we want to find by assigned_to
Chris@1494 411 else
Chris@1494 412 groups = Group.find_all_by_id(value)
Chris@1494 413 end
Chris@1494 414 groups ||= []
Chris@1494 415
Chris@1494 416 members_of_groups = groups.inject([]) {|user_ids, group|
Chris@1494 417 user_ids + group.user_ids + [group.id]
Chris@1494 418 }.uniq.compact.sort.collect(&:to_s)
Chris@1494 419
Chris@1494 420 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
Chris@1494 421 end
Chris@1494 422
Chris@1494 423 def sql_for_assigned_to_role_field(field, operator, value)
Chris@1494 424 case operator
Chris@1494 425 when "*", "!*" # Member / Not member
Chris@1494 426 sw = operator == "!*" ? 'NOT' : ''
Chris@1494 427 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
Chris@1494 428 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
Chris@1494 429 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
Chris@1494 430 when "=", "!"
Chris@1494 431 role_cond = value.any? ?
Chris@1494 432 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
Chris@1494 433 "1=0"
Chris@1494 434
Chris@1494 435 sw = operator == "!" ? 'NOT' : ''
Chris@1494 436 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
Chris@1494 437 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
Chris@1494 438 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
Chris@1494 439 end
Chris@1494 440 end
Chris@1494 441
Chris@1494 442 def sql_for_is_private_field(field, operator, value)
Chris@1494 443 op = (operator == "=" ? 'IN' : 'NOT IN')
Chris@1494 444 va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',')
Chris@1494 445
Chris@1494 446 "#{Issue.table_name}.is_private #{op} (#{va})"
Chris@1494 447 end
Chris@1494 448
Chris@1494 449 def sql_for_relations(field, operator, value, options={})
Chris@1494 450 relation_options = IssueRelation::TYPES[field]
Chris@1494 451 return relation_options unless relation_options
Chris@1494 452
Chris@1494 453 relation_type = field
Chris@1494 454 join_column, target_join_column = "issue_from_id", "issue_to_id"
Chris@1494 455 if relation_options[:reverse] || options[:reverse]
Chris@1494 456 relation_type = relation_options[:reverse] || relation_type
Chris@1494 457 join_column, target_join_column = target_join_column, join_column
Chris@1494 458 end
Chris@1494 459
Chris@1494 460 sql = case operator
Chris@1494 461 when "*", "!*"
Chris@1494 462 op = (operator == "*" ? 'IN' : 'NOT IN')
Chris@1494 463 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')"
Chris@1494 464 when "=", "!"
Chris@1494 465 op = (operator == "=" ? 'IN' : 'NOT IN')
Chris@1494 466 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})"
Chris@1494 467 when "=p", "=!p", "!p"
Chris@1494 468 op = (operator == "!p" ? 'NOT IN' : 'IN')
Chris@1494 469 comp = (operator == "=!p" ? '<>' : '=')
Chris@1494 470 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})"
Chris@1494 471 end
Chris@1494 472
Chris@1494 473 if relation_options[:sym] == field && !options[:reverse]
Chris@1494 474 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
Chris@1494 475 sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
Chris@1494 476 end
Chris@1494 477 "(#{sql})"
Chris@1494 478 end
Chris@1494 479
Chris@1494 480 IssueRelation::TYPES.keys.each do |relation_type|
Chris@1494 481 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
Chris@1494 482 end
Chris@1494 483 end