[#796] Adds date range filter.
Based on r6226 from Redmine by Jean-Philippe Lang
This commit is contained in:
parent
260e8b84f8
commit
72eadcc6ea
|
@ -33,6 +33,7 @@ class Query < ActiveRecord::Base
|
|||
"*" => :label_all,
|
||||
">=" => :label_greater_or_equal,
|
||||
"<=" => :label_less_or_equal,
|
||||
"><" => :label_between,
|
||||
"<t+" => :label_in_less_than,
|
||||
">t+" => :label_in_more_than,
|
||||
"t+" => :label_in,
|
||||
|
@ -50,8 +51,8 @@ class Query < ActiveRecord::Base
|
|||
:list_status => [ "o", "=", "!", "c", "*" ],
|
||||
:list_optional => [ "=", "!", "!*", "*" ],
|
||||
:list_subprojects => [ "*", "!*", "=" ],
|
||||
:date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ],
|
||||
:date_past => [ ">t-", "<t-", "t-", "t", "w" ],
|
||||
:date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ],
|
||||
:date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w" ],
|
||||
:string => [ "=", "~", "!", "!~" ],
|
||||
:text => [ "~", "!~" ],
|
||||
:integer => [ "=", ">=", "<=", "!*", "*" ] }
|
||||
|
@ -189,7 +190,7 @@ class Query < ActiveRecord::Base
|
|||
|
||||
def add_filter(field, operator, values)
|
||||
# values must be an array
|
||||
return unless values and values.is_a? Array # and !values.first.empty?
|
||||
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]
|
||||
|
@ -198,7 +199,7 @@ class Query < ActiveRecord::Base
|
|||
# allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
|
||||
# filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
|
||||
#end
|
||||
filters[field] = {:operator => operator, :values => values }
|
||||
filters[field] = {:operator => operator, :values => (values || ['']) }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -210,9 +211,9 @@ class Query < ActiveRecord::Base
|
|||
|
||||
# Add multiple filters using +add_filter+
|
||||
def add_filters(fields, operators, values)
|
||||
if fields.is_a?(Array) && operators.is_a?(Hash) && values.is_a?(Hash)
|
||||
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[field])
|
||||
add_filter(field, operators[field], values && values[field])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -220,6 +221,10 @@ class Query < ActiveRecord::Base
|
|||
def has_filter?(field)
|
||||
filters and filters[field]
|
||||
end
|
||||
|
||||
def type_for(field)
|
||||
available_filters[field][:type] if available_filters.has_key?(field)
|
||||
end
|
||||
|
||||
def operator_for(field)
|
||||
has_filter?(field) ? filters[field][:operator] : nil
|
||||
|
@ -229,6 +234,10 @@ class Query < ActiveRecord::Base
|
|||
has_filter?(field) ? filters[field][:values] : nil
|
||||
end
|
||||
|
||||
def value_for(field, index=0)
|
||||
(values_for(field) || [])[index]
|
||||
end
|
||||
|
||||
def label_for(field)
|
||||
label = available_filters[field][:name] if available_filters.has_key?(field)
|
||||
label ||= field.gsub(/\_id$/, "")
|
||||
|
@ -529,11 +538,15 @@ class Query < ActiveRecord::Base
|
|||
sql = ''
|
||||
case operator
|
||||
when "="
|
||||
if value.present?
|
||||
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
||||
if [:date, :date_past].include?(type_for(field))
|
||||
sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil))
|
||||
else
|
||||
# empty set of allowed values produces no result
|
||||
sql = "0=1"
|
||||
if value.any?
|
||||
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
||||
else
|
||||
# IN an empty set
|
||||
sql = "0=1"
|
||||
end
|
||||
end
|
||||
when "!"
|
||||
if value.present?
|
||||
|
@ -549,42 +562,58 @@ class Query < ActiveRecord::Base
|
|||
sql = "#{db_table}.#{db_field} IS NOT NULL"
|
||||
sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
|
||||
when ">="
|
||||
if is_custom_filter
|
||||
sql = "#{db_table}.#{db_field} != '' AND CAST(#{db_table}.#{db_field} AS decimal(60,4)) >= #{value.first.to_f}"
|
||||
if [:date, :date_past].include?(type_for(field))
|
||||
sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
|
||||
else
|
||||
sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
|
||||
if is_custom_filter
|
||||
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_i}"
|
||||
else
|
||||
sql = "#{db_table}.#{db_field} >= #{value.first.to_i}"
|
||||
end
|
||||
end
|
||||
when "<="
|
||||
if is_custom_filter
|
||||
sql = "#{db_table}.#{db_field} != '' AND CAST(#{db_table}.#{db_field} AS decimal(60,4)) <= #{value.first.to_f}"
|
||||
if [:date, :date_past].include?(type_for(field))
|
||||
sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
|
||||
else
|
||||
sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
|
||||
if is_custom_filter
|
||||
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_i}"
|
||||
else
|
||||
sql = "#{db_table}.#{db_field} <= #{value.first.to_i}"
|
||||
end
|
||||
end
|
||||
when "><"
|
||||
if [:date, :date_past].include?(type_for(field))
|
||||
sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
|
||||
else
|
||||
if is_custom_filter
|
||||
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_i} AND #{value[1].to_i}"
|
||||
else
|
||||
sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_i} AND #{value[1].to_i}"
|
||||
end
|
||||
end
|
||||
when "o"
|
||||
sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
|
||||
when "c"
|
||||
sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
|
||||
when ">t-"
|
||||
sql = date_range_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-"
|
||||
sql = date_range_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-"
|
||||
sql = date_range_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+"
|
||||
sql = date_range_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+"
|
||||
sql = date_range_clause(db_table, db_field, 0, value.first.to_i)
|
||||
sql = relative_date_clause(db_table, db_field, 0, value.first.to_i)
|
||||
when "t+"
|
||||
sql = date_range_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"
|
||||
sql = date_range_clause(db_table, db_field, 0, 0)
|
||||
sql = relative_date_clause(db_table, db_field, 0, 0)
|
||||
when "w"
|
||||
from = l(:general_first_day_of_week) == '7' ?
|
||||
# week starts on sunday
|
||||
((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) :
|
||||
# week starts on monday (Rails default)
|
||||
Time.now.at_beginning_of_week
|
||||
sql = "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(from), connection.quoted_date(from + 7.days)]
|
||||
first_day_of_week = l(:general_first_day_of_week).to_i
|
||||
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)
|
||||
sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6)
|
||||
when "~"
|
||||
sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
|
||||
when "!~"
|
||||
|
@ -618,16 +647,21 @@ class Query < ActiveRecord::Base
|
|||
@available_filters["cf_#{field.id}"] = options.merge({ :name => field.name })
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Returns a SQL clause for a date or datetime field.
|
||||
def date_range_clause(table, field, from, to)
|
||||
def date_clause(table, field, from, to)
|
||||
s = []
|
||||
if from
|
||||
s << ("#{table}.#{field} > '%s'" % [connection.quoted_date((Date.yesterday + from).to_time.end_of_day)])
|
||||
s << ("#{table}.#{field} > '%s'" % [connection.quoted_date((from - 1).to_time.end_of_day)])
|
||||
end
|
||||
if to
|
||||
s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date((Date.today + to).to_time.end_of_day)])
|
||||
s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to.to_time.end_of_day)])
|
||||
end
|
||||
s.join(' AND ')
|
||||
end
|
||||
|
||||
# Returns a SQL clause for a date or datetime field using relative dates.
|
||||
def relative_date_clause(table, field, days_from, days_to)
|
||||
date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -22,16 +22,33 @@ function toggle_filter(field) {
|
|||
if (check_box.checked) {
|
||||
Element.show("operators_" + field);
|
||||
Form.Element.enable("operators_" + field);
|
||||
Form.Element.enable("values_" + field);
|
||||
toggle_operator(field);
|
||||
} else {
|
||||
Element.hide("operators_" + field);
|
||||
Element.hide("div_values_" + field);
|
||||
Form.Element.disable("operators_" + field);
|
||||
Form.Element.disable("values_" + field);
|
||||
enableValues(field, []);
|
||||
}
|
||||
}
|
||||
|
||||
function enableValues(field, indexes) {
|
||||
var f = $$(".values_" + field);
|
||||
for(var i=0;i<f.length;i++) {
|
||||
if (indexes.include(i)) {
|
||||
Form.Element.enable(f[i]);
|
||||
f[i].up('span').show();
|
||||
} else {
|
||||
f[i].value = '';
|
||||
Form.Element.disable(f[i]);
|
||||
f[i].up('span').hide();
|
||||
}
|
||||
}
|
||||
if (indexes.length > 0) {
|
||||
Element.show("div_values_" + field);
|
||||
} else {
|
||||
Element.hide("div_values_" + field);
|
||||
}
|
||||
}
|
||||
|
||||
function toggle_operator(field) {
|
||||
operator = $("operators_" + field);
|
||||
switch (operator.value) {
|
||||
|
@ -41,21 +58,32 @@ function toggle_operator(field) {
|
|||
case "w":
|
||||
case "o":
|
||||
case "c":
|
||||
Element.hide("div_values_" + field);
|
||||
enableValues(field, []);
|
||||
break;
|
||||
case "><":
|
||||
enableValues(field, [0,1]);
|
||||
break;
|
||||
case "<t+":
|
||||
case ">t+":
|
||||
case "t+":
|
||||
case ">t-":
|
||||
case "<t-":
|
||||
case "t-":
|
||||
enableValues(field, [2]);
|
||||
break;
|
||||
default:
|
||||
Element.show("div_values_" + field);
|
||||
enableValues(field, [0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function toggle_multi_select(field) {
|
||||
select = $('values_' + field);
|
||||
if (select.multiple == true) {
|
||||
select.multiple = false;
|
||||
} else {
|
||||
select.multiple = true;
|
||||
}
|
||||
function toggle_multi_select(el) {
|
||||
var select = $(el);
|
||||
if (select.multiple == true) {
|
||||
select.multiple = false;
|
||||
} else {
|
||||
select.multiple = true;
|
||||
}
|
||||
}
|
||||
|
||||
function apply_filters_observer() {
|
||||
|
@ -92,16 +120,19 @@ Event.observe(document,"dom:loaded", apply_filters_observer);
|
|||
<div id="div_values_<%= field %>" style="display:none;">
|
||||
<% case options[:type]
|
||||
when :list, :list_optional, :list_status, :list_subprojects %>
|
||||
<select <%= "multiple=true" if query.values_for(field) and query.values_for(field).length > 1 %> name="v[<%= field %>][]" id="values_<%= field %>" class="select-small" style="vertical-align: top;">
|
||||
<%= options_for_select options[:values], query.values_for(field) %>
|
||||
</select>
|
||||
<%= link_to_function image_tag('bullet_toggle_plus.png'), "toggle_multi_select('#{field}');", :style => "vertical-align: bottom;" %>
|
||||
<span class="span_values_<%= field %>">
|
||||
<%= select_tag "v[#{field}][]", options_for_select(options[:values], query.values_for(field)), :class => "values_#{field}", :id => "values_#{field}_1", :multiple => (query.values_for(field) && query.values_for(field).length > 1) %>
|
||||
<%= link_to_function image_tag('bullet_toggle_plus.png'), "toggle_multi_select('values_#{field}_1');", :style => "vertical-align: bottom;" %>
|
||||
</span>
|
||||
<% when :date, :date_past %>
|
||||
<%= text_field_tag "v[#{field}][]", query.values_for(field).try(:first), :id => "values_#{field}", :size => 3, :class => "select-small" %> <%= l(:label_day_plural) %>
|
||||
<span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :size => 10, :class => "values_#{field}", :id => "values_#{field}_1" %> <%= calendar_for "values_#{field}_1" %></span>
|
||||
<span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field, 1), :size => 10, :class => "values_#{field}", :id => "values_#{field}_2" %> <%= calendar_for "values_#{field}_2" %></span>
|
||||
<span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :size => 3, :class => "values_#{field}" %> <%= l(:label_day_plural) %></span>
|
||||
<% when :string, :text %>
|
||||
<%= text_field_tag "v[#{field}][]", query.values_for(field).try(:first), :id => "values_#{field}", :size => 30, :class => "select-small" %>
|
||||
<span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :class => "values_#{field}", :id => "values_#{field}", :size => 30 %></span>
|
||||
<% when :integer %>
|
||||
<%= text_field_tag "v[#{field}][]", query.values_for(field).try(:first), :id => "values_#{field}", :size => 3, :class => "select-small" %>
|
||||
<span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :class => "values_#{field}", :id => "values_#{field}_1", :size => 3 %></span>
|
||||
<span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field, 1), :class => "values_#{field}", :id => "values_#{field}_2", :size => 3 %></span>
|
||||
<% end %>
|
||||
</div>
|
||||
<script type="text/javascript">toggle_filter('<%= field %>');</script>
|
||||
|
|
|
@ -610,6 +610,7 @@ en:
|
|||
label_in_more_than: in more than
|
||||
label_greater_or_equal: '>='
|
||||
label_less_or_equal: '<='
|
||||
label_between: "between"
|
||||
label_in: in
|
||||
label_today: today
|
||||
label_all_time: all time
|
||||
|
|
|
@ -104,6 +104,34 @@ class QueryTest < ActiveSupport::TestCase
|
|||
find_issues_with_query(query)
|
||||
end
|
||||
|
||||
def test_operator_date_equals
|
||||
query = Query.new(:name => '_')
|
||||
query.add_filter('due_date', '=', ['2011-07-10'])
|
||||
assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
|
||||
find_issues_with_query(query)
|
||||
end
|
||||
|
||||
def test_operator_date_lesser_than
|
||||
query = Query.new(:name => '_')
|
||||
query.add_filter('due_date', '<=', ['2011-07-10'])
|
||||
assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
|
||||
find_issues_with_query(query)
|
||||
end
|
||||
|
||||
def test_operator_date_greater_than
|
||||
query = Query.new(:name => '_')
|
||||
query.add_filter('due_date', '>=', ['2011-07-10'])
|
||||
assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement
|
||||
find_issues_with_query(query)
|
||||
end
|
||||
|
||||
def test_operator_date_between
|
||||
query = Query.new(:name => '_')
|
||||
query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10'])
|
||||
assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
|
||||
find_issues_with_query(query)
|
||||
end
|
||||
|
||||
def test_operator_in_more_than
|
||||
Issue.find(7).update_attribute(:due_date, (Date.today + 15))
|
||||
query = Query.new(:project => Project.find(1), :name => '_')
|
||||
|
|
Loading…
Reference in New Issue