Class: Query

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::SubclassFactory
Defined in:
app/models/query.rb

Direct Known Subclasses

IssueQuery, TimeEntryQuery

Defined Under Namespace

Classes: StatementInvalid

Constant Summary

VISIBILITY_PRIVATE =
0
VISIBILITY_ROLES =
1
VISIBILITY_PUBLIC =
2

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SubclassFactory

included

Constructor Details

#initialize(attributes = nil, *args) ⇒ Query

Returns a new instance of Query



328
329
330
331
# File 'app/models/query.rb', line 328

def initialize(attributes=nil, *args)
  super attributes
  @is_for_all = project.nil?
end

Class Method Details

.add_available_column(column) ⇒ Object



521
522
523
# File 'app/models/query.rb', line 521

def self.add_available_column(column)
  self.available_columns << (column) if column.is_a?(QueryColumn)
end

.build_from_params(params, attributes = {}) ⇒ Object

Builds a new query from the given params and attributes



350
351
352
# File 'app/models/query.rb', line 350

def self.build_from_params(params, attributes={})
  new(attributes).build_from_params(params)
end

.operators_labelsObject

Returns a hash of localized labels for all filter operators



400
401
402
# File 'app/models/query.rb', line 400

def self.operators_labels
  operators.inject({}) {|h, operator| h[operator.first] = l(*operator.last); h}
end

.visible(*args) ⇒ Object

Scope of visible queries, can be used from subclasses only. Unlike other visible scopes, a class methods is used as it let handle inheritance more nicely than scope DSL.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/models/query.rb', line 267

def self.visible(*args)
  if self == ::Query
    # Visibility depends on permissions for each subclass,
    # raise an error if the scope is called from Query (eg. Query.visible)
    raise Exception.new("Cannot call .visible scope from the base Query class, but from subclasses only.")
  end

  user = args.shift || User.current
  base = Project.allowed_to_condition(user, view_permission, *args)
  scope = joins("LEFT OUTER JOIN #{Project.table_name} ON #{table_name}.project_id = #{Project.table_name}.id").
    where("#{table_name}.project_id IS NULL OR (#{base})")

  if user.admin?
    scope.where("#{table_name}.visibility <> ? OR #{table_name}.user_id = ?", VISIBILITY_PRIVATE, user.id)
  elsif user.memberships.any?
    scope.where("#{table_name}.visibility = ?" +
      " OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" +
        "SELECT DISTINCT q.id FROM #{table_name} q" +
        " INNER JOIN #{table_name_prefix}queries_roles#{table_name_suffix} qr on qr.query_id = q.id" +
        " INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = qr.role_id" +
        " INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" +
        " WHERE q.project_id IS NULL OR q.project_id = m.project_id))" +
      " OR #{table_name}.user_id = ?",
      VISIBILITY_PUBLIC, VISIBILITY_ROLES, user.id, user.id)
  elsif user.logged?
    scope.where("#{table_name}.visibility = ? OR #{table_name}.user_id = ?", VISIBILITY_PUBLIC, user.id)
  else
    scope.where("#{table_name}.visibility = ?", VISIBILITY_PUBLIC)
  end
end

Instance Method Details

#add_available_filter(field, options) ⇒ Object

Adds an available filter



442
443
444
445
446
# File 'app/models/query.rb', line 442

def add_available_filter(field, options)
  @available_filters ||= ActiveSupport::OrderedHash.new
  @available_filters[field] = options
  @available_filters
end

#add_filter(field, operator, values = nil) ⇒ Object



467
468
469
470
471
472
473
474
475
# File 'app/models/query.rb', line 467

def add_filter(field, operator, values=nil)
  # values must be an array
  return unless values.nil? || values.is_a?(Array)
  # check if field is defined as an available filter
  if available_filters.has_key? field
    filter_options = available_filters[field]
    filters[field] = {:operator => operator, :values => (values || [''])}
  end
end

#add_filter_error(field, message) ⇒ Object



382
383
384
385
# File 'app/models/query.rb', line 382

