Commit f71649a4

mo k <mo@mokhan.ca>
2012-05-22 13:13:23
add validation for website url on User model.
1 parent 3b923ce
Changed files (5)
app/models/user.rb
@@ -1,5 +1,6 @@
 class User < ActiveRecord::Base
   validates :name,  :presence => true
+  validates :website, :format => URI::regexp(%w(http https)), :presence => false
   has_many :authentications
   devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
   attr_accessible :name, :email, :website, :twitter, :facebook, :password, :password_confirmation, :remember_me
script/features
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+set -e 
+
+cucumber -drb $*
script/test
@@ -0,0 +1,35 @@
+#!/bin/bash
+#
+# From Destroy All Software screencast #10, at:
+# http://destroyallsoftware.com/screencasts/catalog/fast-tests-with-and-without-rails
+#
+# Put this in the script/ directory of your Rails app, then run it with a spec
+# filename. If the spec uses spec_helper, it'll be run inside Bundler.
+# Otherwise, it'll be run directly with whatever `rspec` executable is on the
+# path.
+
+set -e
+
+need_rails=1
+
+if [ $# -gt 0 ]; then # we have args
+    filename=$1
+    # Remove trailing line numbers from filename, e.g. spec/my_spec.rb:33
+    grep_filename=`echo $1 | sed 's/:.*$//g'`
+
+    (set +e; grep -r '\bspec_helper\b' $grep_filename) > /dev/null
+    if [ $? -eq 1 ]; then # no match; we have a stand-alone spec
+        need_rails=''
+    fi
+else # we have no args
+    filename='spec'
+fi
+
+command='rspec'
+
+if [ $need_rails ]; then
+    command="ruby -S bundle exec $command"
+fi
+
+RAILS_ENV=test $command $filename
+
spec/models/user_spec.rb
@@ -10,4 +10,20 @@ describe User do
       creation.favorites.length.should eq(1)
     end
   end
+  describe "when a website url is supplied" do
+    describe "when the url is valid" do
+      it "can validate" do
+        user = User.new
+        user.update_attributes(:website => 'http://example.com')
+        user.errors[:website].any?.should == false
+      end
+    end
+    describe "when the url is not valid" do
+      it "cannot validate" do
+        user = User.new
+        user.update_attributes(:website => 'blah')
+        user.errors[:website].any?.should == true
+      end
+    end
+  end
 end
.vimrc
@@ -0,0 +1,1 @@
+nmap ,t :w\|:!rspec spec/models<cr>