From 0e3d30e6bc86e3aea1298aa111c9cf0900da5165 Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Wed, 11 Jan 2017 11:46:47 +0200 Subject: [PATCH 1/7] Provide support for BigDecimal use with float_filter --- lib/mutations/float_filter.rb | 4 ++-- spec/float_filter_spec.rb | 10 +++++++++- spec/spec_helper.rb | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/mutations/float_filter.rb b/lib/mutations/float_filter.rb index 2c2b7c2..8c3a050 100644 --- a/lib/mutations/float_filter.rb +++ b/lib/mutations/float_filter.rb @@ -13,7 +13,7 @@ def filter(data) return [nil, nil] if options[:nils] return [nil, :nils] end - + # Now check if it's empty: return [data, :empty] if data == "" @@ -21,7 +21,7 @@ def filter(data) if !data.is_a?(Float) if data.is_a?(String) && data =~ /^[-+]?\d*\.?\d+/ data = data.to_f - elsif data.is_a?(Fixnum) + elsif data.is_a?(Numeric) data = data.to_f else return [data, :float] diff --git a/spec/float_filter_spec.rb b/spec/float_filter_spec.rb index 42be4c4..b5cdbe1 100644 --- a/spec/float_filter_spec.rb +++ b/spec/float_filter_spec.rb @@ -65,7 +65,7 @@ assert_equal nil, filtered assert_equal nil, errors end - + it "considers empty strings to be empty" do f = Mutations::FloatFilter.new filtered, errors = f.filter("") @@ -100,4 +100,12 @@ assert_equal nil, errors end + it 'allows BigDecimal as input' do + f = Mutations::FloatFilter.new + value = BigDecimal.new(5) + filtered, errors = f.filter(value) + assert_equal 5, filtered + assert_equal nil, errors + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 31a38b3..6d8905d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ require 'minitest/unit' require 'minitest/autorun' +require 'bigdecimal' require 'pp' $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) From 3fb3327ed05db27cd96863dc3accc6e759dee7bd Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Thu, 26 Jan 2017 09:24:22 +0200 Subject: [PATCH 2/7] support empty as nil for floats --- lib/mutations/float_filter.rb | 5 +++++ lib/mutations/version.rb | 2 +- spec/float_filter_spec.rb | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/mutations/float_filter.rb b/lib/mutations/float_filter.rb index 8c3a050..9224466 100644 --- a/lib/mutations/float_filter.rb +++ b/lib/mutations/float_filter.rb @@ -8,6 +8,11 @@ class FloatFilter < AdditionalFilter def filter(data) + # change empty to nil if required + if data == "" && options[:empty_as_nil] + data = nil + end + # Handle nil case if data.nil? return [nil, nil] if options[:nils] diff --git a/lib/mutations/version.rb b/lib/mutations/version.rb index 5fe7963..9a43763 100644 --- a/lib/mutations/version.rb +++ b/lib/mutations/version.rb @@ -1,3 +1,3 @@ module Mutations - VERSION = "0.8.0" + VERSION = "0.8.1" end diff --git a/spec/float_filter_spec.rb b/spec/float_filter_spec.rb index b5cdbe1..df310ea 100644 --- a/spec/float_filter_spec.rb +++ b/spec/float_filter_spec.rb @@ -72,6 +72,13 @@ assert_equal :empty, errors end + it "it allows empty strings as nil" do + f = Mutations::FloatFilter.new(empty_as_nil: true, nils: true) + filtered, errors = f.filter("") + assert_equal nil, filtered + assert_equal nil, errors + end + it "considers low numbers invalid" do f = Mutations::FloatFilter.new(:min => 10) filtered, errors = f.filter(3) From 33f2e4a0184614637f6e3556d18d29dfd5cedd60 Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Thu, 26 Jan 2017 09:25:37 +0200 Subject: [PATCH 3/7] supply default --- lib/mutations/float_filter.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/mutations/float_filter.rb b/lib/mutations/float_filter.rb index 9224466..47910ff 100644 --- a/lib/mutations/float_filter.rb +++ b/lib/mutations/float_filter.rb @@ -1,9 +1,10 @@ module Mutations class FloatFilter < AdditionalFilter @default_options = { - :nils => false, # true allows an explicit nil to be valid. Overrides any other options - :min => nil, # lowest value, inclusive - :max => nil # highest value, inclusive + :nils => false, # true allows an explicit nil to be valid. Overrides any other options + :empty_as_nil => false, # true will interpret empty strings as nil + :min => nil, # lowest value, inclusive + :max => nil # highest value, inclusive } def filter(data) From 7e1dfbecf088aa7808e8310e7d091b30721f9726 Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Thu, 26 Jan 2017 09:28:34 +0200 Subject: [PATCH 4/7] use the same naming as IntegerFilter --- lib/mutations/float_filter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mutations/float_filter.rb b/lib/mutations/float_filter.rb index 47910ff..7bbb63d 100644 --- a/lib/mutations/float_filter.rb +++ b/lib/mutations/float_filter.rb @@ -2,7 +2,7 @@ module Mutations class FloatFilter < AdditionalFilter @default_options = { :nils => false, # true allows an explicit nil to be valid. Overrides any other options - :empty_as_nil => false, # true will interpret empty strings as nil + :empty_is_nil => false, # if true, treat empty string as if it were nil :min => nil, # lowest value, inclusive :max => nil # highest value, inclusive } @@ -10,7 +10,7 @@ class FloatFilter < AdditionalFilter def filter(data) # change empty to nil if required - if data == "" && options[:empty_as_nil] + if data == "" && options[:empty_is_nil] data = nil end From 04036d585a8e83de6cac70c3bfc27deb474caf5d Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Thu, 26 Jan 2017 09:46:15 +0200 Subject: [PATCH 5/7] fix spec --- spec/float_filter_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/float_filter_spec.rb b/spec/float_filter_spec.rb index df310ea..28b3dbe 100644 --- a/spec/float_filter_spec.rb +++ b/spec/float_filter_spec.rb @@ -73,7 +73,7 @@ end it "it allows empty strings as nil" do - f = Mutations::FloatFilter.new(empty_as_nil: true, nils: true) + f = Mutations::FloatFilter.new(empty_is_nil: true, nils: true) filtered, errors = f.filter("") assert_equal nil, filtered assert_equal nil, errors From 60e39837eb8ecd5dd796752614fd016aa2916ffc Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Thu, 26 Jan 2017 09:48:02 +0200 Subject: [PATCH 6/7] Provide empty_is_nil support for model filters --- lib/mutations/model_filter.rb | 15 ++++++++++----- spec/model_filter_spec.rb | 18 +++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/mutations/model_filter.rb b/lib/mutations/model_filter.rb index 5c66f6d..96d3cc5 100644 --- a/lib/mutations/model_filter.rb +++ b/lib/mutations/model_filter.rb @@ -1,10 +1,11 @@ module Mutations class ModelFilter < InputFilter @default_options = { - :nils => false, # true allows an explicit nil to be valid. Overrides any other options - :class => nil, # default is the attribute name.to_s.camelize.constantize. This overrides it with class or class.constantize - :builder => nil, # Could be a class or a string which will be constantized. If present, and a hash is passed, then we use that to construct a model - :new_records => false, # If false, unsaved AR records are not valid. Things that don't respond to new_record? are valid. true: anything is valid + :nils => false, # true allows an explicit nil to be valid. Overrides any other options + :empty_is_nil => false, # if true, treat empty string as if it were nil + :class => nil, # default is the attribute name.to_s.camelize.constantize. This overrides it with class or class.constantize + :builder => nil, # Could be a class or a string which will be constantized. If present, and a hash is passed, then we use that to construct a model + :new_records => false, # If false, unsaved AR records are not valid. Things that don't respond to new_record? are valid. true: anything is valid } def initialize(name, opts = {}) @@ -25,7 +26,7 @@ def initialize_constants! true end - + unless Mutations.cache_constants? options[:class] = options[:class].to_s.constantize if options[:class] options[:builder] = options[:builder].to_s.constantize if options[:builder] @@ -35,6 +36,10 @@ def initialize_constants! def filter(data) initialize_constants! + if data == "" && options[:empty_is_nil] + data = nil + end + # Handle nil case if data.nil? return [nil, nil] if options[:nils] diff --git a/spec/model_filter_spec.rb b/spec/model_filter_spec.rb index 173caf9..f8bcd50 100644 --- a/spec/model_filter_spec.rb +++ b/spec/model_filter_spec.rb @@ -60,7 +60,15 @@ def new_record? assert_equal nil, filtered assert_equal nil, errors end - + + it 'treats empty as nil with the option empty' do + # This is useful for Graphql where every nil gets made into empty strings + f = Mutations::ModelFilter.new(:simple_model, nils: true, empty_is_nil: true) + filtered, errors = f.filter("") + assert_equal nil, filtered + assert_equal nil, errors + end + it "will re-constantize if cache_constants is false" do was = Mutations.cache_constants? Mutations.cache_constants = false @@ -69,16 +77,16 @@ def new_record? filtered, errors = f.filter(m) assert_equal m, filtered assert_equal nil, errors - + Object.send(:remove_const, 'SimpleModel') - + class SimpleModel; end - + m = SimpleModel.new filtered, errors = f.filter(m) assert_equal m, filtered assert_equal nil, errors - + Mutations.cache_constants = was end From 4f235b70fca68897b72a929a6073db584f17b4f0 Mon Sep 17 00:00:00 2001 From: Tyrone Wilson Date: Thu, 26 Jan 2017 10:43:19 +0200 Subject: [PATCH 7/7] Set empty_is_nil by default --- lib/mutations/float_filter.rb | 2 +- lib/mutations/integer_filter.rb | 2 +- lib/mutations/model_filter.rb | 2 +- spec/hash_filter_spec.rb | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/mutations/float_filter.rb b/lib/mutations/float_filter.rb index 7bbb63d..a1e9d8e 100644 --- a/lib/mutations/float_filter.rb +++ b/lib/mutations/float_filter.rb @@ -2,7 +2,7 @@ module Mutations class FloatFilter < AdditionalFilter @default_options = { :nils => false, # true allows an explicit nil to be valid. Overrides any other options - :empty_is_nil => false, # if true, treat empty string as if it were nil + :empty_is_nil => true, # if true, treat empty string as if it were nil :min => nil, # lowest value, inclusive :max => nil # highest value, inclusive } diff --git a/lib/mutations/integer_filter.rb b/lib/mutations/integer_filter.rb index f9b341d..9a8f86c 100644 --- a/lib/mutations/integer_filter.rb +++ b/lib/mutations/integer_filter.rb @@ -2,7 +2,7 @@ module Mutations class IntegerFilter < AdditionalFilter @default_options = { :nils => false, # true allows an explicit nil to be valid. Overrides any other options - :empty_is_nil => false, # if true, treat empty string as if it were nil + :empty_is_nil => true, # if true, treat empty string as if it were nil :min => nil, # lowest value, inclusive :max => nil, # highest value, inclusive :in => nil, # Can be an array like %w(3 4 5) diff --git a/lib/mutations/model_filter.rb b/lib/mutations/model_filter.rb index 96d3cc5..6f35a3b 100644 --- a/lib/mutations/model_filter.rb +++ b/lib/mutations/model_filter.rb @@ -2,7 +2,7 @@ module Mutations class ModelFilter < InputFilter @default_options = { :nils => false, # true allows an explicit nil to be valid. Overrides any other options - :empty_is_nil => false, # if true, treat empty string as if it were nil + :empty_is_nil => true, # if true, treat empty string as if it were nil :class => nil, # default is the attribute name.to_s.camelize.constantize. This overrides it with class or class.constantize :builder => nil, # Could be a class or a string which will be constantized. If present, and a hash is passed, then we use that to construct a model :new_records => false, # If false, unsaved AR records are not valid. Things that don't respond to new_record? are valid. true: anything is valid diff --git a/spec/hash_filter_spec.rb b/spec/hash_filter_spec.rb index 25d5360..b4ba69d 100644 --- a/spec/hash_filter_spec.rb +++ b/spec/hash_filter_spec.rb @@ -155,7 +155,7 @@ assert_equal ({"foo" => "bar"}), filtered assert_equal nil, errors end - + it "bar is optional -- discards empty if it needs to be stripped" do hf = Mutations::HashFilter.new do required do @@ -170,7 +170,7 @@ assert_equal ({"foo" => "bar"}), filtered assert_equal nil, errors end - + it "bar is optional -- don't discard empty if it's spaces but stripping is off" do hf = Mutations::HashFilter.new do required do