def add_filter_error(field, message)
  m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages')
  errors.add(:base, m)
end

#add_filters(fields, operators, values) ⇒ Object

Add multiple filters using add_filter



488
489
490
491
492
493
494
# File 'app/models/query.rb', line 488

def add_filters(fields, operators, values)
  if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash))
    fields.each do |field|
      add_filter(field, operators[field], values && values[field])
    end
  end
end

#add_short_filter(field, expression) ⇒ Object



477
478
479
480
481
482
483
484
485
# File 'app/models/query.rb', line 477

def add_short_filter(field, expression)
  return unless expression && available_filters.has_key?(field)
  field_type = available_filters[field][:type]
  operators_by_filter_type[field_type].sort.reverse.detect do |operator|
    next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/
    values = $1
    add_filter field, operator, values.present? ? values.split('|') : ['']
  end || add_filter(field, '=', expression.to_s.split('|'))
end

#all_projectsObject



420
421
422
# File 'app/models/query.rb', line 420

def all_projects
  @all_projects ||= Project.visible.to_a
end

#all_projects_valuesObject



424
425
426
427
428
429
430
431
432
433
# File 'app/models/query.rb', line 424

def all_projects_values
  return @all_projects_values if @all_projects_values

  values = []
  Project.project_tree(all_projects) do |p, level|
    prefix = (level > 0 ? ('--' * level + ' ') : '')
    values << ["#{prefix}#{p.name}", p.id.to_s]
  end
  @all_projects_values = values
end

#available_block_columnsObject



558
559
560
# File 'app/models/query.rb', line 558

def available_block_columns
  available_columns.reject(&:inline?)
end

#available_filtersObject

Return a hash of available filters



456
457
458
459
460
461
462
463
464
465
# File 'app/models/query.rb', line 456

def available_filters
  unless @available_filters
    initialize_available_filters
    @available_filters ||= {}
    @available_filters.each do |field, options|
      options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, ''))
    end
  end
  @available_filters
end

#available_filters_as_jsonObject

Returns a representation of the available filters for JSON serialization



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/models/query.rb', line 405

def available_filters_as_json
  json = {}
  available_filters.each do |field, options|
    options = options.slice(:type, :name, :values)
    if options[:values] && values_for(field)
      missing = Array(values_for(field)).select(&:present?) - options[:values].map(&:last)
      if missing.any? && respond_to?(method = "find_#{field}_filter_values")
        options[:values] += send(method, missing)
      end
    end
    json[field] = options.stringify_keys
  end
  json
end

#available_inline_columnsObject



554
555
556
# File 'app/models/query.rb', line 554

def available_inline_columns
  available_columns.select(&:inline?)
end

#available_totalable_columnsObject



562
563
564
# File 'app/models/query.rb', line 562

def available_totalable_columns
  available_columns.select(&:totalable)
end

#block_columnsObject



550
551
552
# File 'app/models/query.rb', line 550

def block_columns
  columns.reject(&:inline?)
end

#build_from_params(params) ⇒ Object

Builds the query from the given params



334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'app/models/query.rb', line 334

def build_from_params(params)
  if params[:fields] || params[:f]
    self.filters = {}
    add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
  else
    available_filters.keys.each do |field|
      add_short_filter(field, params[field]) if params[field]
    end
  end
  self.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
  self.column_names = params[:c] || (params[:query] && params[:query][:column_names])
  self.totalable_names = params[:t] || (params[:query] && params[:query][:totalable_names])
  self
end

#column_names=(names) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
# File 'app/models/query.rb', line 574

def column_names=(names)
  if names
    names = names.select {|n| n.is_a?(Symbol) || !n.blank? }
    names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym }
    # Set column_names to nil if default columns
    if names == default_columns_names
      names = nil
    end
  end
  write_attribute(:column_names, names)
end

#columnsObject



538
539
540
541
542
543
544
# File 'app/models/query.rb', line 538

