class TimetablesController < ApplicationController # FILTERS before_filter :authenticate_user!, :except => [:show, :about] # SHOW def show if user_signed_in? && current_user.timetable @timetable_items = current_user.timetable elsif user_signed_in? && !current_user.timetable || !user_signed_in? redirect_to about_timetable_path # Redirect user to the about timetable page if they are either not logged in or do not have a timetable. end end # ABOUT - Redirected to the about page if they user doesn't have a timetable or they aren't logged in. def about end # NEW def new if current_user.timetable redirect_to timetable_path, :notice => "You already have a timetable!" else @timetable = Timetable.new timetable_item = @timetable.timetable_items.build # Build the first Timetable Item so it is displayed timetable_item.timetable_events.build # Build the first Timetable Event so it is displayed end end def create update_timetable_event_params(params[:timetable]) # Hacky method to get simple_time_select to work @timetable = Timetable.new(params[:timetable]) @timetable.user = current_user if @timetable.save redirect_to timetable_path, :notice => "Timetable created successfully!" else render :action => 'new' end end # EDIT def edit if @timetable = current_user.timetable @timetable else redirect_to new_timetable_path end end def update @timetable = current_user.timetable update_timetable_event_params(params[:timetable]) # Hacky method to get simple_time_select to work if @timetable.update_attributes(params[:timetable]) redirect_to timetable_path, :notice => "Your timetable has successfully been updated! :)" else render :action => 'edit' end end private def update_timetable_event_params(timetable_params) @item_params = timetable_params["timetable_items_attributes"] @item_params.keys.each do |item_key| unless @item_params[item_key]["timetable_events_attributes"].blank? @item_params[item_key]["timetable_events_attributes"].keys.each do |event_key| @event_params = @item_params[item_key]["timetable_events_attributes"][event_key] @event_params["start_time"] = @event_params["start_time(5i)"] @event_params.delete("start_time(5i)") end end end end end