Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i have a timesheet application, where a consultant has to submit a timesheet report. here he has to submit timesheet. the dates he is submitting should be beween his project start_date and end_date. please help me how to do it

class Timesheet < ActiveRecord::Base
  attr_accessible :start_date,
    :end_date,
    :time_proof,
    :time_proof_cache,
    :is_approved,
    :approved_by,
    :approved_date,
    :user_id,
    :client_id,
    :placement_id,
    :timesheet_days_attributes

  belongs_to :placement
  belongs_to :client
  belongs_to :user

 # after_save :timesheet_report

  mount_uploader :time_proof, TimeProofUploader

  has_many :timesheet_days, :inverse_of => :timesheet

  accepts_nested_attributes_for :timesheet_days

  validates_presence_of :start_date
  validates_presence_of :end_date
  validates_presence_of :placement_id
  validate :assignment_start_date_validity
  validate :assignment_end_date_validity

  scope :awaiting_approval, lambda { where("is_approved IS NULL or is_approved = ?", false) }
  scope :recent_timesheets, lambda { limit(5) }

  def pay_period_code
    placement.pay_period_code if placement
  end

  def start_date=(value)
    begin
      self[:start_date] = Date.strptime(value, '%m/%d/%Y')
    rescue ArgumentError
      self.errors.add(:base, "Invalid Date format")
    end
  end

  def end_date=(value)
    begin
      self[:end_date] = Date.strptime(value, '%m/%d/%Y')
    rescue ArgumentError
      self.errors.add(:base, "Invalid Date format")
    end
  end

def assignment_start_date_validity
  debugger
 if start_date
     errors.add(:start_date, "Assignment not valid") if start_date < placement.assignment_start 
  end
 end

def assignment_end_date_validity
  debugger
  if end_date
    errors.add(:end_date, "Assignment not valid") if  end_date > placement.assignment_end
   end  
 end
end
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.