parsley/app/controllers/logs_controller.rb
2016-07-06 21:00:35 -05:00

79 lines
1.4 KiB
Ruby

class LogsController < ApplicationController
before_action :set_log, only: [:show, :edit, :update, :destroy]
before_action :set_recipe, only: [:index, :new, :create]
before_action :require_recipe, only: [:new, :create]
before_filter :ensure_valid_user
def index
@logs = Log.for_user(current_user)
if @recipe
@logs = @logs.for_recipe(@recipe)
end
end
def show
ensure_owner(@log)
end
def edit
ensure_owner(@log)
end
def update
ensure_owner(@log) do
if @log.update(log_params)
redirect_to @log, notice: 'Log Entry was successfully updated.'
else
render :edit
end
end
end
def new
@log = Log.new
end
def create
@log = Recipe.new(log_params)
@log.user = current_user
@log.souce_recipe = @recipe
if @log.save
redirect_to @log, notice: 'Log Entry was successfully created.'
else
render :new
end
end
def destroy
ensure_owner(@log) do
@log.destroy
redirect_to logs_url, notice: 'Log Entry was successfully destroyed.'
end
end
private
def set_log
@log = Log.find(params[:id])
end
def set_recipe
if params[:recipe_id].present?
@recipe = Recipe.find(params[:recipe_id])
end
end
def require_recepe
unless @recipe
raise ActiveRecord::RecordNotFound
end
end
def log_params
params.require(:log).permit()
end
end