Velocipede's User, Sales, and Bike Inventory Web App
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

12 years ago
class User < ActiveRecord::Base
acts_as_loggable
12 years ago
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :lockable #TODO ,:confirmable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :nickname, :user_role_id, :bike_id,
:user_profiles_attributes
12 years ago
has_many :user_profiles
accepts_nested_attributes_for :user_profiles, allow_destroy: false
belongs_to :user_role
belongs_to :bike
12 years ago
validates :first_name, :presence => true
validates :last_name, :presence => true
def to_s
"#{first_name} #{last_name}"
end
def user?
user_role.to_s == "user"
end
def staff?
user_role.to_s == "staff"
end
def admin?
user_role.to_s == "admin"
end
def total_hours
ActsAsLoggable::Log.where( :loggable_type => self.class.to_s, :loggable_id => self.id).sum { |l| (l.end_date - l.start_date)/3600 }.round(2)
end
def current_month_hours
#TODO need to prevent users from saving logs across months, force to create a new log if crossing month
current_month_range = (Time.now.beginning_of_month..Time.now.end_of_month)
ActsAsLoggable::Log.where( :loggable_type => self.class.to_s, :loggable_id => self.id)
.where( :start_date => current_month_range)
.where( :end_date => current_month_range)
.sum { |l| (l.end_date - l.start_date)/3600 }
.round(2)
end
12 years ago
end