diff --git a/app/models/cost_query/operator.rb b/app/models/cost_query/operator.rb index d9ce5ee30e..307be103f1 100644 --- a/app/models/cost_query/operator.rb +++ b/app/models/cost_query/operator.rb @@ -1,5 +1,6 @@ class CostQuery::Operator include CostQuery::QueryUtils + include CostQuery::Validation ############################################################################################# # Wrapped so we can place this at the top of the file. @@ -165,22 +166,8 @@ class CostQuery::Operator return query if value.to_s.empty? "<".to_operator.modify query, field, quoted_date(value) end - - def validate(*values) - @errors.clear - values.all? do |vals| - vals = vals.is_a?(Array) ? vals : [vals] - vals.all? do |val| - begin - !!val.to_dateish - rescue ArgumentError - validate(vals - [val]) - @errors << "\'#{val}\' is not a valid date!" - false - end - end - end - end + + include CostQuery::Validation::DateValidation end new ">d", :label => :label_greater_or_equal do @@ -189,21 +176,7 @@ class CostQuery::Operator ">".to_operator.modify query, field, quoted_date(value) end - def validate(*values) - @errors.clear - values.all? do |vals| - vals = vals.is_a?(Array) ? vals : [vals] - vals.all? do |val| - begin - !!val.to_dateish - rescue ArgumentError - validate(vals - [val]) - @errors << "\'#{val}\' is not a valid date!" - false - end - end - end - end + include CostQuery::Validation::DateValidation end new "<>d", :label => :label_between do @@ -213,21 +186,7 @@ class CostQuery::Operator query end - def validate(*values) - @errors.clear - values.all? do |vals| - vals = vals.is_a?(Array) ? vals : [vals] - vals.all? do |val| - begin - !!val.to_dateish - rescue ArgumentError - validate(vals - [val]) - @errors << "\'#{val}\' is not a valid date!" - false - end - end - end - end + include CostQuery::Validation::DateValidation end new "=d", :label => :label_date_on do @@ -236,21 +195,7 @@ class CostQuery::Operator "=".to_operator.modify query, field, quoted_date(value) end - def validate(*values) - @errors.clear - values.all? do |vals| - vals = vals.is_a?(Array) ? vals : [vals] - vals.all? do |val| - begin - !!val.to_dateish - rescue ArgumentError - validate(vals - [val]) - @errors << "\'#{val}\' is not a valid date!" - false - end - end - end - end + include CostQuery::Validation::DateValidation end new "=_child_projects", :label => :label_is_project_with_subprojects do @@ -332,21 +277,15 @@ class CostQuery::Operator end attr_reader :name - attr_reader :errors def initialize(name, values = {}, &block) @name = name.to_s - @errors = [] values.each do |key, value| metaclass.class_eval { define_method(key) { value } } end metaclass.class_eval(&block) if block end - def validate(*values) - true - end - def to_operator self end diff --git a/app/models/cost_query/validation.rb b/app/models/cost_query/validation.rb new file mode 100644 index 0000000000..6a941d0d5a --- /dev/null +++ b/app/models/cost_query/validation.rb @@ -0,0 +1,31 @@ +module CostQuery::Validation + module CostQuery::Validation::DateValidation + include CostQuery::Validation + + def validate(*values) + errors.clear + values.all? do |vals| + vals = vals.is_a?(Array) ? vals : [vals] + vals.all? do |val| + begin + !!val.to_dateish + rescue ArgumentError + validate(vals - [val]) + errors << "\'#{val}\' is not a valid date!" + false + end + end + end + end + end + + def validate(*values) + true + end + + def errors + @errors ||= [] + @errors + end + +end \ No newline at end of file