Skip to content

Commit

Permalink
imlement allow_nil and allow_blank
Browse files Browse the repository at this point in the history
  • Loading branch information
dubadub committed Jun 6, 2019
1 parent e4ab4b0 commit 4b5aa63
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/filtered/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ def field(field_name, options = {}, &block)
->(value) { -> { where(field_name => value) } }
end

raise Error, "'if' can't be used with 'allow_nil' or 'allow_blank'" if options[:if] && (options[:allow_nil] || options[:allow_blank])

field_definition.acceptance_computer = if options[:if]
options[:if]
else
->(value) { !value.nil? && value != "" }
->(value) { (options[:allow_nil] || !value.nil?) && (options[:allow_blank] || value != "") }
end

field_definition.default_computer = if options[:default].is_a?(Proc)
Expand Down
26 changes: 21 additions & 5 deletions spec/filtered_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,30 @@ class MyFilter < Filtered::Base


describe "allow_blank: ..." do
xit "supports 'allow_blank: true'" do
class MyFilter < Filtered::Base
field :status, allow_blank: true
context "when 'allow_blank: true'" do
it "allows blank values in query" do
class MyFilter < Filtered::Base
field :status, allow_blank: true
end

filter = MyFilter.new(status: "")

expect(filter.to_hash).to eq(status: "")
end
end
end

describe "allow_nil: ..." do
context "when 'allow_nil: true'" do
it "allows nil values in query" do
class MyFilter < Filtered::Base
field :status, allow_nil: true
end

filter = MyFilter.new(status: "")
filter = MyFilter.new(status: nil)

expect(filter.to_hash).to eq(status: "")
expect(filter.to_hash).to eq(status: nil)
end
end
end

Expand Down

0 comments on commit 4b5aa63

Please sign in to comment.