small ui changes

This commit is contained in:
Dan Elbert 2016-04-04 14:28:23 -05:00
parent d368ef4a6f
commit 80c203b227
9 changed files with 163 additions and 62 deletions

View File

@ -23,6 +23,7 @@ ENV PASSENGER_APP_ENV docker
RUN mkdir -p /home/app/parsley/ RUN mkdir -p /home/app/parsley/
COPY Gemfile /home/app/parsley/ COPY Gemfile /home/app/parsley/
COPY Gemfile.lock /home/app/parsley/ COPY Gemfile.lock /home/app/parsley/
RUN gem install bundler
RUN cd /home/app/parsley/ && bundle install --deployment RUN cd /home/app/parsley/ && bundle install --deployment
# Copy the app into the image # Copy the app into the image

View File

@ -37,12 +37,15 @@ class RecipesController < ApplicationController
end end
end end
@recipe = RecipeDecorator.decorate(@recipe, view_context)
end end
# GET /recipes/1 # GET /recipes/1
def scale def scale
@scale = params[:factor] @scale = params[:factor]
@recipe.scale(@scale, true) @recipe.scale(@scale, true)
@recipe = RecipeDecorator.decorate(@recipe, view_context)
render :show render :show
end end
@ -84,7 +87,7 @@ class RecipesController < ApplicationController
ensure_owner(@recipe) do ensure_owner(@recipe) do
@recipe.deleted = true @recipe.deleted = true
if @recipe.save if @recipe.save(validate: false)
redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' redirect_to recipes_url, notice: 'Recipe was successfully destroyed.'
else else
redirect_to recipes_url, error: 'Recipe could not be destroyed.' redirect_to recipes_url, error: 'Recipe could not be destroyed.'

View File

@ -0,0 +1,50 @@
require 'delegate'
# Minimal Decorator Base Class
#
# Implements a SimpleDelegator, provides access to the view_context through the `h` method, and provides
# a factory method to construct Delegators or collections of Delegators
#
# In a controller, the view_context can be retrieved by calling a method of the same name
# In a view or view helper, the context is `self`
#
# See also ApplicationHelper#decorate
class BaseDecorator < SimpleDelegator
class << self
# Decorates a single object or a collection of objects. If the given objects are `BaseDecorators`, they will be
# unwrapped first
def decorate(obj, view_context)
decorated = Array.wrap(obj).map do |o|
case o
when nil
nil
when BaseDecorator
new(o.__getobj__, view_context)
else
new(o, view_context)
end
end
obj.respond_to?(:each) ? decorated : decorated.first
end
end
def initialize(base, view_context)
super(base)
@view_context = view_context
end
def h
@view_context
end
# For some reason, view_context.html_escape is marked as private. To provide access to the same functionality,
# define it here
def html_escape(*args)
ERB::Util.html_escape(*args)
end
def wrapped
__getobj__
end
end

View File

@ -0,0 +1,19 @@
class NutritionDataDecorator < BaseDecorator
[:protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar].each do |m|
def format_number(n)
'%.1f' % n
end
define_method m do
format_number(super())
end
define_method "#{m}_per" do |per|
format_number(wrapped.send(m) / per)
end
end
end

View File

@ -0,0 +1,17 @@
class RecipeDecorator < BaseDecorator
def source_markup
uri = begin
URI.parse(self.source)
rescue URI::InvalidURIError
nil
end
if uri.is_a? URI::HTTP
h.link_to(uri.host, uri.to_s)
else
h.content_tag('span', self.source)
end
end
end

View File

@ -1,5 +1,18 @@
module ApplicationHelper module ApplicationHelper
# Given a model or collection of models, returns them with the given decorator. If a block is passed, yields the
# decorated objects as well
#
# Useful in this context:
# <% decorate(@model_obj, ModelObjDecorator) do |model_obj| %>
# <%= model_obj.decorator_method %>
# <% end %>
def decorate(obj, decorator_class)
decorated = decorator_class.decorate(obj, self)
yield(decorated) if block_given?
decorated
end
def timestamp(time) def timestamp(time)
time ? time.strftime('%D %R') : '' time ? time.strftime('%D %R') : ''
end end

View File

@ -58,7 +58,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<%= f.label :carbohydrates, "Grams of Fat", class: 'control-label' %> <%= f.label :carbohydrates, "Grams of Carbohydrates", class: 'control-label' %>
<%= f.text_field :carbohydrates, class: 'form-control' %> <%= f.text_field :carbohydrates, class: 'form-control' %>
</div> </div>

View File

