1
0

Toggling quantity primariness from nutrients view

This commit is contained in:
cryptogopher 2019-11-01 21:43:56 +01:00
parent 09c441f662
commit cf9c913897
12 changed files with 174 additions and 102 deletions

View File

@ -3,6 +3,7 @@ class IngredientsController < ApplicationController
before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients] before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients]
before_action :find_ingredient, only: [:destroy, :toggle] before_action :find_ingredient, only: [:destroy, :toggle]
before_action :find_quantity, only: [:toggle_nutrient_column]
before_action :authorize before_action :authorize
def index def index
@ -17,22 +18,12 @@ class IngredientsController < ApplicationController
def nutrients def nutrients
@ingredient = @project.ingredients.new @ingredient = @project.ingredients.new
@ingredient.nutrients.new(ingredient: @ingredient) @ingredient.nutrients.new(ingredient: @ingredient)
prepare_nutrients
end
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) def toggle_nutrient_column
@primary_quantities = @project.quantities.where(primary: true) @quantity.toggle_primary!
@primary_nutrients = {} prepare_nutrients
@extra_nutrients = {}
ingredients.each do |i|
@primary_nutrients[i] = {}
@extra_nutrients[i] = {}
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
if @primary_quantities.include?(n.quantity)
@primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
else
@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
end
end
end
end end
def create def create
@ -189,4 +180,22 @@ class IngredientsController < ApplicationController
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render_404 render_404
end end
def prepare_nutrients
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
@primary_quantities = @project.quantities.where(primary: true)
@primary_nutrients = {}
@extra_nutrients = {}
ingredients.each do |i|
@primary_nutrients[i] = {}
@extra_nutrients[i] = {}
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n|
if @primary_quantities.include?(n.quantity)
@primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]"
else
@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
end
end
end
end
end end

View File

@ -28,7 +28,7 @@ class QuantitiesController < ApplicationController
end end
def toggle def toggle
@quantity.update(primary: !@quantity.primary) @quantity.toggle_primary!
@quantities = @project.quantities @quantities = @project.quantities
end end
@ -67,13 +67,4 @@ class QuantitiesController < ApplicationController
:primary :primary
) )
end end
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
def find_quantity
@quantity = Quantity.find(params[:id])
@project = @quantity.project
rescue ActiveRecord::RecordNotFound
render_404
end
end end

View File

@ -5,6 +5,15 @@ module IngredientsHelper
end end
end end
def nutrient_column_options
disabled = []
options = nested_set_options(@project.quantities.diet) do |q|
disabled << q.id if q.primary
raw("#{'&ensp;' * q.level}#{q.name}")
end
options_for_select(options, disabled: disabled)
end
def unit_options def unit_options
@project.units.map do |u| @project.units.map do |u|
[u.shortname, u.id] [u.shortname, u.id]

View File

@ -19,4 +19,8 @@ class Quantity < ActiveRecord::Base
self.primary = false if self.primary.nil? self.primary = false if self.primary.nil?
end end
end end
def toggle_primary!
self.toggle!(:primary)
end
end end

View File

@ -0,0 +1,78 @@
<% if @primary_nutrients.any? %>
<%= render :partial => 'ingredients/options' %>
<table class="nutrients list odd-even">
<thead>
<tr>
<% total_width = 3 + @primary_quantities.length %>
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th>
<% @primary_quantities.each do |q| %>
<th style="width:<%= 100/total_width %>%"><%= q.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @primary_nutrients.each do |i, values| %>
<% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %>
<tr id="ingredient-<%= i.id %>" class="primary <%= row_class %>">
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= i.name %></span>
</td>
<% @primary_quantities.each do |q| %>
<td class="primary value">
<%= values[q.id] || '-' %>
</td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= i.name %></span>
</td>
<% @primary_quantities.each do |q| %>
<td class="primary quantity">
<%= q.name %>
</td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="space"></td>
<% @primary_quantities.each do |q| %>
<td class="primary value">
<%= values[q.id] || '-' %>
</td>
<% end %>
</tr>
<% extras = @extra_nutrients[i].keys %>
<% extras.each_slice(@primary_quantities.length).with_index do |names, index| %>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% names.each do |name| %>
<td class="extra quantity">
<%= name %>
</td>
<% end %>
<% if @primary_quantities.length > names.length %>
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td>
<% end %>
</tr>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% names.each do |name| %>
<td class="extra value">
<%= @extra_nutrients[i][name] %>
</td>
<% end %>
<% if @primary_quantities.length > names.length %>
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td>
<% end %>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<% else %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>

View File

