From 994c9273d306ad3ef0d21697982494deae81bc57 Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:04:31 -0700 Subject: [PATCH 1/9] Adds ability to add a new recipe for logged-in users. Missing tests. --- app/controllers/recipes_controller.rb | 22 ++++++++++++++++++++++ app/models/recipe.rb | 4 ++++ app/views/recipes/index.html.erb | 4 ++++ app/views/recipes/new.html.erb | 5 +++++ app/views/users/show.html.erb | 3 +++ 5 files changed, 38 insertions(+) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 9f1f85e3..660fbe82 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -1,5 +1,6 @@ class RecipesController < ApplicationController before_action :set_recipe, only: [:show] + before_action :require_login, only: [:new, :create] # TODO: test that these views require login def index if params[:search] @@ -12,8 +13,29 @@ def index def show end + def new + @recipe = Recipe.new + end + + def create + @recipe = Recipe.new(create_params) + @recipe.user_id = session[:user_id] + + if @recipe.save + flash[:message] = { success: "Created successfully!" } # TODO: standardize flash messages / displays + redirect_to recipe_path(@recipe) + else + flash.now[:error] = @recipe.errors # TODO: standardize flash messages / displays + render :new + end + end + private def set_recipe @recipe = Recipe.find(params[:id]) end + + def create_params + params.require(:recipe).permit(:name, :description, :image) + end end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 5ad32f38..aedb063e 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -5,8 +5,12 @@ class Recipe < ActiveRecord::Base has_many :ingredients, through: :measurements has_many :steps belongs_to :user + mount_uploader :image, ImageUploader + validates :name, presence: true # TODO: test this + validates :user_id, presence: true # TODO: test this + scope :by_name, -> { order(:name) } def self.search_by_ingredient(query) diff --git a/app/views/recipes/index.html.erb b/app/views/recipes/index.html.erb index 6ef556b4..75359677 100644 --- a/app/views/recipes/index.html.erb +++ b/app/views/recipes/index.html.erb @@ -3,3 +3,7 @@ <% end %> <%= render 'shared/list_of_links_with_images', { objects: @recipes } %> + +<% if session[:user_id] %> + <%= link_to "Add Recipe", new_recipe_path, class: "btn btn-default" %> +<% end %> diff --git a/app/views/recipes/new.html.erb b/app/views/recipes/new.html.erb index e69de29b..db86670b 100644 --- a/app/views/recipes/new.html.erb +++ b/app/views/recipes/new.html.erb @@ -0,0 +1,5 @@ +<% content_for :page_title do %> +

New Recipe

+<% end %> + +<%= render 'form', { recipe: @recipe } %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index e24373e2..d0d7a083 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -4,6 +4,9 @@

<%= @user.username %>'s Recipes

<%= render 'shared/list_of_links_with_images', { objects: @recipes } %> +<% if session[:user_id] == @user.id %> + <%= link_to "Add Recipe", new_recipe_path, class: "btn btn-default" %> +<% end %>

<%= @user.username %>'s Cookbooks

<%= render 'cookbooks/cookbooks_list', { cookbooks: @cookbooks } %> From bb01408220dfe7d8aabf3e584fbd6567e34d0238 Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:22:48 -0700 Subject: [PATCH 2/9] Adds ability to edit/update a recipe --- app/controllers/recipes_controller.rb | 22 ++++++++++++++++++++-- app/models/recipe.rb | 4 ++++ app/views/recipes/edit.html.erb | 5 +++++ app/views/recipes/show.html.erb | 2 +- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 660fbe82..c4ff5eb3 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -1,6 +1,7 @@ class RecipesController < ApplicationController - before_action :set_recipe, only: [:show] - before_action :require_login, only: [:new, :create] # TODO: test that these views require login + before_action :require_login, only: [:new, :create, :edit, :update] # TODO: test that these views require login + before_action :set_recipe, only: [:show, :edit, :update] + before_action :authenticate_user, only: [:edit, :update] # TODO: test this! def index if params[:search] @@ -30,6 +31,19 @@ def create end end + def edit + end + + def update + if @recipe.update(create_params) + flash[:message] = { success: "Updated successfully!" } + redirect_to recipe_path(@recipe) + else + flash.now[:error] = @recipe.errors + render :edit + end + end + private def set_recipe @recipe = Recipe.find(params[:id]) @@ -38,4 +52,8 @@ def set_recipe def create_params params.require(:recipe).permit(:name, :description, :image) end + + def authenticate_user + redirect_to root_path unless @recipe.owner?(session[:user_id]) + end end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index aedb063e..fd9ac5e2 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -18,4 +18,8 @@ def self.search_by_ingredient(query) .where("ingredients.name like ?", "%#{query}%") .references(:ingredients) end + + def owner?(session_id) # TODO: test this + user_id == session_id + end end diff --git a/app/views/recipes/edit.html.erb b/app/views/recipes/edit.html.erb index e69de29b..58e6c2de 100644 --- a/app/views/recipes/edit.html.erb +++ b/app/views/recipes/edit.html.erb @@ -0,0 +1,5 @@ +<% content_for :page_title do %> +

Edit Recipe

+<% end %> + +<%= render 'form', { recipe: @recipe } %> diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index 1a50acb3..b19f9059 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -4,7 +4,7 @@ <% end %> <% if @recipe.owner?(session[:user_id]) %> - <%= button_to "Edit Recipe", edit_recipe_path(@recipe), class: "btn btn-default" %> + <%= link_to "Edit Recipe", edit_recipe_path(@recipe), class: "btn btn-default" %>
<% end %> From 44b9616bc040cf9fb38b5f6b9942e9b961a2edf0 Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:27:43 -0700 Subject: [PATCH 3/9] Updated 'recipes/:id' view. Of note: extracted recipe step display into a partial, and also added a step#by_number method. Tests missing. --- app/controllers/recipes_controller.rb | 1 + app/models/step.rb | 2 ++ app/views/recipes/show.html.erb | 21 ++++++++++----------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index c4ff5eb3..6502337a 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -12,6 +12,7 @@ def index end def show + @steps = @recipe.steps.by_number end def new diff --git a/app/models/step.rb b/app/models/step.rb index 29afb04f..b66abb22 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -2,6 +2,8 @@ class Step < ActiveRecord::Base belongs_to :recipe has_many :measurements + scope :by_number, -> { order(:number) } + def has_measurements? measurements.count > 0 end diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index b19f9059..31460d73 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -3,6 +3,10 @@

<%= link_to @recipe.user.username, user_path(@recipe.user) %>

<% end %> +<% if @recipe.description # TODO: fix seed data, then change this %> +

<%= @recipe.description %>

+<% end %> + <% if @recipe.owner?(session[:user_id]) %> <%= link_to "Edit Recipe", edit_recipe_path(@recipe), class: "btn btn-default" %>
@@ -11,16 +15,11 @@ <%= image_tag @recipe.image_url, class: "img-responsive center-block img-rounded" %>
    - <% @recipe.steps.each do |step| %><%# OPTIMIZE: this might be a good refactor spot %> -
  1. - <% if step.has_measurements? %> -
      - <% step.measurements.each do |measurement| %> -
    • <%= measurement.amount %> <%= link_to measurement.ingredient.name, ingredient_path(measurement.ingredient) %>
    • - <% end %> -
    - <% end %> - <%= step.directions %> -
  2. + <% @steps.each do |step| %> + <%= render 'steps/display_step_with_ingredients', { recipe: @recipe, step: step } %> <% end %>
+ +<% if @recipe.owner?(session[:user_id]) %> + <%= button_to "Add Step", new_recipe_step_path(@recipe), method: :get, class: "btn btn-default" %> +<% end %> From 1a50508135ad0513c25dc188a34b14869dc04c9c Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:32:05 -0700 Subject: [PATCH 4/9] Minor: fixed link to the edit ingredient page --- app/views/ingredients/show.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/ingredients/show.html.erb b/app/views/ingredients/show.html.erb index 6690bfba..ea85c6c6 100644 --- a/app/views/ingredients/show.html.erb +++ b/app/views/ingredients/show.html.erb @@ -9,7 +9,7 @@ <%= render 'shared/list_of_links_with_images', { objects: @ingredient.recipes } %> <% if @ingredient.owner?(session[:user_id]) %><%# TODO: style this more %> - <%= link_to "Edit #{@ingredient.name}", ingredient_path(@ingredient), class: "btn btn-default" %> + <%= link_to "Edit #{@ingredient.name}", edit_ingredient_path(@ingredient), class: "btn btn-default" %> <%= button_to "Delete #{@ingredient.name}", ingredient_path(@ingredient), method: :delete, data: { confirm: "Are you 100% sure you want to delete this ingredient for everyone? It will be removed from all recipes!" }, class: "btn btn-danger" %> From e89a9d33fa309cbb1fd3b8428406990e151de2a0 Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:34:22 -0700 Subject: [PATCH 5/9] Minor: added link to create an ingredient to 'users/:id' --- app/views/users/show.html.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index d0d7a083..613608a0 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -13,3 +13,6 @@

<%= @user.username %>'s Ingredients

<%= render 'shared/list_of_links_with_images', { objects: @ingredients } %> +<% if session[:user_id] == @user.id %> + <%= link_to "New Ingredient", new_ingredient_path, class: "btn btn-default" %> +<% end %> From 2fd39aaeb4836acbfdbb54da01b9e499eca5710f Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:34:41 -0700 Subject: [PATCH 6/9] Minor: Changed the text of the link/button to create a new recipe --- app/views/users/show.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 613608a0..0c6e2ea0 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -5,7 +5,7 @@

<%= @user.username %>'s Recipes

<%= render 'shared/list_of_links_with_images', { objects: @recipes } %> <% if session[:user_id] == @user.id %> - <%= link_to "Add Recipe", new_recipe_path, class: "btn btn-default" %> + <%= link_to "New Recipe", new_recipe_path, class: "btn btn-default" %> <% end %>

<%= @user.username %>'s Cookbooks

From 5121e445cf4c57339b65338a5bafe2545d52350e Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:37:46 -0700 Subject: [PATCH 7/9] WIP -- adds preliminary recipe destroy method. Left to do: fill in destroy action, and add link(s?) to delete a recipe. --- app/controllers/recipes_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 6502337a..65a65922 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -1,7 +1,7 @@ class RecipesController < ApplicationController - before_action :require_login, only: [:new, :create, :edit, :update] # TODO: test that these views require login + before_action :require_login, only: [:new, :create, :edit, :update, :destroy] # TODO: test that these views require login before_action :set_recipe, only: [:show, :edit, :update] - before_action :authenticate_user, only: [:edit, :update] # TODO: test this! + before_action :authenticate_user, only: [:edit, :update, :destroy] # TODO: test this! def index if params[:search] @@ -45,6 +45,11 @@ def update end end + def destroy + # TODO: Brandi was going to do this + redirect_to user_path(session[:user_id]) + end + private def set_recipe @recipe = Recipe.find(params[:id]) From 9fa40f01fc14071dcad42f435eb3616784174854 Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 12:42:08 -0700 Subject: [PATCH 8/9] AMEND: adds the recipe form partial --- app/views/recipes/_form.html.erb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/views/recipes/_form.html.erb diff --git a/app/views/recipes/_form.html.erb b/app/views/recipes/_form.html.erb new file mode 100644 index 00000000..6bf0d4a8 --- /dev/null +++ b/app/views/recipes/_form.html.erb @@ -0,0 +1,18 @@ +<%= form_for recipe do |f| %> +
+ <%= f.label :name %> + <%= f.text_field :name, class: "form-control", required: true %> +
+ +
+ <%= f.label :description %> + <%= f.text_area :description, class: "form-control" %> +
+ +
+ <%= f.label :image %> + <%= f.file_field :image %> +
+ + <%= f.submit "Save", { class: "btn btn-primary" } %> +<% end %> From 5b3eed43907f6aeaed205379c0c4d88044afe120 Mon Sep 17 00:00:00 2001 From: Ashley Watkins Date: Fri, 31 Jul 2015 13:14:23 -0700 Subject: [PATCH 9/9] AMEND: Adds step+ingredients display partial --- .../_display_step_with_ingredients.html.erb | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/views/steps/_display_step_with_ingredients.html.erb diff --git a/app/views/steps/_display_step_with_ingredients.html.erb b/app/views/steps/_display_step_with_ingredients.html.erb new file mode 100644 index 00000000..cd8d218e --- /dev/null +++ b/app/views/steps/_display_step_with_ingredients.html.erb @@ -0,0 +1,25 @@ +
  • + + <% if step.has_measurements? %> +
      + <% step.measurements.each do |measurement| %> +
    • <%= measurement.amount %> <%= link_to measurement.ingredient.name, ingredient_path(measurement.ingredient) %>
    • + <% end %> +
    + <% end %> + + <%= step.directions %> + <% if recipe.owner?(session[:user_id]) && params[:recipe_id].nil? %> +
      +
    • + <%= link_to "Edit Step", edit_recipe_step_path(recipe, step), class: "btn btn-default" %> +
    • +
    • + <%= button_to "Delete Step", recipe_step_path(recipe, step), + method: :delete, data: { confirm: "Are you 100% sure you want to delete this step? The associated ingredients will also be removed from the recipe!" }, + class: "btn btn-danger" %> +
    • +
    + <% end %> + +