Skip to content

Commit

Permalink
Merge pull request #25 from QuartzBrandi/multiple_changes/anw/anw+bew
Browse files Browse the repository at this point in the history
Multiple changes
  • Loading branch information
QuartzBrandi committed Jul 31, 2015
2 parents bcf7d4b + 5b3eed4 commit f6af747
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 14 deletions.
48 changes: 47 additions & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class RecipesController < ApplicationController
before_action :set_recipe, only: [:show]
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, :destroy] # TODO: test this!

def index
if params[:search]
Expand All @@ -10,10 +12,54 @@ def index
end

def show
@steps = @recipe.steps.by_number
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

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

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])
end

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
8 changes: 8 additions & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ 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)
Recipe.includes(:ingredients)
.where("ingredients.name like ?", "%#{query}%")
.references(:ingredients)
end

def owner?(session_id) # TODO: test this
user_id == session_id
end
end
2 changes: 2 additions & 0 deletions app/models/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/ingredients/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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" %>
Expand Down
18 changes: 18 additions & 0 deletions app/views/recipes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= form_for recipe do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", required: true %>
</div>

<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>

<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image %>
</div>

<%= f.submit "Save", { class: "btn btn-primary" } %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/recipes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% content_for :page_title do %>
<h2>Edit Recipe</h2>
<% end %>

<%= render 'form', { recipe: @recipe } %>
4 changes: 4 additions & 0 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
5 changes: 5 additions & 0 deletions app/views/recipes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% content_for :page_title do %>
<h2>New Recipe</h2>
<% end %>

<%= render 'form', { recipe: @recipe } %>
23 changes: 11 additions & 12 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
<h3><%= link_to @recipe.user.username, user_path(@recipe.user) %></h3>
<% end %>

<% if @recipe.description # TODO: fix seed data, then change this %>
<p><%= @recipe.description %></p>
<% 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" %>
<br>
<% end %>
<%= image_tag @recipe.image_url, class: "img-responsive center-block img-rounded" %>
<ol class="list-unstyled">
<% @recipe.steps.each do |step| %><%# OPTIMIZE: this might be a good refactor spot %>
<li class="preparation_step">
<% if step.has_measurements? %>
<ul class="list-unstyled text-muted">
<% step.measurements.each do |measurement| %>
<li class="ingredients_line"><%= measurement.amount %> <%= link_to measurement.ingredient.name, ingredient_path(measurement.ingredient) %></li>
<% end %>
</ul>
<% end %>
<%= step.directions %>
</li>
<% @steps.each do |step| %>
<%= render 'steps/display_step_with_ingredients', { recipe: @recipe, step: step } %>
<% end %>
</ol>
<% if @recipe.owner?(session[:user_id]) %>
<%= button_to "Add Step", new_recipe_step_path(@recipe), method: :get, class: "btn btn-default" %>
<% end %>
25 changes: 25 additions & 0 deletions app/views/steps/_display_step_with_ingredients.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<li class="preparation_step list-unstyled">

<% if step.has_measurements? %>
<ul class="list-unstyled text-muted">
<% step.measurements.each do |measurement| %>
<li class="ingredients_line"><%= measurement.amount %> <%= link_to measurement.ingredient.name, ingredient_path(measurement.ingredient) %></li>
<% end %>
</ul>
<% end %>

<%= step.directions %>
<% if recipe.owner?(session[:user_id]) && params[:recipe_id].nil? %>
<ul class="list-unstyled">
<li>
<%= link_to "Edit Step", edit_recipe_step_path(recipe, step), class: "btn btn-default" %>
</li>
<li>
<%= 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" %>
</li>
</ul>
<% end %>

</li>
6 changes: 6 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

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

<h3><%= @user.username %>'s Cookbooks</h3>
<%= render 'cookbooks/cookbooks_list', { cookbooks: @cookbooks } %>

<h3><%= @user.username %>'s Ingredients</h3>
<%= 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 %>

0 comments on commit f6af747

Please sign in to comment.