[#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_all,
|
||||||
">=" => :label_greater_or_equal,
|
">=" => :label_greater_or_equal,
|
||||||
"<=" => :label_less_or_equal,
|
"<=" => :label_less_or_equal,
|
||||||
|
"><" => :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,
|
"t+" => :label_in,
|
||||||
|
@ -50,8 +51,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", "w", ">t-", "<t-", "t-" ],
|
||||||
:date_past => [ ">t-", "<t-", "t-", "t", "w" ],
|
:date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w" ],
|
||||||
:string => [ "=", "~", "!", "!~" ],
|
:string => [ "=", "~", "!", "!~" ],
|
||||||
:text => [ "~", "!~" ],
|
:text => [ "~", "!~" ],
|
||||||
:integer => [ "=", ">=", "<=", "!*", "*" ] }
|
:integer => [ "=", ">=", "<=", "!*", "*" ] }
|
||||||
|
@ -189,7 +190,7 @@ class Query < ActiveRecord::Base
|
||||||
|
|
||||||
def add_filter(field, operator, values)
|
def add_filter(field, operator, values)
|
||||||
# values must be an array
|
# 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
|
# check if field is defined as an available filter
|
||||||
if available_filters.has_key? field
|
if available_filters.has_key? field
|
||||||
filter_options = available_filters[field]
|
filter_options = available_filters[field]
|
||||||
|
@ -198,7 +199,7 @@ class Query < ActiveRecord::Base
|
||||||
# allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
|
# 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
|
# filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
|
||||||
#end
|
#end
|
||||||
filters[field] = {:operator => operator, :values => values }
|
filters[field] = {:operator => operator, :values => (values || ['']) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -210,9 +211,9 @@ class Query < ActiveRecord::Base
|
||||||
|
|
||||||
# Add multiple filters using +add_filter+
|
# Add multiple filters using +add_filter+
|
||||||
def add_filters(fields, operators, values)
|
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|
|
fields.each do |field|
|
||||||
add_filter(field, operators[field], values[field])
|
add_filter(field, operators[field], values && values[field])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -221,6 +222,10 @@ class Query < ActiveRecord::Base
|
||||||
filters and filters[field]
|
filters and filters[field]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def type_for(field)
|
||||||
|
available_filters[field][:type] if available_filters.has_key?(field)
|
||||||
|
end
|
||||||
|
|
||||||
def operator_for(field)
|
def operator_for(field)
|
||||||
has_filter?(field) ? filters[field][:operator] : nil
|
has_filter?(field) ? filters[field][:operator] : nil
|
||||||
end
|
end
|
||||||
|
@ -229,6 +234,10 @@ class Query < ActiveRecord::Base
|
||||||
has_filter?(field) ? filters[field][:values] : nil
|
has_filter?(field) ? filters[field][:values] : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def value_for(field, index=0)
|
||||||
|
(values_for(field) || [])[index]
|
||||||
|
end
|
||||||
|
|
||||||
def label_for(field)
|
def label_for(field)
|
||||||
label = available_filters[field][:name] if available_filters.has_key?(field)
|
label = available_filters[field][:name] if available_filters.has_key?(field)
|
||||||
label ||= field.gsub(/\_id$/, "")
|
label ||= field.gsub(/\_id$/, "")
|
||||||
|
@ -529,12 +538,16 @@ class Query < ActiveRecord::Base
|
||||||
sql = ''
|
sql = ''
|
||||||
case operator
|
case operator
|
||||||
when "="
|
when "="
|
||||||
if value.present?
|
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
|
||||||
|
if value.any?
|
||||||
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
||||||
else
|
else
|
||||||
# empty set of allowed values produces no result
|
# IN an empty set
|
||||||
sql = "0=1"
|
sql = "0=1"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
when "!"
|
when "!"
|
||||||
if value.present?
|
if value.present?
|
||||||
sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
|
sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
|
||||||
|
@ -549,42 +562,58 @@ class Query < ActiveRecord::Base
|
||||||
sql = "#{db_table}.#{db_field} IS NOT NULL"
|
sql = "#{db_table}.#{db_field} IS NOT NULL"
|
||||||
sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
|
sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
|
||||||
when ">="
|
when ">="
|
||||||
if is_custom_filter
|
if [:date, :date_past].include?(type_for(field))
|
||||||
sql = "#{db_table}.#{db_field} != '' AND CAST(#{db_table}.#{db_field} AS decimal(60,4)) >= #{value.first.to_f}"
|
sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
|
||||||
else
|
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
|
end
|
||||||
when "<="
|
when "<="
|
||||||
if is_custom_filter
|
if [:date, :date_past].include?(type_for(field))
|
||||||
sql = "#{db_table}.#{db_field} != '' AND CAST(#{db_table}.#{db_field} AS decimal(60,4)) <= #{value.first.to_f}"
|
sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
|
||||||
else
|
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
|
end
|
||||||
when "o"
|
when "o"
|
||||||
sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
|
sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
|
||||||
when "c"
|
when "c"
|
||||||
sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
|
sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
|
||||||
when ">t-"
|
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-"
|
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-"
|
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+"
|
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+"
|
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+"
|
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"
|
when "t"
|
||||||
sql = date_range_clause(db_table, db_field, 0, 0)
|
sql = relative_date_clause(db_table, db_field, 0, 0)
|
||||||
when "w"
|
when "w"
|
||||||
from = l(:general_first_day_of_week) == '7' ?
|
first_day_of_week = l(:general_first_day_of_week).to_i
|
||||||
# week starts on sunday
|
day_of_week = Date.today.cwday
|
||||||
((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) :
|
days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
|
||||||
# week starts on monday (Rails default)
|
sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6)
|
||||||
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)]
|
|
||||||
when "~"
|
when "~"
|
||||||
sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
|
sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
|
||||||
when "!~"
|
when "!~"
|
||||||
|
@ -620,14 +649,19 @@ class Query < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a SQL clause for a date or datetime field.
|
# 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 = []
|
s = []
|
||||||
if from
|
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
|
end
|
||||||
if to
|
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
|
end
|
||||||
s.join(' AND ')
|
s.join(' AND ')
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -22,13 +22,30 @@ function toggle_filter(field) {
|
||||||
if (check_box.checked) {
|
if (check_box.checked) {
|
||||||
Element.show("operators_" + field);
|
Element.show("operators_" + field);
|
||||||
Form.Element.enable("operators_" + field);
|
Form.Element.enable("operators_" + field);
|
||||||
Form.Element.enable("values_" + field);
|
|
||||||
toggle_operator(field);
|
toggle_operator(field);
|
||||||
} else {
|
} else {
|
||||||
Element.hide("operators_" + field);
|
Element.hide("operators_" + field);
|
||||||
Element.hide("div_values_" + field);
|
|
||||||
Form.Element.disable("operators_" + 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,16 +58,27 @@ function toggle_operator(field) {
|
||||||
case "w":
|
case "w":
|
||||||
case "o":
|
case "o":
|
||||||
case "c":
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
Element.show("div_values_" + field);
|
enableValues(field, [0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggle_multi_select(field) {
|
function toggle_multi_select(el) {
|
||||||
select = $('values_' + field);
|
var select = $(el);
|
||||||
if (select.multiple == true) {
|
if (select.multiple == true) {
|
||||||
select.multiple = false;
|
select.multiple = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,16 +120,19 @@ Event.observe(document,"dom:loaded", apply_filters_observer);
|
||||||
<div id="div_values_<%= field %>" style="display:none;">
|
<div id="div_values_<%= field %>" style="display:none;">
|
||||||
<% case options[:type]
|
<% case options[:type]
|
||||||
when :list, :list_optional, :list_status, :list_subprojects %>
|
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;">
|
<span class="span_values_<%= field %>">
|
||||||
<%= options_for_select options[:values], query.values_for(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) %>
|
||||||
</select>
|
<%= link_to_function image_tag('bullet_toggle_plus.png'), "toggle_multi_select('values_#{field}_1');", :style => "vertical-align: bottom;" %>
|
||||||
<%= link_to_function image_tag('bullet_toggle_plus.png'), "toggle_multi_select('#{field}');", :style => "vertical-align: bottom;" %>
|
</span>
|
||||||
<% when :date, :date_past %>
|
<% 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 %>
|
<% 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 %>
|
<% 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 %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<script type="text/javascript">toggle_filter('<%= field %>');</script>
|
<script type="text/javascript">toggle_filter('<%= field %>');</script>
|
||||||
|
|
|
@ -610,6 +610,7 @@ en:
|
||||||
label_in_more_than: in more than
|
label_in_more_than: in more than
|
||||||
label_greater_or_equal: '>='
|
label_greater_or_equal: '>='
|
||||||
label_less_or_equal: '<='
|
label_less_or_equal: '<='
|
||||||
|
label_between: "between"
|
||||||
label_in: in
|
label_in: in
|
||||||
label_today: today
|
label_today: today
|
||||||
label_all_time: all time
|
label_all_time: all time
|
||||||
|
|
|
@ -104,6 +104,34 @@ class QueryTest < ActiveSupport::TestCase
|
||||||
find_issues_with_query(query)
|
find_issues_with_query(query)
|
||||||
end
|
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
|
def test_operator_in_more_than
|
||||||
Issue.find(7).update_attribute(:due_date, (Date.today + 15))
|
Issue.find(7).update_attribute(:due_date, (Date.today + 15))
|
||||||
query = Query.new(:project => Project.find(1), :name => '_')
|
query = Query.new(:project => Project.find(1), :name => '_')
|
||||||
|
|
Loading…
Reference in New Issue