Permalink
Browse files

Empty string fields are ignore by default, use :allow_blank => true t…

…o revert
  • Loading branch information...
1 parent a68bcb1 commit de5eba99a0f487be3a55fca6115996c31deba097 @dolzenko dolzenko committed Nov 29, 2011
@@ -11,7 +11,7 @@ def self.kind
@kind ||= name.demodulize.underscore.to_sym
end
- attr_reader :name, :operators, :attribute, :scope
+ attr_reader :name, :operators, :attribute, :scope, :options
# Accepts a name and options. Valid options are:
# * <tt>:using</tt> - a Symbol or a Proc pointing to a DB column, optional (uses name by default)
@@ -28,10 +28,11 @@ def self.kind
#
def initialize(name, options = {})
@name = name.to_s
- @attribute = options[:using] || name
- @operators = Set.new(self.class.operators & Array.wrap(options[:with]))
+ @options = options.dup
+ @attribute = @options.delete(:using) || name
+ @operators = Set.new(self.class.operators & Array.wrap(@options.delete(:with)))
@operators = Set.new(self.class.defaults) if @operators.empty?
- @scope = options[:scope]
+ @scope = @options.delete(:scope)
end
# Merge params into a relation
@@ -23,6 +23,11 @@ class Decimal < Number
class String < Base
self.operators = [:eq, :not_eq]
+
+ def _convert(v)
+ result = super
+ result.blank? && !options[:allow_blank] ? nil : result
+ end
end
class Timestamp < Base
@@ -63,5 +63,9 @@ def merge(name, operator, value)
rel.first.should == posts(:article)
end
+ it 'should store unrecognized options' do
+ Bsm::Constrainable::Field::String.new("some", :allow_blank => true).options.should == {:allow_blank=>true}
+ end
+
end
@@ -2,9 +2,7 @@
describe "Common Fields" do
- def subject(value = "")
- described_class.new(value)
- end
+ let(:subject) { described_class.new('') }
describe Bsm::Constrainable::Field::Number do
it { subject.class.should have(9).operators }
@@ -32,6 +30,11 @@ def subject(value = "")
it { subject.class.should have(2).operators }
it { subject.convert(1).should == "1" }
it { subject.convert(["a", 1]).should == ["a", "1"] }
+ it { subject.convert("").should be_nil }
+ it {
+ subject.options[:allow_blank] = true
+ subject.convert("").should == ""
+ }
end
describe Bsm::Constrainable::Field::Timestamp do

0 comments on commit de5eba9

Please sign in to comment.