From 0f6017f4c48384f0ea9370ecbfc2776a07d58d26 Mon Sep 17 00:00:00 2001 From: "j.wollert" Date: Fri, 1 Oct 2010 11:09:18 +0000 Subject: [PATCH] filters can now be invalid given certain combinations of values and operators. by default, a filter is valid git-svn-id: https://dev.finn.de/svn/cockpit/branches/reporting_merge@1831 7926756e-e54e-46e6-9721-ed318f58905e --- app/controllers/cost_reports_controller.rb | 28 +++++++---- app/models/cost_query/filter/base.rb | 4 ++ app/models/cost_query/operator.rb | 56 ++++++++++++++++++++++ app/views/cost_reports/index.rhtml | 8 ++-- 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/app/controllers/cost_reports_controller.rb b/app/controllers/cost_reports_controller.rb index a398542c76..99cb5096c7 100644 --- a/app/controllers/cost_reports_controller.rb +++ b/app/controllers/cost_reports_controller.rb @@ -8,15 +8,18 @@ class CostReportsController < ApplicationController include ReportingHelper def index - if @query.group_bys.empty? - @table_partial = "cost_entry_table" - elsif @query.depth_of(:column) + @query.depth_of(:row) == 1 - @table_partial = "simple_cost_report_table" - else - if @query.depth_of(:column) == 0 || @query.depth_of(:row) == 0 - @query.depth_of(:column) == 0 ? @query.column(:singleton_value) : @query.row(:singleton_value) + @valid = valid_query? + if @valid + if @query.group_bys.empty? + @table_partial = "cost_entry_table" + elsif @query.depth_of(:column) + @query.depth_of(:row) == 1 + @table_partial = "simple_cost_report_table" + else + if @query.depth_of(:column) == 0 || @query.depth_of(:row) == 0 + @query.depth_of(:column) == 0 ? @query.column(:singleton_value) : @query.row(:singleton_value) + end + @table_partial = "cost_report_table" end - @table_partial = "cost_report_table" end respond_to do |format| format.html { render :layout => !request.xhr? } @@ -141,6 +144,15 @@ class CostReportsController < ApplicationController @query end + def valid_query? + return true unless @query + errornous = @query.filters ? @query.filters.select { |f| !f.valid? } : [] + @custom_error = errornous.map do |err| + "Filter #{l(err.label)} with value(s) #{err.values.reject{|val| val.empty?}.join(', ')} seems to be invalid" + end + errornous.empty? + end + ## # FIXME: Split # This method does three things: diff --git a/app/models/cost_query/filter/base.rb b/app/models/cost_query/filter/base.rb index 804a379066..3e96f6c23d 100644 --- a/app/models/cost_query/filter/base.rb +++ b/app/models/cost_query/filter/base.rb @@ -63,6 +63,10 @@ module CostQuery::Filter true end + def valid? + @operator ? @operator.validate(values) : true + end + def group_by_fields [] end diff --git a/app/models/cost_query/operator.rb b/app/models/cost_query/operator.rb index fb0a73d610..20f345bd2a 100644 --- a/app/models/cost_query/operator.rb +++ b/app/models/cost_query/operator.rb @@ -15,6 +15,10 @@ class CostQuery::Operator "%s %s '%s'" end + def validate(*values) + true + end + def modify(query, field, *values) query.where [where_clause, field, sql_operator, *values] query @@ -165,6 +169,19 @@ class CostQuery::Operator return query if value.to_s.empty? "<".to_operator.modify query, field, quoted_date(value) end + + def validate(*values) + values.all? do |vals| + vals = vals.is_a?(Array) ? vals : [vals] + vals.all? do |val| + begin + !!val.to_dateish + rescue ArgumentError + false + end + end + end + end end new ">d", :label => :label_greater_or_equal do @@ -172,6 +189,19 @@ class CostQuery::Operator return query if value.to_s.empty? ">".to_operator.modify query, field, quoted_date(value) end + + def validate(*values) + values.all? do |vals| + vals = vals.is_a?(Array) ? vals : [vals] + vals.all? do |val| + begin + !!val.to_dateish + rescue ArgumentError + false + end + end + end + end end new "<>d", :label => :label_between do @@ -180,6 +210,19 @@ class CostQuery::Operator query.where "#{field} BETWEEN '#{quoted_date from}' AND '#{quoted_date to}'" query end + + def validate(*values) + values.all? do |vals| + vals = vals.is_a?(Array) ? vals : [vals] + vals.all? do |val| + begin + !!val.to_dateish + rescue ArgumentError + false + end + end + end + end end new "=d", :label => :label_date_on do @@ -187,6 +230,19 @@ class CostQuery::Operator return query if value.to_s.empty? "=".to_operator.modify query, field, quoted_date(value) end + + def validate(*values) + values.all? do |vals| + vals = vals.is_a?(Array) ? vals : [vals] + vals.all? do |val| + begin + !!val.to_dateish + rescue ArgumentError + false + end + end + end + end end end diff --git a/app/views/cost_reports/index.rhtml b/app/views/cost_reports/index.rhtml index 7d455b8fa3..ff29881346 100644 --- a/app/views/cost_reports/index.rhtml +++ b/app/views/cost_reports/index.rhtml @@ -4,8 +4,10 @@ <%= stylesheet_link_tag 'reporting', :plugin => 'redmine_reporting' %> <% end %> -<% if @custom_error %> -
<%= @custom_error %>
+<% if @custom_error && !@custom_error.empty? %> + <% @custom_error.each do |err| %> +
<%= err %>
+ <% end %> <% end %>

<%= l(:label_cost_report) %>

@@ -53,7 +55,7 @@ <% end %> -<% if @query.result.count > 0 %> +<% if @valid and @query.result.count > 0 %> <%= render :partial => @table_partial, :locals => {:query => @query, :walker => @query.walker} %>

<%= l(:text_costs_are_rounded_note) %>