Created
October 15, 2013 11:44
-
-
Save thomasstr/6990305 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: @[email protected], to: @[email protected]) == 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