def columns
  # preserve the column_names order
  cols = (has_default_columns? ? default_columns_names : column_names).collect do |name|
     available_columns.find { |col| col.name == name }
  end.compact
  available_columns.select(&:frozen?) | cols
end

#default_columns_namesObject



566
567
568
# File 'app/models/query.rb', line 566

def default_columns_names
  []
end

#default_totalable_namesObject



570
571
572
# File 'app/models/query.rb', line 570

def default_totalable_names
  []
end

#delete_available_filter(field) ⇒ Object

Removes an available filter



449
450
451
452
453
# File 'app/models/query.rb', line 449

def delete_available_filter(field)
  if @available_filters
    @available_filters.delete(field)
  end
end

#editable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


387
388
389
390
391
392
393
# File 'app/models/query.rb', line 387

def editable_by?(user)
  return false unless user
  # Admin can edit them all and regular users can edit their private queries
  return true if user.admin? || (is_private? && self.user_id == user.id)
  # Members can not edit public queries that are for all project (only admin is allowed to)
  is_public? && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
end

#group_by_columnObject



654
655
656
# File 'app/models/query.rb', line 654

def group_by_column
  groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by}
end

#group_by_sort_orderObject

Returns the SQL sort order that should be prepended for grouping



642
643
644
645
646
647
# File 'app/models/query.rb', line 642

def group_by_sort_order
  if column = group_by_column
    order = (sort_criteria_order_for(column.name) || column.default_order || 'asc').try(:upcase)
    Array(column.sortable).map {|s| "#{s} #{order}"}
  end
end

#group_by_statementObject



658
659
660
# File 'app/models/query.rb', line 658

def group_by_statement
  group_by_column.try(:groupable)
end

#groupable_columnsObject

Returns an array of columns that can be used to group the results



526
527
528
# File 'app/models/query.rb', line 526

def groupable_columns
  available_columns.select {|c| c.groupable}
end

#grouped?Boolean

Returns true if the query is a grouped query

Returns:

  • (Boolean)


650
651
652
# File 'app/models/query.rb', line 650

def grouped?
  !group_by_column.nil?
end

#has_column?(column) ⇒ Boolean

Returns:

  • (Boolean)


586
587
588
# File 'app/models/query.rb', line 586

def has_column?(column)
  column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column)
end

#has_custom_field_column?Boolean

Returns:

  • (Boolean)


590
591
592
# File 'app/models/query.rb', line 590

def has_custom_field_column?
  columns.any? {|column| column.is_a? QueryCustomFieldColumn}
end

#has_default_columns?Boolean

Returns:

  • (Boolean)


594
595
596
# File 'app/models/query.rb', line 594

def has_default_columns?
  column_names.nil? || column_names.empty?
end

#has_filter?(field) ⇒ Boolean

Returns:

  • (Boolean)


496
497
498
# File 'app/models/query.rb', line 496

def has_filter?(field)
  filters and filters[field]
end

#inline_columnsObject



546
547
548
# File 'app/models/query.rb', line 546

def inline_columns
  columns.select(&:inline?)
end

#is_private?Boolean

Returns:

  • (Boolean)


316
317
318
# File 'app/models/query.rb', line 316

def is_private?
  visibility == VISIBILITY_PRIVATE
end

#is_public?Boolean

Returns:

  • (Boolean)


320
321
322
# File 'app/models/query.rb', line 320

def is_public?
  !is_private?
end

#label_for(field) ⇒ Object



516
517
518
519
# File 'app/models/query.rb', line 516

def label_for(field)
  label = available_filters[field][:name] if available_filters.has_key?(field)
  label ||= queried_class.human_attribute_name(field, :default => field)
end

#operator_for(field) ⇒ Object



504
505
506
# File 'app/models/query.rb', line 504

def operator_for(field)
  has_filter?(field) ? filters[field][:operator] : nil
end

#project_statementObject



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'app/models/query.rb', line 662

