diff --git a/app/assets/javascripts/recipes.js b/app/assets/javascripts/recipes.js index 0a335ce..e79578b 100644 --- a/app/assets/javascripts/recipes.js +++ b/app/assets/javascripts/recipes.js @@ -237,7 +237,7 @@ var lines = data.replace("\r", "").split("\n"); - var regex = /^(?:([\d\/.]+)\s+)?(?:([\w-]+)(?:\s+of)?\s+)?(\w[\w ,\-\(\)\/]*)$/i; + var regex = /^(?:([\d\/.]+(?:\s+[\d\/]+)?)\s+)?(?:([\w-]+)(?:\s+of)?\s+)?(\w[\w ,\-\(\)\/]*)$/i; var magicFunc = function(str) { if (str == "-") { diff --git a/app/assets/stylesheets/recipes.scss b/app/assets/stylesheets/recipes.scss index 8294782..7aced8c 100644 --- a/app/assets/stylesheets/recipes.scss +++ b/app/assets/stylesheets/recipes.scss @@ -48,4 +48,14 @@ div.step-editor { div#ingredient-list, div#step-list { padding-bottom: 15px; +} + +div.recipe-view { + .ingredients div { + padding-bottom: 15px; + } + + .steps div { + padding-bottom: 15px; + } } \ No newline at end of file diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index ba87b24..0f19340 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -1,10 +1,10 @@ class RecipesController < ApplicationController - before_action :set_recipe, only: [:show, :edit, :update, :destroy] + before_action :set_recipe, only: [:show, :edit, :update, :destroy, :scale] # GET /recipes # GET /recipes.json def index - @recipes = Recipe.all + @recipes = Recipe.active end # GET /recipes/1 @@ -12,6 +12,13 @@ class RecipesController < ApplicationController def show end + # GET /recipes/1 + def scale + @scale = params[:factor] + @recipe.scale(@scale) + render :show + end + # GET /recipes/new def new @recipe = Recipe.new @@ -54,10 +61,16 @@ class RecipesController < ApplicationController # DELETE /recipes/1 # DELETE /recipes/1.json def destroy - @recipe.destroy + @recipe.deleted = true + respond_to do |format| - format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' } - format.json { head :no_content } + if @recipe.save + format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' } + format.json { head :no_content } + else + format.html { redirect_to recipes_url, error: 'Recipe could not be destroyed.' } + format.json { head :no_content } + end end end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 7669859..0c6c5ac 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -3,6 +3,8 @@ class Recipe < ActiveRecord::Base has_many :recipe_ingredients, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy + scope :active, -> { where('deleted <> ? OR deleted IS NULL', true) } + accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true accepts_nested_attributes_for :recipe_steps, allow_destroy: true @@ -11,5 +13,11 @@ class Recipe < ActiveRecord::Base validates :total_time, numericality: true, allow_blank: true validates :active_time, numericality: true, allow_blank: true + def scale(factor) + recipe_ingredients.each do |ri| + ri.scale(factor) + end + end + end diff --git a/app/models/recipe_ingredient.rb b/app/models/recipe_ingredient.rb index 7b58012..542a06f 100644 --- a/app/models/recipe_ingredient.rb +++ b/app/models/recipe_ingredient.rb @@ -22,4 +22,10 @@ class RecipeIngredient < ActiveRecord::Base end end + def scale(factor) + if factor.present? && self.quantity.present? && factor != '1' + self.quantity = UnitConversion.convert(self.quantity, factor, nil, nil) + end + end + end diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index a742b00..e1bf423 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -2,34 +2,50 @@
+ <%= @recipe.description %> +
+<%= recipe_time(@recipe) %>
Yields
<%= @recipe.yields %>
Source
<%= @recipe.source %>