@ -14,13 +14,12 @@
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<% if current_user? %>
<th></th>
<% end %>
<th>Yields</th> <th>Yields</th>
<th>Time</th> <th>Time</th>
<th>Created</th> <th>Created</th>
<th>Modified</th> <% if current_user? %>
<th></th>
<% end %>
</tr> </tr>
</thead> </thead>
@ -28,7 +27,9 @@
<% @recipes.each do |recipe| %> <% @recipes.each do |recipe| %>
<tr> <tr>
<td><%= link_to recipe.display_name, recipe %></td> <td><%= link_to recipe.display_name, recipe %></td>
<td><%= recipe.yields %></td>
<td><%= recipe_time(recipe) %></td>
<td><%= timestamp(recipe.created_at) %></td>
<% if current_user? %> <% if current_user? %>
<td> <td>
<%= link_to edit_recipe_path(recipe), class: 'btn btn-sm btn-primary' do %> <%= link_to edit_recipe_path(recipe), class: 'btn btn-sm btn-primary' do %>
@ -39,11 +40,6 @@
<% end %> <% end %>
</td> </td>
<% end %> <% end %>
<td><%= recipe.yields %></td>
<td><%= recipe_time(recipe) %></td>
<td><%= timestamp(recipe.created_at) %></td>
<td><%= timestamp(recipe.updated_at) %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

@ -32,7 +32,7 @@
<% if @recipe.source.present? %> <% if @recipe.source.present? %>
<div class="source"> <div class="source">
<p>Source</p><p><%= @recipe.source %></p> <p>Source</p><p><%= @recipe.source_markup %></p>
</div> </div>
<% end %> <% end %>
@ -88,6 +88,7 @@
<h3 class="panel-title">Nutrition Data</h3> <h3 class="panel-title">Nutrition Data</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<% decorate(@recipe.nutrition_data, NutritionDataDecorator) do |nutrition_data| %>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -101,46 +102,47 @@
<tr> <tr>
<td>Calories</td> <td>Calories</td>
<% if @recipe.parsed_yield %> <% if @recipe.parsed_yield %>
<td><%= @recipe.nutrition_data.kcal / @recipe.parsed_yield.number %></td> <td><%= nutrition_data.kcal_per(@recipe.parsed_yield.number) %></td>
<% end %> <% end %>
<td><%= @recipe.nutrition_data.kcal %></td> <td><%= nutrition_data.kcal %></td>
</tr> </tr>
<tr> <tr>
<td>Grams Protein</td> <td>Grams Protein</td>
<% if @recipe.parsed_yield %> <% if @recipe.parsed_yield %>
<td><%= @recipe.nutrition_data.protein / @recipe.parsed_yield.number %></td> <td><%= nutrition_data.protein_per(@recipe.parsed_yield.number) %></td>
<% end %> <% end %>
<td><%= @recipe.nutrition_data.protein %></td> <td><%= nutrition_data.protein %></td>
</tr> </tr>
<tr> <tr>
<td>Grams Fat</td> <td>Grams Fat</td>
<% if @recipe.parsed_yield %> <% if @recipe.parsed_yield %>
<td><%= @recipe.nutrition_data.lipids / @recipe.parsed_yield.number %></td> <td><%= nutrition_data.lipids_per(@recipe.parsed_yield.number) %></td>
<% end %> <% end %>
<td><%= @recipe.nutrition_data.lipids %></td> <td><%= nutrition_data.lipids %></td>
</tr> </tr>
<tr> <tr>
<td>Grams Carbohydrates</td> <td>Grams Carbohydrates</td>
<% if @recipe.parsed_yield %> <% if @recipe.parsed_yield %>
<td><%= @recipe.nutrition_data.carbohydrates / @recipe.parsed_yield.number %></td> <td><%= nutrition_data.carbohydrates_per(@recipe.parsed_yield.number) %></td>
<% end %> <% end %>
<td><%= @recipe.nutrition_data.carbohydrates %></td> <td><%= nutrition_data.carbohydrates %></td>
</tr> </tr>
<tr> <tr>
<td>Grams Sugar</td> <td>Grams Sugar</td>
<% if @recipe.parsed_yield %> <% if @recipe.parsed_yield %>
<td><%= @recipe.nutrition_data.sugar / @recipe.parsed_yield.number %></td> <td><%= nutrition_data.sugar_per(@recipe.parsed_yield.number) %></td>
<% end %> <% end %>
<td><%= @recipe.nutrition_data.sugar %></td> <td><%= nutrition_data.sugar %></td>
</tr> </tr>
<tr> <tr>
<td>Grams Fiber</td> <td>Grams Fiber</td>
<% if @recipe.parsed_yield %> <% if @recipe.parsed_yield %>
<td><%= @recipe.nutrition_data.fiber / @recipe.parsed_yield.number %></td> <td><%= nutrition_data.fiber_per(@recipe.parsed_yield.number) %></td>
<% end %> <% end %>
<td><%= @recipe.nutrition_data.fiber %></td> <td><%= nutrition_data.fiber %></td>
</tr> </tr>
</table> </table>
<% end %>
<h3>Nutrition Calculation Warnings</h3> <h3>Nutrition Calculation Warnings</h3>