Changes how relative date filters work and adds specific filters for filtering dates in past/next n days (#11426).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10768 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-10-30 08:21:15 +00:00
parent b9c72bf590
commit d62ef6b9b1
5 changed files with 44 additions and 5 deletions

View File

@ -106,11 +106,13 @@ class Query < ActiveRecord::Base
"><" => :label_between, "><" => :label_between,
"<t+" => :label_in_less_than, "<t+" => :label_in_less_than,
">t+" => :label_in_more_than, ">t+" => :label_in_more_than,
"><t+"=> :label_in_the_next_days,
"t+" => :label_in, "t+" => :label_in,
"t" => :label_today, "t" => :label_today,
"w" => :label_this_week, "w" => :label_this_week,
">t-" => :label_less_than_ago, ">t-" => :label_less_than_ago,
"<t-" => :label_more_than_ago, "<t-" => :label_more_than_ago,
"><t-"=> :label_in_the_past_days,
"t-" => :label_ago, "t-" => :label_ago,
"~" => :label_contains, "~" => :label_contains,
"!~" => :label_not_contains, "!~" => :label_not_contains,
@ -124,8 +126,8 @@ class Query < ActiveRecord::Base
:list_status => [ "o", "=", "!", "c", "*" ], :list_status => [ "o", "=", "!", "c", "*" ],
:list_optional => [ "=", "!", "!*", "*" ], :list_optional => [ "=", "!", "!*", "*" ],
:list_subprojects => [ "*", "!*", "=" ], :list_subprojects => [ "*", "!*", "=" ],
:date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "><t+", "t+", "t", "w", ">t-", "<t-", "><t-", "t-", "!*", "*" ],
:date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "><t-", "t-", "t", "w", "!*", "*" ],
:string => [ "=", "~", "!", "!~", "!*", "*" ], :string => [ "=", "~", "!", "!~", "!*", "*" ],
:text => [ "~", "!~", "!*", "*" ], :text => [ "~", "!~", "!*", "*" ],
:integer => [ "=", ">=", "<=", "><", "!*", "*" ], :integer => [ "=", ">=", "<=", "><", "!*", "*" ],
@ -183,7 +185,7 @@ class Query < ActiveRecord::Base
case operator_for(field) case operator_for(field)
when "=", ">=", "<=", "><" when "=", ">=", "<=", "><"
add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) }
when ">t-", "<t-", "t-", ">t+", "<t+", "t+" 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+$/) } add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
end end
end end
@ -936,21 +938,35 @@ class Query < ActiveRecord::Base
sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id"
when "c" when "c"
sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id"
when ">t-" when "><t-"
# between today - n days and today
sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0)
when ">t-"
# >= today - n days
sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil)
when "<t-" when "<t-"
# <= today - n days
sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i)
when "t-" when "t-"
# = n days in past
sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i)
when "><t+"
# between today and today + n days
sql = relative_date_clause(db_table, db_field, 0, value.first.to_i)
when ">t+" when ">t+"
# >= today + n days
sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) sql = relative_date_clause(db_table, db_field, value.first.to_i, nil)
when "<t+" when "<t+"
sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) # <= today + n days
sql = relative_date_clause(db_table, db_field, nil, value.first.to_i)
when "t+" when "t+"
# = today + n days
sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i)
when "t" when "t"
# = today
sql = relative_date_clause(db_table, db_field, 0, 0) sql = relative_date_clause(db_table, db_field, 0, 0)
when "w" when "w"
# = this week
first_day_of_week = l(:general_first_day_of_week).to_i first_day_of_week = l(:general_first_day_of_week).to_i
day_of_week = Date.today.cwday day_of_week = Date.today.cwday
days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)

View File

@ -656,6 +656,8 @@ en:
label_not_equals: is not label_not_equals: is not
label_in_less_than: in less than label_in_less_than: in less than
label_in_more_than: in more than label_in_more_than: in more than
label_in_the_next_days: in the next
label_in_the_past_days: in the past
label_greater_or_equal: '>=' label_greater_or_equal: '>='
label_less_or_equal: '<=' label_less_or_equal: '<='
label_between: between label_between: between

View File

@ -649,6 +649,8 @@ fr:
label_not_equals: différent label_not_equals: différent
label_in_less_than: dans moins de label_in_less_than: dans moins de
label_in_more_than: dans plus de label_in_more_than: dans plus de
label_in_the_next_days: dans les prochains jours
label_in_the_past_days: dans les derniers jours
label_in: dans label_in: dans
label_today: aujourd'hui label_today: aujourd'hui
label_all_time: toute la période label_all_time: toute la période

View File

@ -252,9 +252,11 @@ function toggleOperator(field) {
break; break;
case "<t+": case "<t+":
case ">t+": case ">t+":
case "><t+":
case "t+": case "t+":
case ">t-": case ">t-":
case "<t-": case "<t-":
case "><t-":
case "t-": case "t-":
enableValues(field, [2]); enableValues(field, [2]);
break; break;

View File

@ -413,6 +413,14 @@ class QueryTest < ActiveSupport::TestCase
query.add_filter('due_date', '<t+', ['15']) query.add_filter('due_date', '<t+', ['15'])
issues = find_issues_with_query(query) issues = find_issues_with_query(query)
assert !issues.empty? assert !issues.empty?
issues.each {|issue| assert(issue.due_date <= (Date.today + 15))}
end
def test_operator_in_the_next_days
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '><t+', ['15'])
issues = find_issues_with_query(query)
assert !issues.empty?
issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))}
end end
@ -422,6 +430,15 @@ class QueryTest < ActiveSupport::TestCase
query.add_filter('due_date', '>t-', ['3']) query.add_filter('due_date', '>t-', ['3'])
issues = find_issues_with_query(query) issues = find_issues_with_query(query)
assert !issues.empty? assert !issues.empty?
issues.each {|issue| assert(issue.due_date >= (Date.today - 3))}
end
def test_operator_in_the_past_days
Issue.find(7).update_attribute(:due_date, (Date.today - 3))
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '><t-', ['3'])
issues = find_issues_with_query(query)
assert !issues.empty?
issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)}
end end