Created
August 21, 2012 17:51
-
-
Save enricostano/3417843 to your computer and use it in GitHub Desktop.
user model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_and_belongs_to_many :roles | |
accepts_nested_attributes_for :roles | |
has_many :carts | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :confirmable, | |
# :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :email, :password, :password_confirmation, :remember_me, :phone, :address, :cap, :city, :partitaiva, :role_ids | |
# attr_accessible :title, :body | |
validates :phone, :address, :cap, :city, :partitaiva, :presence => true | |
validates :cap, :length => { :is => 5 } | |
validates :partitaiva, :length => { :is => 11 } | |
validates :partitaiva, :cap, :numericality => { :only_integer => true } | |
validates :partitaiva, :uniqueness => true | |
def role?(role) | |
return !!self.roles.find_by_name(role.to_s.camelize) | |
end | |
def any_role?(*roles) | |
return roles.any? { |r| self.roles.include?(r.to_s.camelize) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment