start work on logs
This commit is contained in:
parent
e42150f883
commit
37f150e1cc
79
app/controllers/logs_controller.rb
Normal file
79
app/controllers/logs_controller.rb
Normal file
@ -0,0 +1,79 @@
|
||||
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
|
13
app/models/log.rb
Normal file
13
app/models/log.rb
Normal file
@ -0,0 +1,13 @@
|
||||
class Log < ActiveRecord::Base
|
||||
|
||||
belongs_to :recipe
|
||||
belongs_to :source_recipe, class_name: 'Recipe'
|
||||
belongs_to :user
|
||||
|
||||
validates :date, presence: true
|
||||
validates :user_id, presence: true
|
||||
|
||||
scope :for_user, ->(user) { where(user: user) }
|
||||
scope :for_recipe, ->(recipe) { where(source_recipe: recipe) }
|
||||
|
||||
end
|
11
app/views/logs/_form.html.erb
Normal file
11
app/views/logs/_form.html.erb
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
<%= form_for(@log) do |f| %>
|
||||
|
||||
<%= render partial: 'shared/error_list', locals: {model: @log} %>
|
||||
|
||||
<div class="form-group">
|
||||
<%= f.label :name, class: 'control-label' %>
|
||||
<%= f.text_field :name, class: 'form-control name', autofocus: true %>
|
||||
</div>
|
||||
|
||||
<% end %>
|
0
app/views/logs/index.html.erb
Normal file
0
app/views/logs/index.html.erb
Normal file
0
app/views/logs/new.html.erb
Normal file
0
app/views/logs/new.html.erb
Normal file
@ -1,9 +1,11 @@
|
||||
Rails.application.routes.draw do
|
||||
|
||||
resources :recipes do
|
||||
|
||||
resources :logs, only: [:index, :new, :create]
|
||||
end
|
||||
|
||||
resources :logs, except: [:new, :create]
|
||||
|
||||
resources :ingredients, except: [:show] do
|
||||
collection do
|
||||
get :usda_food_search
|
||||
|
13
db/migrate/20160707011314_create_logs.rb
Normal file
13
db/migrate/20160707011314_create_logs.rb
Normal file
@ -0,0 +1,13 @@
|
||||
class CreateLogs < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :logs do |t|
|
||||
t.integer :user_id
|
||||
t.integer :recipe_id
|
||||
t.integer :source_recipe_id
|
||||
t.datetime :date
|
||||
t.integer :rating
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
end
|
8
spec/factories/logs.rb
Normal file
8
spec/factories/logs.rb
Normal file
@ -0,0 +1,8 @@
|
||||
FactoryGirl.define do
|
||||
factory :log do
|
||||
recipe_id 1
|
||||
date "2016-07-06 20:13:14"
|
||||
rating 1
|
||||
end
|
||||
|
||||
end
|
4
spec/models/log_spec.rb
Normal file
4
spec/models/log_spec.rb
Normal file
@ -0,0 +1,4 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Log, type: :model do
|
||||
end
|
Loading…
Reference in New Issue
Block a user