Permalink
Browse files

Added :allow_blank option to properties

  • Loading branch information...
samlown committed Aug 2, 2012
1 parent eea969a commit 56ebe61d142a9d704671d53e743c215d4d93a2b9
@@ -101,8 +101,9 @@ def collection_of(attrib, *options)
def merge_belongs_to_association_options(attrib, options = nil)
opts = {
:foreign_key => attrib.to_s.singularize + '_id',
- :class_name => attrib.to_s.singularize.camelcase,
- :proxy_name => attrib.to_s.pluralize
+ :class_name => attrib.to_s.singularize.camelcase,
+ :proxy_name => attrib.to_s.pluralize,
+ :allow_blank => false
}
opts.merge!(options) if options.is_a?(Hash)
@@ -4,7 +4,7 @@ class Property
include ::CouchRest::Model::Typecast
- attr_reader :name, :type, :type_class, :read_only, :alias, :default, :casted, :init_method, :options
+ attr_reader :name, :type, :type_class, :read_only, :alias, :default, :casted, :init_method, :options, :allow_blank
# Attribute to define.
# All Properties are assumed casted unless the type is nil.
@@ -103,13 +103,13 @@ def parse_type(type)
end
def parse_options(options)
- @validation_format = options.delete(:format) if options[:format]
- @read_only = options.delete(:read_only) if options[:read_only]
- @alias = options.delete(:alias) if options[:alias]
- @default = options.delete(:default) unless options[:default].nil?
+ @validation_format = options.delete(:format) if options[:format]
+ @read_only = options.delete(:read_only) if options[:read_only]
+ @alias = options.delete(:alias) if options[:alias]
+ @default = options.delete(:default) unless options[:default].nil?
@init_method = options.delete(:init_method) || 'new'
+ @allow_blank = options[:allow_blank].nil? ? true : options.delete(:allow_blank)
@options = options
end
-
end
end
@@ -3,7 +3,7 @@ module Model
module Typecast
def typecast_value(parent, property, value)
- return nil if value.nil?
+ return nil if value.nil? || (!property.allow_blank && value.to_s.empty?)
klass = property.type_class
if value.instance_of?(klass) || klass == Object
if klass == Time && !value.utc?
@@ -6,6 +6,7 @@ class Course < CouchRest::Model::Base
use_database TEST_SERVER.default_database
property :title, String
+ property :subtitle, String, :allow_blank => false
property :questions, [Question]
property :professor, Person
property :participants, [Object]
View
@@ -428,6 +428,16 @@
property.init_method.should eql('parse')
end
+ it "should set the allow_blank option to true by default" do
+ property = CouchRest::Model::Property.new(:test, String)
+ property.allow_blank.should be_true
+ end
+
+ it "should allow setting of the allow_blank option to false" do
+ property = CouchRest::Model::Property.new(:test, String, :allow_blank => false)
+ property.allow_blank.should be_false
+ end
+
describe "#build" do
it "should allow instantiation of new object" do
property = CouchRest::Model::Property.new(:test, Date)
View
@@ -14,6 +14,28 @@
end
end
+ describe "when value is empty string" do
+ it "leaves the value unchanged" do
+ @course.title = ""
+ @course['title'].should == ""
+ end
+ end
+
+ describe "when blank is not allow on property" do
+ it "leaves nil as nil" do
+ @course.subtitle = nil
+ @course['subtitle'].should == nil
+ end
+ it "converts blank to nil" do
+ @course.subtitle = ""
+ @course['subtitle'].should == nil
+ end
+ it "leaves text as text" do
+ @course.subtitle = "Test"
+ @course['subtitle'].should == "Test"
+ end
+ end
+
describe "when type primitive is an Object" do
it "it should not cast given value" do
@course.participants = [{}, 'q', 1]

0 comments on commit 56ebe61

Please sign in to comment.