Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide support for BigDecimal use with float_filter #106

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/mutations/float_filter.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
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_is_nil => true, # if true, treat empty string as if it were nil
:min => nil, # lowest value, inclusive
:max => nil # highest value, inclusive
}

def filter(data)

# change empty to nil if required
if data == "" && options[:empty_is_nil]
data = nil
end

# Handle nil case
if data.nil?
return [nil, nil] if options[:nils]
return [nil, :nils]
end

# Now check if it's empty:
return [data, :empty] if data == ""

# Ensure it's the correct data type (Float)
if !data.is_a?(Float)
if data.is_a?(String) && data =~ /^[-+]?\d*\.?\d+/
data = data.to_f
elsif data.is_a?(Integer)
elsif data.is_a?(Numeric)
data = data.to_f
else
return [data, :float]
Expand Down
2 changes: 1 addition & 1 deletion lib/mutations/integer_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions lib/mutations/model_filter.rb
Original file line number Diff line number Diff line change
@@ -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 => 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
}

def initialize(name, opts = {})
Expand All @@ -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]
Expand All @@ -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]
Expand Down
17 changes: 16 additions & 1 deletion spec/float_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,20 @@
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("")
assert_equal :empty, errors
end

it "it allows empty strings as nil" do
f = Mutations::FloatFilter.new(empty_is_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)
Expand Down Expand Up @@ -100,4 +107,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
4 changes: 2 additions & 2 deletions spec/hash_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 13 additions & 5 deletions spec/model_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/unit'
require 'minitest/autorun'
require 'bigdecimal'
require 'pp'

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
Expand Down