Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created June 10, 2011 05:07
Show Gist options
  • Save bcardarella/1018263 to your computer and use it in GitHub Desktop.
Save bcardarella/1018263 to your computer and use it in GitHub Desktop.
User and hour model
class Hour < ActiveRecord::Base
belongs_to :user
validates_with PunchcardValidator #defined in config/initializers/punchclock_validator.rb
validates_presence_of :role, :message => "can't be blank"
validates_associated :user
validates_numericality_of :manual, :allow_nil => true
validates_presence_of :start_time, :if => :punchcard?
validates_presence_of :manual, :if => :manual?
scope :punched_in, where{start_time.not_eq nil}.where{end_time.eq nil}
def punchcard?
!start_time.blank? && manual.blank?
end
def manual?
!manual.blank? && start_time.blank?
end
def self.punch_in
create!(:start_time => Time.now)
end
def punch_out
update_attribute(:end_time, Time.now)
end
end
class User < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :recoverable, :registerable,
:rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :role, :email, :password, :password_confirmation, :remember_me
has_many :hours
delegate :punched_in, :punch_in, :to => :hours
def punchcard
punched_in.first
end
def punch_out
punchcard.punch_out
end
def punched_in?
!punchcard.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment