diff --git a/lib/report/operator.rb b/lib/report/operator.rb index 2f8545c15b..b60914b6c2 100644 --- a/lib/report/operator.rb +++ b/lib/report/operator.rb @@ -1,6 +1,7 @@ class Report::Operator include Report::QueryUtils include Report::Validation + extend Forwardable ############################################################################################# # Wrapped so we can place this at the top of the file. @@ -8,6 +9,9 @@ class Report::Operator # Defaults defaults do + + def_delegators :'singleton_class', :enforced?, :enforce!, :enforced + def sql_operator name end @@ -183,10 +187,12 @@ class Report::Operator end end - new ">=d", :label => :label_between_now_and_date, :validate => :dates do + new ">=d", :label => :label_days_ago, :validate => :integers do + enforce! :integers + def modify(query, field, value) now = Time.now - from = value.to_dateish + from = (now - value.to_i.days).beginning_of_day '<>d'.to_operator.modify query, field, from, now end end @@ -212,6 +218,18 @@ class Report::Operator end end + def self.enforce!(type) + @enforced = type + end + + def self.enforced? + !!@enforced + end + + def self.enforced + @enforced + end + def self.new(name, values = {}, &block) all[name.to_s] ||= super end diff --git a/lib/widget/filters/operators.rb b/lib/widget/filters/operators.rb index b16710b038..3f276f58db 100644 --- a/lib/widget/filters/operators.rb +++ b/lib/widget/filters/operators.rb @@ -12,6 +12,7 @@ class Widget::Filters::Operators < Widget::Filters::Base select_box = content_tag :select, options do filter_class.available_operators.collect do |o| opts = {:value => h(o.to_s), :"data-arity" => o.arity} + opts.reverse_merge! :"data-enforced" => o.enforced if o.enforced? opts[:selected] = "selected" if filter.operator.to_s == o.to_s content_tag(:option, opts) { h(l(o.label)) } end.join.html_safe