Permalink
Browse files

[Core] Boomark model should validate the its 'url' field before so th…

…at we don't save arbitrary data in that field.
  • Loading branch information...
1 parent ce8f261 commit f1c427aeb3031ec17090a3dc5d600f06cb85a88e @mongrelion mongrelion committed Jun 30, 2011
Showing with 18 additions and 0 deletions.
  1. +8 −0 app/models/bookmark.rb
  2. +10 −0 spec/models/bookmark_spec.rb
View
@@ -13,4 +13,12 @@ class Bookmark
index :url, unique: true
index :tags
+ # - Validations -
+ validate :format_of_url
+
+ # - Instance Methods -
+ def format_of_url
+ errors.add :url, "is not a valid" if ( self.url =~ URI::regexp ).nil?
+ end
+
end
@@ -4,4 +4,14 @@
it { should have_and_belong_to_many :users }
+ it "should not be persisted when its url is invalid" do
+ @bookmark = Fabricate.build( :bookmark, :url => 'k4b00m' )
+ @bookmark.save.should be_false
+ end
+
+ it "should be persisted when its url is valid" do
+ @bookmark = Fabricate.build( :bookmark )
+ @bookmark.save.should be_true
+ end
+
end

0 comments on commit f1c427a

Please sign in to comment.