diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index e02fb2b..383cf46 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,7 +1,7 @@ class IngredientsController < ApplicationController require 'csv' - before_action :find_project_by_project_id, only: [:index, :create, :import] + before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients] before_action :find_ingredient, only: [:destroy] before_action :authorize @@ -133,6 +133,23 @@ class IngredientsController < ApplicationController redirect_to project_ingredients_url(@project) end + def nutrients + ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) + @header = @project.quantities.where(displayed: true) + @nutrients = Hash.new { |h,k| h[k] = {} } + @descriptions = Hash.new { |h,k| h[k] = [] } + ingredients.each do |i| + i.nutrients.sort_by { |n| n.quantity.lft }.each do |n| + if @header.include?(n.quantity) + @nutrients[i.name][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]" + else + @descriptions[i.name] << "#{n.quantity.name}: #{n.amount} [#{n.unit.shortname}]" + end + end + end + @descriptions.each { |k, v| @descriptions[k] = v.join(", ") } + end + private def ingredient_params diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index 6c0dd9f..ee65e43 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -3,8 +3,10 @@ <% end %>
+ <%= link_to t(".heading_nutrient_view"), nutrients_project_ingredients_path(@project), + :class => 'icon icon-stats' %> <% if User.current.allowed_to?(:manage_common, @project) %> - <%= link_to t(".heading_import_ingredients"), '#', :class => 'icon icon-file', + <%= link_to t(".heading_import_ingredients"), '#', :class => 'icon icon-multiple', :onclick => 'showAndScrollTo("import-ingredients", "filename"); return false;' %> <%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add', :onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %> diff --git a/app/views/ingredients/nutrients.html.erb b/app/views/ingredients/nutrients.html.erb new file mode 100644 index 0000000..b6a89ee --- /dev/null +++ b/app/views/ingredients/nutrients.html.erb @@ -0,0 +1,34 @@ +<% content_for :sidebar do %> + <%= render :partial => 'body_trackers/sidebar' %> +<% end %> + +
+ <%= link_to t(".heading_ingredient_list"), project_ingredients_path(@project), + :class => 'icon icon-list' %> +
+ +

<%= t ".heading" %>

+<% if @nutrients.any? %> + + + + + <% @header.each do |q| %> + + <% end %> + + + + <% @nutrients.each do |name, values| %> + + + <% @header.each do |q| %> + + <% end %> + + <% end %> + +
<%= l(:field_name) %><%= q.name %>
<%= name %><%= values[q.id] || '-' %>
+<% else %> +

<%= l(:label_no_data) %>

+<% end %> diff --git a/app/views/layouts/_body_tracking.html.erb b/app/views/layouts/_body_tracking.html.erb new file mode 100644 index 0000000..dbe471c --- /dev/null +++ b/app/views/layouts/_body_tracking.html.erb @@ -0,0 +1,3 @@ +<% content_for :header_tags do %> + <%= stylesheet_link_tag 'body_tracking', :plugin => 'body_tracking' %> +<% end %> diff --git a/assets/stylesheets/body_tracking.css b/assets/stylesheets/body_tracking.css new file mode 100644 index 0000000..2abcd23 --- /dev/null +++ b/assets/stylesheets/body_tracking.css @@ -0,0 +1 @@ +table.nutrients td.value {text-align:right;} diff --git a/config/locales/en.yml b/config/locales/en.yml index 9af04bb..17eb5eb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -38,6 +38,7 @@ en: ingredients: index: heading: 'Ingredients' + heading_nutrient_view: 'Nutrient view' heading_import_ingredients: 'Import' heading_new_ingredient: 'New ingredient' label_import_select_csv_file: 'Select CSV file' @@ -50,6 +51,9 @@ en: Sample header: "Name,Reference,Group,Proteins[g],Fats[g],Carbohydrates[g]". Sample data row: "Brussels,100[g],other,3.4,300[mg],9". Unit given in data row has precedence over that specified in header.' + nutrients: + heading: 'Nutrients' + heading_ingredient_list: 'Ingredient list' form: button_add_nutrient: 'Add nutrient' button_delete_nutrient: 'Delete' diff --git a/config/routes.rb b/config/routes.rb index 225a237..076193d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ resources :projects do end resources :ingredients, :only => [:index, :create, :destroy] do post 'import', on: :collection + get 'nutrients', on: :collection end resources :sources, :only => [:index, :create, :destroy] resources :quantities, :only => [:index, :create, :destroy] diff --git a/init.rb b/init.rb index fe7474a..ea7f4e8 100644 --- a/init.rb +++ b/init.rb @@ -1,3 +1,5 @@ +require_dependency 'body_tracking/body_trackers_view_listener' + (Rails::VERSION::MAJOR < 5 ? ActionDispatch : ActiveSupport)::Reloader.to_prepare do Project.include BodyTracking::ProjectPatch end @@ -13,7 +15,7 @@ Redmine::Plugin.register :body_tracking do project_module :body_tracking do permission :view_body_trackers, { :body_trackers => [:index], - :ingredients => [:index], + :ingredients => [:index, :nutrients], :sources => [:index], :quantities => [:index], :units => [:index], diff --git a/lib/body_tracking/body_trackers_view_listener.rb b/lib/body_tracking/body_trackers_view_listener.rb new file mode 100644 index 0000000..9ef4ad0 --- /dev/null +++ b/lib/body_tracking/body_trackers_view_listener.rb @@ -0,0 +1,5 @@ +module BodyTracking + class BodyTrackersViewListener < Redmine::Hook::ViewListener + render_on :view_layouts_base_html_head, partial: 'layouts/body_tracking' + end +end