issue-filter-by-spent-time.diff

Anonymous, 2017-09-01 14:50

Download (2.88 KB)

View differences:

app/models/issue_query.rb
88 88
      :type => :list, :values => lambda { project_values }
89 89
    ) if project.nil?
90 90

  
91
    if User.current.allowed_to?(:view_time_entries, project, :global => true)
92
      add_available_filter "spent_time", :type => :float, :label => :label_spent_time
93
    end
94

  
91 95
    add_available_filter "tracker_id",
92 96
      :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
93 97

  
......
380 384
    "#{neg} EXISTS (#{subquery})"
381 385
  end
382 386

  
387
  def sql_for_spent_time_field(field, operator, value)
388
    first, second = value
389

  
390
    sql_op =
391
      case operator
392
      when "=", ">=", "<=" ;  "#{operator} #{first.to_f}"
393
      when "><"            ;  "BETWEEN #{first.to_f} AND #{second.to_f}"
394
      when "*"             ;  "> 0"
395
      when "!*"            ;  "= 0"
396
      end
397

  
398
    "COALESCE((" +
399
      "SELECT ROUND(CAST(SUM(hours) AS NUMERIC), 2) " +
400
      "FROM #{TimeEntry.table_name} " +
401
      "WHERE issue_id = #{Issue.table_name}.id), 0) #{sql_op}"
402
  end
403

  
383 404
  def sql_for_watcher_id_field(field, operator, value)
384 405
    db_table = Watcher.table_name
385 406
    "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
test/unit/query_test.rb
2119 2119

  
2120 2120
    assert_equal ['1','2','3','4','5','6'], query.available_filters['status_id'][:values].map(&:second)
2121 2121
  end
2122

  
2123
  def test_issue_query_filter_by_spent_time
2124
    query = IssueQuery.new(:name => '_')
2125

  
2126
    query.filters = {'spent_time' => {:operator => '*', :values => ['']}}
2127
    assert_equal query.issues.pluck(:id), [3, 1]
2128

  
2129
    query.filters = {'spent_time' => {:operator => '!*', :values => ['']}}
2130
    assert_equal query.issues.pluck(:id), [13, 12, 11, 8, 7, 5, 2]
2131

  
2132
    query.filters = {'spent_time' => {:operator => '>=', :values => ['10']}}
2133
    assert_equal query.issues.pluck(:id), [1]
2134

  
2135
    query.filters = {'spent_time' => {:operator => '<=', :values => ['10']}}
2136
    assert_equal query.issues.pluck(:id), [13, 12, 11, 8, 7, 5, 3, 2]
2137

  
2138
    query.filters = {'spent_time' => {:operator => '><', :values => ['1', '2']}}
2139
    assert_equal query.issues.pluck(:id), [3]
2140
  end
2122 2141
end
2123
-