@ -0,0 +1,31 @@
<fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<%= form_tag toggle_nutrient_column_project_ingredients_path(@project),
id: 'add_nutrient_column', method: :post, remote: true do %>
<table>
<tr>
<td width="100%"></td>
<td>
<%= select_tag 'id', nutrient_column_options %>
</td>
<td>
<%= submit_tag l(:button_add) %>
</td>
</tr>
</table>
<% end %>
</fieldset>
<table class="list" style="border: none; width: 100%">
<tr>
<% total_width = 3 + @primary_quantities.length %>
<td style="visibility: hidden; border: none; width:<%= 3 * 100/total_width%>%"></td>
<% @primary_quantities.each do |q| %>
<td class="action" style="width:<%= 100/total_width %>%">
<%= link_to l(:button_hide),
toggle_nutrient_column_project_ingredients_path(@project, id: q.id),
{class: "icon icon-close", method: :post, remote: true} %>
</td>
<% end %>
</tr>
</table>

View File

@ -13,79 +13,6 @@
<%= render :partial => 'ingredients/form' %> <%= render :partial => 'ingredients/form' %>
<h2><%= t ".heading" %></h2> <h2><%= t ".heading" %></h2>
<% if @primary_nutrients.any? %> <div id='nutrients'>
<table class="nutrients list odd-even"> <%= render :partial => 'ingredients/list_nutrients' %>
<thead> </div>
<tr>
<th style="width:30%"><%= l(:field_name) %></th>
<% @primary_quantities.each do |q| %>
<th style="width:8%"><%= q.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @primary_nutrients.each do |i, values| %>
<% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %>
<tr id="ingredient-<%= i.id %>" class="primary <%= row_class %>">
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= i.name %></span>
</td>
<% @primary_quantities.each do |q| %>
<td class="primary value">
<%= values[q.id] || '-' %>
</td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= i.name %></span>
</td>
<% @primary_quantities.each do |q| %>
<td class="primary quantity">
<%= q.name %>
</td>
<% end %>
</tr>
<tr class="<%= row_class %>" style="display:none">
<td class="space"></td>
<% @primary_quantities.each do |q| %>
<td class="primary value">
<%= values[q.id] || '-' %>
</td>
<% end %>
</tr>
<% extras = @extra_nutrients[i].keys %>
<% extras.each_slice(@primary_quantities.length).with_index do |names, index| %>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% names.each do |name| %>
<td class="extra quantity">
<%= name %>
</td>
<% end %>
<% if @primary_quantities.length > names.length %>
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td>
<% end %>
</tr>
<tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td>
<% names.each do |name| %>
<td class="extra value">
<%= @extra_nutrients[i][name] %>
</td>
<% end %>
<% if @primary_quantities.length > names.length %>
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td>
<% end %>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<% else %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>

View File

@ -0,0 +1,3 @@
$('div[id^=flash_]').remove();
$('#content').prepend('<%= escape_javascript(render_flash_messages) %>');
$('#nutrients').html('<%= escape_javascript(render :partial => 'ingredients/list_nutrients') %>');

View File

@ -61,6 +61,8 @@ en:
groups: groups:
other: 'other' other: 'other'
meat: 'meat' meat: 'meat'
options:
label_options: 'Options'
index: index:
heading: 'Ingredients' heading: 'Ingredients'
heading_nutrient_view: 'Nutrient view' heading_nutrient_view: 'Nutrient view'

View File

@ -10,6 +10,7 @@ resources :projects do
post 'toggle', on: :member post 'toggle', on: :member
post 'import', on: :collection post 'import', on: :collection
get 'nutrients', on: :collection get 'nutrients', on: :collection
post 'toggle_nutrient_column', on: :collection
end end
resources :sources, :only => [:index, :create, :destroy] resources :sources, :only => [:index, :create, :destroy]
resources :quantities, :only => [:index, :create, :destroy] do resources :quantities, :only => [:index, :create, :destroy] do

View File

@ -1,6 +1,7 @@
require_dependency 'body_tracking/body_trackers_view_listener' require_dependency 'body_tracking/body_trackers_view_listener'
(Rails::VERSION::MAJOR < 5 ? ActionDispatch : ActiveSupport)::Reloader.to_prepare do (Rails::VERSION::MAJOR < 5 ? ActionDispatch : ActiveSupport)::Reloader.to_prepare do
ApplicationController.include BodyTracking::ApplicationControllerPatch
Project.include BodyTracking::ProjectPatch Project.include BodyTracking::ProjectPatch
end end
@ -22,7 +23,7 @@ Redmine::Plugin.register :body_tracking do
}, read: true }, read: true
permission :manage_common, { permission :manage_common, {
:body_trackers => [:defaults], :body_trackers => [:defaults],
:ingredients => [:create, :destroy, :toggle, :import], :ingredients => [:create, :destroy, :toggle, :import, :toggle_nutrient_column],
:sources => [:create, :destroy], :sources => [:create, :destroy],
:quantities => [:create, :destroy, :toggle, :up, :down, :left, :right], :quantities => [:create, :destroy, :toggle, :up, :down, :left, :right],
:units => [:create, :destroy], :units => [:create, :destroy],

View File

@ -0,0 +1,16 @@
module BodyTracking
module ApplicationControllerPatch
ApplicationController.class_eval do
private
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
def find_quantity
@quantity = Quantity.find(params[:id])
@project = @quantity.project
rescue ActiveRecord::RecordNotFound
render_404
end
end
end
end