Commit 3759cc32
Changed files (2)
app
models
spec
models
app/models/user.rb
@@ -7,16 +7,18 @@ class User < ActiveRecord::Base
end
before_save :geocode, :reverse_geocode
+ before_save :ensure_authentication_token
+
validates :name, :presence => true
validates :website, :format => URI::regexp(%w(http https)), :allow_blank => true
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable
+
has_many :creations, :dependent => :destroy
has_many :favorites, :dependent => :destroy
has_many :tutorials, :dependent => :destroy
has_and_belongs_to_many :interests, :join_table => 'users_interests', :autosave => true
has_one :avatar
acts_as_tagger
- before_save :ensure_authentication_token
def add_favorite(creation)
creation.liked_by(self)
@@ -44,7 +46,9 @@ class User < ActiveRecord::Base
"#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
end
- def self.ordered
- User.order(:creations_count => :desc)
+ class << self
+ def ordered
+ User.order(:creations_count => :desc)
+ end
end
end
spec/models/user_spec.rb
@@ -22,25 +22,27 @@ describe User do
describe "when a website url is supplied" do
describe "when the url is valid" do
let(:user) {User.new}
- before(:each) do
- user.update_attributes(:website => 'http://example.com')
- end
+
+ before(:each) { user.update_attribute(:website, 'http://example.com') }
+
it "can validate" do
user.errors[:website].any?.should == false
end
+
it "should not have any validation error messages" do
user.errors[:website].should === []
end
end
describe "when the url is not valid" do
- let(:user) {User.new}
- before(:each) do
- user.update_attributes(:website => 'blah')
- end
+ let(:user) { User.new }
+
+ before(:each) { user.update_attribute(:website, 'blah') }
+
it "cannot validate" do
user.errors[:website].any?.should == true
end
+
it "should have an error message" do
user.errors[:website].should === ["is invalid"]
end