Skip to content

Instantly share code, notes, and snippets.

@enricostano
Created August 21, 2012 17:51
Show Gist options
  • Save enricostano/3417843 to your computer and use it in GitHub Desktop.
Save enricostano/3417843 to your computer and use it in GitHub Desktop.
user model
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