def project_statement
  project_clauses = []
  if project && !project.descendants.active.empty?
    if has_filter?("subproject_id")
      case operator_for("subproject_id")
      when '='
        # include the selected subprojects
        ids = [project.id] + values_for("subproject_id").each(&:to_i)
        project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',')
      when '!*'
        # main project only
        project_clauses << "#{Project.table_name}.id = %d" % project.id
      else
        # all subprojects
        project_clauses << "#{Project.table_name}.lft >= #{project.lft} AND #{Project.table_name}.rgt <= #{project.rgt}"
      end
    elsif Setting.display_subprojects_issues?
      project_clauses << "#{Project.table_name}.lft >= #{project.lft} AND #{Project.table_name}.rgt <= #{project.rgt}"
    else
      project_clauses << "#{Project.table_name}.id = %d" % project.id
    end
  elsif project
    project_clauses << "#{Project.table_name}.id = %d" % project.id
  end
  project_clauses.any? ? project_clauses.join(' AND ') : nil
end

#queried_table_nameObject



324
325
326
# File 'app/models/query.rb', line 324

def queried_table_name
  @queried_table_name ||= self.class.queried_class.table_name
end

#sort_criteriaObject



625
626
627
# File 'app/models/query.rb', line 625

def sort_criteria
  read_attribute(:sort_criteria) || []
end

#sort_criteria=(arg) ⇒ Object



614
615
616
617
618
619
620
621
622
623
# File 'app/models/query.rb', line 614

def sort_criteria=(arg)
  c = []
  if arg.is_a?(Hash)
    arg = arg.keys.sort.collect {|k| arg[k]}
  end
  if arg
    c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']}
  end
  write_attribute(:sort_criteria, c)
end

#sort_criteria_key(arg) ⇒ Object



629
630
631
# File 'app/models/query.rb', line 629

def sort_criteria_key(arg)
  sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
end

#sort_criteria_order(arg) ⇒ Object



633
634
635
# File 'app/models/query.rb', line 633

def sort_criteria_order(arg)
  sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
end

#sort_criteria_order_for(key) ⇒ Object



637
638
639
# File 'app/models/query.rb', line 637

def sort_criteria_order_for(key)
  sort_criteria.detect {|k, order| key.to_s == k}.try(:last)
end

#sortable_columnsObject

Returns a Hash of columns and the key for sorting



531
532
533
534
535
536
# File 'app/models/query.rb', line 531

def sortable_columns
  available_columns.inject({}) {|h, column|
    h[column.name.to_s] = column.sortable
    h
  }
end

#statementObject



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'app/models/query.rb', line 689

def statement
  # filters clauses
  filters_clauses = []
  filters.each_key do |field|
    next if field == "subproject_id"
    v = values_for(field).clone
    next unless v and !v.empty?
    operator = operator_for(field)

    # "me" value substitution
    if %w(assigned_to_id author_id user_id watcher_id).include?(field)
      if v.delete("me")
        if User.current.logged?
          v.push(User.current.id.to_s)
          v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id'
        else
          v.push("0")
        end
      end
    end

    if field == 'project_id'
      if v.delete('mine')
        v += User.current.memberships.map(&:project_id).map(&:to_s)
      end
    end

    if field =~ /cf_(\d+)$/
      # custom field
      filters_clauses << sql_for_custom_field(field, operator, v, $1)
    elsif respond_to?(method = "sql_for_#{field.gsub('.','_')}_field")
      # specific statement
      filters_clauses << send(method, field, operator, v)
    else
      # regular field
      filters_clauses << '(' + sql_for_field(field, operator, v, queried_table_name, field) + ')'
    end
  end if filters and valid?

  if (c = group_by_column) && c.is_a?(QueryCustomFieldColumn)
    # Excludes results for which the grouped custom field is not visible
    filters_clauses << c.custom_field.visibility_by_project_condition
  end

  filters_clauses << project_statement
  filters_clauses.reject!(&:blank?)

  filters_clauses.any? ? filters_clauses.join(' AND ') : nil
end

#total_by_group_for(column) ⇒ Object

Returns a hash of the sum of the given column for each group, or nil if the query is not grouped



746
747
748
749
750
# File 'app/models/query.rb', line 746

def total_by_group_for(column)
  grouped_query do |scope|
    total_with_scope(column, scope)
  end
end

#total_for(column) ⇒ Object

Returns the sum of values for the given column



740
741
742
# File 'app/models/query.rb', line 740

def total_for(column)
  total_with_scope(column, base_scope)
end

#totalable_columnsObject



598
599
600
601
# File 'app/models/query.rb', line 598

def totalable_columns
  names = totalable_names
  available_totalable_columns.select {|column| names.include?(column.name)}
end

#totalable_namesObject



610
611
612
# File 'app/models/query.rb', line 610

def totalable_names
  options[:totalable_names] || default_totalable_names || []
end

#totalable_names=(names) ⇒ Object



603
604
605
606
607
608
# File 'app/models/query.rb', line 603

def totalable_names=(names)
  if names
    names = names.select(&:present?).map {|n| n.is_a?(Symbol) ? n : n.to_sym}
  end
  options[:totalable_names] = names
end

#totals {|totals| ... } ⇒ Object

Yields:



752
753
754
755
756
# File 'app/models/query.rb', line 752

def totals
  totals = totalable_columns.map {|column| [column, total_for(column)]}
  yield totals if block_given?
  totals
end

#totals_by_group {|totals| ... } ⇒ Object

Yields:



758
759
760
761
762
# File 'app/models/query.rb', line 758

def totals_by_group
  totals = totalable_columns.map {|column| [column, total_by_group_for(column)]}
  yield totals if block_given?
  totals
end

#trackersObject



395
396
397
# File 'app/models/query.rb', line 395

def trackers
  @trackers ||= (project.nil? ? Tracker.all : project.rolled_up_trackers).visible.sorted
end

#type_for(field) ⇒ Object



500
501
502
# File 'app/models/query.rb', line 500

def type_for(field)
  available_filters[field][:type] if available_filters.has_key?(field)
end

#validate_query_filtersObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'app/models/query.rb', line 354

def validate_query_filters
  filters.each_key do |field|
    if values_for(field)
      case type_for(field)
      when :integer
        add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/\A[+-]?\d+(,[+-]?\d+)*\z/) }
      when :float
        add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/\A[+-]?\d+(\.\d*)?\z/) }
      when :date, :date_past
        case operator_for(field)
        when "=", ">=", "<=", "><"
          add_filter_error(field, :invalid) if values_for(field).detect {|v|
            v.present? && (!v.match(/\A\d{4}-\d{2}-\d{2}(T\d{2}((:)?\d{2}){0,2}(Z|\d{2}:?\d{2})?)?\z/) || parse_date(v).nil?)
          }
        when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-"
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
        end
      end
    end

    add_filter_error(field, :blank) unless
        # filter requires one or more values
        (values_for(field) and !values_for(field).first.blank?) or
        # filter doesn't require any value
        ["o", "c", "!*", "*", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", "*o", "!o"].include? operator_for(field)
  end if filters
end

#value_for(field, index = 0) ⇒ Object



512
513
514
# File 'app/models/query.rb', line 512

def value_for(field, index=0)
  (values_for(field) || [])[index]
end

#values_for(field) ⇒ Object



508
509
510
# File 'app/models/query.rb', line 508

def values_for(field)
  has_filter?(field) ? filters[field][:values] : nil
end

#visible?(user = User.current) ⇒ Boolean

Returns true if the query is visible to user or the current user.

Returns:

  • (Boolean)


299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'app/models/query.rb', line 299

def visible?(user=User.current)
  return true if user.admin?
  return false unless project.nil? || user.allowed_to?(self.class.view_permission, project)
  case visibility
  when VISIBILITY_PUBLIC
    true
  when VISIBILITY_ROLES
    if project
      (user.roles_for_project(project) & roles).any?
    else
      Member.where(:user_id => user.id).joins(:roles).where(:member_roles => {:role_id => roles.map(&:id)}).any?
    end
  else
    user == self.user
  end
end