Skip to content

Instantly share code, notes, and snippets.

@thomasstr
Created October 15, 2013 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasstr/6990305 to your computer and use it in GitHub Desktop.
Save thomasstr/6990305 to your computer and use it in GitHub Desktop.
class Registerhour < ActiveRecord::Base
before_save :check_hours
belongs_to :project
belongs_to :admin
validates_presence_of :project, :timetype
validate :from_time_cannot_be_after_to_time
def from_time_cannot_be_after_to_time
if from.present? && from >= to
errors.add(:from, "Kan ikke være lik eller i fortid")
end
end
end
def create
@registerhour = Registerhour.new(registerhour_params)
#@registerhour.date_today = Date.today
respond_to do |format|
if Registerhour.exists?(project_id: @registerhour.project_id, timetype: @registerhour.timetype, date_today: Date.today, admin_id: current_admin.id, from: @registerhour.from..@registerhour.to, to: @registerhour.from..@registerhour.to) == 1
flash[:error] = "Timer allerede registrert for dette prosjektet."
format.html { render 'new' }
#if check_hours == 1
# flash[:error] = "Timer allerede registrert for dette prosjektet."
# format.html { render 'new' }
else
if @registerhour.save
format.html { redirect_to my_hours_url, notice: 'Timer registrert.' }
format.json { render action: 'show', status: :created, location: @registerhour }
else
format.html { render action: 'new' }
format.json { render json: @registerhour.errors, status: :unprocessable_entity }
end
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_registerhour
@registerhour = Registerhour.find(params[:id])
#@registerhour = Registerhour.where(:admin_id => current_admin.id)
end
# Never trust parameters from the scary internet, only allow the white list through.
def registerhour_params
params.require(:registerhour).permit(:date_today, :from, :to, :timetype, :notes, :approved, :projects, :project_id, :firstname, :admin_id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment