Permalink
Browse files

Allow the attribute to be nil without making the model invalid

For optional values this will make the object invalid because `nil` won't
match the `UrlFormatter.url_regexp`.
  • Loading branch information...
1 parent 3716c8c commit 7cca3198112e2cca442f426ccbb2648e73424113 @nhocki committed Mar 27, 2012
Showing with 6 additions and 1 deletion.
  1. +1 −1 lib/url_formatter/model_additions.rb
  2. +5 −0 spec/url_formatter/model_additions_spec.rb
@@ -14,7 +14,7 @@ def format_url(attribute)
before_validation do
send("#{attribute}=", UrlFormatter.format_url(send(attribute)))
end
- validates_format_of attribute, with: UrlFormatter.url_regexp, message: "is not a valid URL"
+ validates_format_of attribute, with: UrlFormatter.url_regexp, message: "is not a valid URL", if: "#{attribute}?"
end
end
end
@@ -19,4 +19,9 @@ class Comment < SuperModel::Base
comment.website = "example.com"
comment.should be_valid
end
+
+ it "allows a nil url to be valid" do
+ comment = Comment.new(website: nil)
+ comment.should be_valid
+ end
end

0 comments on commit 7cca319

Please sign in to comment.