*making updating password not mandatory on user update

This commit is contained in:
Seth Call 2012-08-29 08:21:30 -05:00
parent 4a2bf208fb
commit 069de7e754
5 changed files with 31 additions and 19 deletions

View File

@ -2,10 +2,10 @@ module JamRuby
class JamSession < ActiveRecord::Base class JamSession < ActiveRecord::Base
self.primary_key = 'id' self.primary_key = 'id'
belongs_to :user, :inverse_of => :jam_sessions belongs_to :creator, :inverse_of => :jam_sessions, :class_name => "JamRuby::User", :foreign_key => "user_id"
has_many :jam_session_members has_many :jam_session_members, :class_name => "JamRuby::JamSessionMember"
has_many :users, :through => :jam_session_members has_many :users, :through => :jam_session_members, :class_name => "JamRuby::User"
# Verifies that the specified user can join this jam session # Verifies that the specified user can join this jam session
def access?(user) def access?(user)

View File

@ -2,9 +2,13 @@ module JamRuby
class JamSessionMember < ActiveRecord::Base class JamSessionMember < ActiveRecord::Base
self.primary_key = 'id' self.primary_key = 'id'
belongs_to :user belongs_to :user, :class_name => "JamRuby::User"
belongs_to :jam_session belongs_to :jam_session, :class_name => "JamRuby::JamSession"
def to_s
return self.user.to_s
end
end end
end end

View File

@ -2,12 +2,13 @@ module JamRuby
class User < ActiveRecord::Base class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation attr_accessible :name, :email, :password, :password_confirmation
attr_accessor :updating_password
self.primary_key = 'id' self.primary_key = 'id'
has_many :jam_session_members has_many :jam_session_members, :class_name => "JamRuby::JamSessionMember"
has_many :created_jam_sessions, :foreign_key => "user_id", :inverse_of => :user # sessions *created* by the user has_many :created_jam_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::JamSession" # sessions *created* by the user
has_many :jam_sessions, :through => :jam_session_members has_many :jam_sessions, :through => :jam_session_members, :class_name => "JamRuby::JamSession"
has_secure_password has_secure_password
@ -18,18 +19,25 @@ module JamRuby
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, validates :email, presence: true, format: {with: VALID_EMAIL_REGEX},
uniqueness: {case_sensitive: false} uniqueness: {case_sensitive: false}
validates :password, length: {minimum: 6} validates_length_of :password, minimum: 6, maximum: 100, :if => :should_validate_password?
validates :password_confirmation, presence: true
private validates_presence_of :password_confirmation, :if => :should_validate_password?
def create_remember_token #validates :password_confirmation, presence: true
self.remember_token = SecureRandom.urlsafe_base64 validates_confirmation_of :password, :if => :should_validate_password?
end
def should_validate_password?
updating_password || new_record?
end
def to_s def to_s
return email unless email.nil? return email unless email.nil?
return name unless name.nil? return name unless name.nil?
return id return id
end end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end end
end end

View File

@ -1,2 +1,2 @@
#!/bin/bas #!/bin/bash
jam_db up --connopts=dbname:jam host:localhost user:postgres password:postgres --verbose bundle exec jam_db up --connopts=dbname:jam host:localhost user:postgres password:postgres --verbose

View File

@ -8,7 +8,7 @@ describe JamSession do
user2 = FactoryGirl.create(:user) # in the jam session user2 = FactoryGirl.create(:user) # in the jam session
user3 = FactoryGirl.create(:user) # not in the jam session user3 = FactoryGirl.create(:user) # not in the jam session
jam_session = FactoryGirl.create(:jam_session, :user => user1) jam_session = FactoryGirl.create(:jam_session, :creator => user1)
jam_session_member1 = FactoryGirl.create(:jam_session_member, :user => user1, :jam_session => jam_session) jam_session_member1 = FactoryGirl.create(:jam_session_member, :user => user1, :jam_session => jam_session)
jam_session_member2 = FactoryGirl.create(:jam_session_member, :user => user2, :jam_session => jam_session) jam_session_member2 = FactoryGirl.create(:jam_session_member, :user => user2, :jam_session => jam_session)
@ -17,4 +17,4 @@ describe JamSession do
jam_session.access?(user2).should == true jam_session.access?(user2).should == true
jam_session.access?(user3).should == false jam_session.access?(user3).should == false
end end
end end