Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Proc sanitizer with nil safe return #1

Merged
merged 1 commit into from

2 participants

@zghorn

This fixes a bug which would not allow a sanitizer as a Proc to return a nil value without causing an exception

@kevintom kevintom merged commit 5205f42 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 14, 2012
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 10 deletions.
  1. +6 −4 lib/sequel_sanitize.rb
  2. +14 −6 spec/sequel_sanitize_spec.rb
View
10 lib/sequel_sanitize.rb
@@ -1,7 +1,7 @@
module Sequel
module Plugins
- # The Sanitize plugin basically does a 'before_filter' on
- # specified fields, if no sanitizer is specified the default
+ # The Sanitize plugin basically does a 'before_filter' on
+ # specified fields, if no sanitizer is specified the default
# one is used
#
module Sanitize
@@ -14,8 +14,10 @@ def self.configure(model, opts={})
define_method("#{f}=") do |value|
sanitizer = self.class.sanitize_options[:field_sanitize][f]
do_downcase = self.class.sanitize_options[:field_downcase][f]
- sanitized = sanitizer.call(value) if sanitizer.respond_to?(:call)
- sanitized ||= self.send(sanitizer, value) if sanitizer
+ if sanitizer
+ sanitized = sanitizer.call(value) if sanitizer.respond_to?(:call)
+ sanitized = self.send(sanitizer, value) if sanitizer.is_a? Symbol
+ end
sanitized = sanitized.downcase if do_downcase and sanitized.respond_to?(:downcase)
super(sanitized)
end
View
20 spec/sequel_sanitize_spec.rb
@@ -12,10 +12,10 @@ class Item < Sequel::Model; end
it "should be loaded using Model.plugin" do
Item.plugins.should include(Sequel::Plugins::Sanitize)
end
-
+
it "should require a field array" do
class Item2 < Sequel::Model; end
- lambda {Item2.plugin :sanitize}.should raise_error(ArgumentError, ":fields must be a non-empty array")
+ lambda {Item2.plugin :sanitize}.should raise_error(ArgumentError, ":fields must be a non-empty array")
end
it "should require a sanitizer to be a symbol or callable" do
@@ -27,8 +27,16 @@ class Item2 < Sequel::Model; end
i = Item.new(:name => " Kevin ")
i.name.should eql "Kevin"
end
+
+ it 'should allow a Proc as sanitizer to return nil' do
+ class Item < Sequel::Model
+ plugin :sanitize, :fields => [:name], :sanitizer => Proc.new{|s| nil}
+ end
+ i = Item.new(:name => " ")
+ i.name.should eql nil
+ end
end
-
+
describe "downcase option" do
before(:each) do
Item.plugin :sanitize, :fields => [:name]
@@ -47,13 +55,13 @@ class Item2 < Sequel::Model; end
Item2.plugin :sanitize, :fields => [:name, nil]
Item2.sanitize_options[:fields].should eql [:name]
end
-
+
it 'should remove duplicate fields' do
class Item2 < Sequel::Model; end
Item2.plugin :sanitize, :fields => [:name, :slug, :name]
Item2.sanitize_options[:fields].count.should eql 2
end
-
+
it 'should add to the field list on multiple calls' do
#not overwrite fields when you add more fields with different options
class Item2 < Sequel::Model; end
@@ -62,7 +70,7 @@ class Item2 < Sequel::Model; end
Item2.plugin :sanitize, :fields => [:more, :columns, :name]
Item2.sanitize_options[:fields].should eql [:name, :slug, :more, :columns]
end
-
+
it 'should aggregate the values of field options' do
class Item2 < Sequel::Model; end
Item2.plugin :sanitize, :fields => [:name, :slug]
Something went wrong with that request. Please try again.