diff --git a/app/assets/javascripts/recipes.js b/app/assets/javascripts/recipes.js index 308ea00..81f9d12 100644 --- a/app/assets/javascripts/recipes.js +++ b/app/assets/javascripts/recipes.js @@ -12,7 +12,6 @@ var $editors = $container.find("textarea.step").closest(".step-editor"); $editors.each(function(idx, elem) { - console.log('doing stuff!!!'); var $editor = $(elem); var $step = $editor.find("textarea.step"); autosize($step); @@ -161,15 +160,15 @@ var $quantity = $editor.find("input.quantity"); var $units = $editor.find("input.units"); - var $density = $editor.find("input.custom_density"); + var $ingredientId = $editor.find("input.ingredient_id"); var $modalQuantity = $modal.find("input.quantity"); var $modalUnits = $modal.find("input.units"); - var $modalDensity = $modal.find("input.density"); + var $modalIngredientId = $modal.find("input.ingredient_id"); $modalQuantity.val($quantity.val()); $modalUnits.val($units.val()); - $modalDensity.val($density.val()); + $modalIngredientId.val($ingredientId.val()); }) .on("ajax:success", "form", function(evt, data, status, xhr) { var $modal = $("#convert_modal"); diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 58c5e3e..477bbac 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -35,6 +35,7 @@ body { #main_container { background: white; + padding-bottom: 15px; } .footer { diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 7722edf..7d50dab 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -89,6 +89,6 @@ class IngredientsController < ApplicationController end def conversion_params - params.require(:conversion).permit(:input_quantity, :input_units, :scale, :output_units, :density) + params.require(:conversion).permit(:input_quantity, :input_units, :scale, :output_units, :ingredient_id) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8bfb10f..e030367 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,9 @@ module ApplicationHelper + + def timestamp(time) + time ? time.strftime('%D %R') : '' + end + def nav_items [ nav_item('Recipes', recipes_path, 'recipes'), diff --git a/app/helpers/recipes_helper.rb b/app/helpers/recipes_helper.rb index f526316..bfc051e 100644 --- a/app/helpers/recipes_helper.rb +++ b/app/helpers/recipes_helper.rb @@ -1,2 +1,25 @@ module RecipesHelper + def recipe_time(recipe) + output = ''.html_safe + + if recipe.total_time.present? + output << "#{humanize_seconds(recipe.total_time.to_i.minutes)}" + if recipe.active_time.present? + output << " (#{humanize_seconds(recipe.active_time.to_i.minutes)} active)" + end + elsif recipe.active_time.present? + output << humanize_seconds(recipe.active_time.to_i.minutes) + end + + output + end + + def humanize_seconds(secs) + [[60, :s], [60, :m], [24, :h], [1000, :d]].map{ |count, name| + if secs > 0 + secs, n = secs.divmod(count) + n == 0 ? nil : "#{n.to_i} #{name}" + end + }.compact.reverse.join(' ') + end end diff --git a/app/models/conversion.rb b/app/models/conversion.rb index 4e99d99..29c1cd1 100644 --- a/app/models/conversion.rb +++ b/app/models/conversion.rb @@ -3,7 +3,7 @@ class Conversion include ActiveModel::Conversion extend ActiveModel::Naming - attr_accessor :input_quantity, :input_units, :scale, :output_units, :density + attr_accessor :input_quantity, :input_units, :scale, :output_units, :ingredient_id attr_reader :output_quantity validates :input_quantity, presence: true @@ -21,8 +21,9 @@ class Conversion def check_conversion begin + ingredient = ingredient_id.blank? ? nil : Ingredient.find(ingredient_id) scale = self.scale.blank? ? '1' : self.scale - density = self.density.blank? ? nil : self.density + density = ingredient.nil? ? nil : ingredient.density @output_quantity = UnitConversion.convert(input_quantity, scale, input_units, output_units, density) rescue UnitConversion::UnparseableUnitError => err errors[:base] << "Invalid Data: #{err.message}" diff --git a/app/models/unit_conversion.rb b/app/models/unit_conversion.rb index 57e66b5..6dbfd0a 100644 --- a/app/models/unit_conversion.rb +++ b/app/models/unit_conversion.rb @@ -56,9 +56,17 @@ module UnitConversion unit.compatible_with? Unitwise(1, 'g') end + # Returns a Unitwise representation of the density. Raises an exception if the value is anything other than a + # valid density def get_density(str) - unit = parse(str) - raise UnknownUnitError, "#{str} expected to be a density" unless density?(unit) + raise UnknownUnitError, 'No density provided' if str.blank? + begin + unit = parse(str) + rescue UnparseableUnitError => err + raise UnknownUnitError "Invalid density: #{err.message}" + end + + raise UnknownUnitError, "Invalid density: #{str} is not a density" unless density?(unit) unit end diff --git a/app/views/recipes/editor/_conversion_form.erb b/app/views/recipes/editor/_conversion_form.erb index 9a232d5..0841085 100644 --- a/app/views/recipes/editor/_conversion_form.erb +++ b/app/views/recipes/editor/_conversion_form.erb @@ -39,7 +39,7 @@ - <%= f.hidden_field :density, class: 'density' %> + <%= f.hidden_field :ingredient_id, class: 'ingredient_id' %> <% end %> diff --git a/app/views/recipes/editor/_ingredient.html.erb b/app/views/recipes/editor/_ingredient.html.erb index 47829fd..90cb250 100644 --- a/app/views/recipes/editor/_ingredient.html.erb +++ b/app/views/recipes/editor/_ingredient.html.erb @@ -6,7 +6,7 @@
-
+
<%= f.label :custom_name, "Name", class: "control-label" %>
@@ -20,26 +20,19 @@
-
+
<%= f.label :quantity, class: "control-label" %> <%= f.text_field :quantity, class: 'form-control quantity' %>
-
+
<%= f.label :units, class: "control-label" %> <%= f.text_field :units, class: 'form-control units' %>
- -
-
- <%= f.label :custom_density, "Density", class: "control-label" %> - <%= f.text_field :custom_density, class: 'form-control custom_density' %> -
-
diff --git a/app/views/recipes/index.html.erb b/app/views/recipes/index.html.erb index ae21cbd..cca0435 100644 --- a/app/views/recipes/index.html.erb +++ b/app/views/recipes/index.html.erb @@ -9,35 +9,42 @@

No Recipes

<% else %> - - - - - - - - - - - - - - - <% @recipes.each do |recipe| %> +
+
NameYieldsTotal TimeActive TimeCreatedModified
+ - - - - - - - - - + + + + + + - <% end %> - -
<%= recipe.name %><%= recipe.yields %><%= recipe.total_time %><%= recipe.active_time %><%= recipe.created_at %><%= recipe.updated_at %><%= link_to 'Show', recipe %><%= link_to 'Edit', edit_recipe_path(recipe) %><%= link_to 'Destroy', recipe, method: :delete, data: { confirm: 'Are you sure?' } %>NameYieldsTimeCreatedModified
+ + + + <% @recipes.each do |recipe| %> + + <%= link_to recipe.name, recipe %> + + <%= link_to edit_recipe_path(recipe), class: 'btn btn-sm btn-primary' do %> + + <% end %> + + + <%= link_to recipe, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-sm btn-danger' do %> + + <% end %> + + <%= recipe.yields %> + <%= recipe_time(recipe) %> + <%= timestamp(recipe.created_at) %> + <%= timestamp(recipe.updated_at) %> + + <% end %> + + +
<% end %> diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb index 446e989..a742b00 100644 --- a/app/views/recipes/show.html.erb +++ b/app/views/recipes/show.html.erb @@ -1,60 +1,52 @@ -
-
- -
-

<%= @recipe.name %>

+ +
+ +
+
+
-
- -
-

<%= @recipe.description %>

-
+
+ <% if @recipe.total_time.present? || @recipe.active_time.present? %> +
+ <%= recipe_time(@recipe) %> +
+ <% end %> + + <% if @recipe.yields.present? %> +
+ Yields<%= @recipe.yields %> +
+ <% end %> + + <% if @recipe.source.present? %> +
+ Source<%= @recipe.source %> +
+ <% end %> +
-
- -
-

<%= @recipe.source %>

-
-
- -
- -
-

<%= @recipe.yields %>

-
-
- -
- -
-

<%= @recipe.total_time %>

-
-
- -
- -
-

<%= @recipe.active_time %>

-
-
- -
- -
+
+
+

Ingredients

    <% @recipe.recipe_ingredients.each do |i| %>
  • <%= "#{i.quantity} #{i.units} of #{i.custom_name}" %>
  • <% end %>
-
-
- -
+
+

Directions

    <% @recipe.recipe_steps.each do |s| %>
  1. <%= "#{s.step}" %>
  2. @@ -62,13 +54,14 @@
+
- <%= link_to 'Edit', edit_recipe_path(@recipe) %> | - <%= link_to 'Back', recipes_path %> + <%= link_to 'Edit', edit_recipe_path(@recipe), class: 'btn btn-default' %> + <%= link_to 'Back', recipes_path, class: 'btn btn-default' %>
\ No newline at end of file