1
0

View tracking through params in Ingredients

Squeezed ingredient form
This commit is contained in:
cryptogopher 2020-04-04 02:13:46 +02:00
parent 738161fc43
commit f00f93c9e9
17 changed files with 47 additions and 62 deletions

View File

@ -4,7 +4,6 @@ class IngredientsController < ApplicationController
layout 'body_tracking' layout 'body_tracking'
menu_item :body_trackers menu_item :body_trackers
helper :body_trackers helper :body_trackers
helper_method :current_view
include Concerns::Finders include Concerns::Finders
@ -16,15 +15,12 @@ class IngredientsController < ApplicationController
before_action :authorize before_action :authorize
def index def index
self.current_view = :ingredients prepare_ingredients
prepare_items
end end
def new def new
@ingredient = @project.ingredients.new @ingredient = @project.ingredients.new
# passing attr for Nutrient after_initialize @ingredient.nutrients.new(unit: @ingredient.ref_unit)
# FIXME: is this necessary when creating through association?
@ingredient.nutrients.new(ingredient: @ingredient)
end end
def create def create
@ -33,7 +29,7 @@ class IngredientsController < ApplicationController
flash[:notice] = 'Created new ingredient' flash[:notice] = 'Created new ingredient'
prepare_items prepare_items
else else
@ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty? @ingredient.nutrients.new(unit: @ingredient.ref_unit) if @ingredient.nutrients.empty?
render :new render :new
end end
end end
@ -63,14 +59,12 @@ class IngredientsController < ApplicationController
end end
def nutrients def nutrients
self.current_view = :nutrients prepare_nutrients
prepare_items
end end
def toggle_column def toggle_column
@project.nutrient_columns.toggle!(@quantity) @project.nutrient_columns.toggle!(@quantity)
prepare_items prepare_nutrients
render :index
end end
def filter def filter
@ -206,6 +200,10 @@ class IngredientsController < ApplicationController
) )
end end
def prepare_items
params[:view] == 'index' ? prepare_ingredients : prepare_nutrients
end
def prepare_ingredients def prepare_ingredients
@ingredients, @formula_q = @project.ingredients @ingredients, @formula_q = @project.ingredients
.includes(:ref_unit, :source) .includes(:ref_unit, :source)
@ -217,15 +215,4 @@ class IngredientsController < ApplicationController
@ingredients, @requested_n, @extra_n, @formula_q = @project.ingredients @ingredients, @requested_n, @extra_n, @formula_q = @project.ingredients
.filter(session[:i_filters], @quantities) .filter(session[:i_filters], @quantities)
end end
def prepare_items
(current_view == :nutrients) ? prepare_nutrients : prepare_ingredients
end
def current_view
@current_view || (params[:view_mode] == "nutrients" ? :nutrients : :ingredients)
end
def current_view=(cv)
@current_view = cv
end
end end

View File

@ -33,8 +33,8 @@ module IngredientsHelper
end end
end end
def action_links(i) def action_links(i, view)
link_to(l(:button_edit), edit_ingredient_path(i, view_mode: current_view), link_to(l(:button_edit), edit_ingredient_path(i, view: view),
{remote: true, class: "icon icon-edit"}) + {remote: true, class: "icon icon-edit"}) +
delete_link(ingredient_path(i), {remote: true, data: {}}) delete_link(ingredient_path(i), {remote: true, data: {}})
end end

View File

@ -5,10 +5,4 @@ class Nutrient < ActiveRecord::Base
validates :quantity, uniqueness: {scope: :ingredient_id} validates :quantity, uniqueness: {scope: :ingredient_id}
validates :amount, numericality: {greater_than_or_equal_to: 0.0} validates :amount, numericality: {greater_than_or_equal_to: 0.0}
after_initialize do
if new_record?
self.unit ||= self.ingredient.ref_unit
end
end
end end

View File

@ -1,7 +1,6 @@
<% if User.current.allowed_to?(:manage_common, @project) %> <% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".link_import_ingredients"), '#', class: 'icon icon-multiple', <%= link_to t(".link_import_ingredients"), '#', class: 'icon icon-multiple',
onclick: '$("#import-ingredients").show(); $("#filename").focus(); return false;' %> onclick: '$("#import-ingredients").show(); $("#filename").focus(); return false;' %>
<%= link_to t(".link_new_ingredient"), <%= link_to t(".link_new_ingredient"), new_project_ingredient_path(@project, view: view),
new_project_ingredient_path(@project, view_mode: current_view),
{remote: true, class: 'icon icon-add'} %> {remote: true, class: 'icon icon-add'} %>
<% end %> <% end %>

View File

@ -1,5 +1,5 @@
<%= labelled_form_for @ingredient, <%= labelled_form_for @ingredient,
url: ingredient_path(@ingredient, view_mode: current_view), url: ingredient_path(@ingredient, view: view),
method: :patch, remote: true, method: :patch, remote: true,
html: {id: 'ingredient-edit-form', name: 'ingredient-edit-form'} do |f| %> html: {id: 'ingredient-edit-form', name: 'ingredient-edit-form'} do |f| %>

View File

@ -4,18 +4,20 @@
<p><%= f.text_field :name, size: 40, required: true %></p> <p><%= f.text_field :name, size: 40, required: true %></p>
<p><%= f.text_area :notes, cols: 40, rows: 3, required: false, <p><%= f.text_area :notes, cols: 40, rows: 3, required: false,
style: "width: 100%;" %></p> style: "width: 100%;" %></p>
<p>
<%= f.number_field :ref_amount, size: 8, required: true, min: 0,
label: :field_reference %>
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
</p>
<p><%= f.select :group, group_options, required: true %></p>
<div class="splitcontent"> <div class="splitcontent">
<div class="splitcontentleft"> <div class="splitcontentleft">
<p><%= f.select :source_id, source_options, <p><%= f.select :group, group_options, required: true %></p>
{required: false, include_blank: t('.null_source')} %></p> <p>
<%= f.number_field :ref_amount, size: 8, required: true, min: 0,
label: :field_reference %>
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
</p>
</div> </div>
<div class="splitcontentright"> <div class="splitcontentright">
<p>
<%= f.select :source_id, source_options,
{required: false, include_blank: t('.null_source')} %>
</p>
<p><%= f.text_field :source_ident, size: 25, required: false %></p> <p><%= f.text_field :source_ident, size: 25, required: false %></p>
</div> </div>
</div> </div>

View File

@ -1,5 +1,5 @@
<%= render partial: 'ingredients/filters', <%= render partial: 'ingredients/filters',
locals: {url: filter_project_ingredients_path(@project)} %> locals: {url: filter_project_ingredients_path(@project, view: :index)} %>
<% if @ingredients.any? { |i| i.persisted? } %> <% if @ingredients.any? { |i| i.persisted? } %>
<%= error_messages_for @formula_q.formula if @formula_q %> <%= error_messages_for @formula_q.formula if @formula_q %>
@ -35,7 +35,7 @@
<%= i.source.name if i.source.present? %> <%= i.source.name if i.source.present? %>
<%= ", #{i.source_ident}" if i.source_ident.present? %> <%= ", #{i.source_ident}" if i.source_ident.present? %>
</td> </td>
<td class="action unwrappable"><%= action_links(i) %></td> <td class="action unwrappable"><%= action_links(i, :index) %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

@ -1,7 +1,7 @@
<h2><%= t ".heading_new_ingredient" %></h2> <h2><%= t ".heading_new_ingredient" %></h2>
<%= labelled_form_for @ingredient, <%= labelled_form_for @ingredient,
url: project_ingredients_path(@project, view_mode: current_view), url: project_ingredients_path(@project, view: view),
remote: true, remote: true,
html: {id: 'new-ingredient-form', name: 'new-ingredient-form'} do |f| %> html: {id: 'new-ingredient-form', name: 'new-ingredient-form'} do |f| %>

View File

@ -1,5 +1,5 @@
<%= render partial: 'ingredients/filters', <%= render partial: 'ingredients/filters',
locals: {url: filter_project_ingredients_path(@project, view_mode: current_view)} %> locals: {url: filter_project_ingredients_path(@project, view: :nutrients)} %>
<% if @ingredients.any? %> <% if @ingredients.any? %>
<%= render partial: 'ingredients/options' %> <%= render partial: 'ingredients/options' %>
@ -17,8 +17,7 @@
<th style="width:<%= 100/total_width %>%" class="closable ellipsible"> <th style="width:<%= 100/total_width %>%" class="closable ellipsible">
<div style="float:right;position:relative;"> <div style="float:right;position:relative;">
<%= link_to '', <%= link_to '',
toggle_column_project_ingredients_path(@project, quantity_id: q.id, toggle_column_project_ingredients_path(@project, quantity_id: q.id),
view_mode: current_view),
{class: "icon icon-close", method: :post, remote: true} %> {class: "icon icon-close", method: :post, remote: true} %>
</div> </div>
<%= q.name %> <%= q.name %>
@ -39,7 +38,7 @@
<% @requested_n[index].each do |*, value| %> <% @requested_n[index].each do |*, value| %>
<td class="primary value ellipsible"><%= format_value(value) %></td> <td class="primary value ellipsible"><%= format_value(value) %></td>
<% end %> <% end %>
<td class="action unwrappable"><%= action_links(i) %></td> <td class="action unwrappable"><%= action_links(i, :nutrients) %></td>
</tr> </tr>
<tr class="<%= row_class %>" style="display:none"> <tr class="<%= row_class %>" style="display:none">
@ -58,7 +57,9 @@
<p class="value"><%= format_value(value) %></p> <p class="value"><%= format_value(value) %></p>
</td> </td>
<% end %> <% end %>
<td rowspan="<%= rows %>" class="action unwrappable"><%= action_links(i) %></td> <td rowspan="<%= rows %>" class="action unwrappable">
<%= action_links(i, :nutrients) %>
</td>
</tr> </tr>
<% next unless @quantities.length > 0 %> <% next unless @quantities.length > 0 %>

View File

@ -1,7 +1,7 @@
<fieldset id="options" class="collapsible"> <fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend> <legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<div> <div>
<%= form_tag toggle_column_project_ingredients_path(@project, view_mode: current_view), <%= form_tag toggle_column_project_ingredients_path(@project),
id: 'toggle-column-form', name: 'toggle-column-form', id: 'toggle-column-form', name: 'toggle-column-form',
method: :post, remote: true do %> method: :post, remote: true do %>

View File

@ -1,6 +1,6 @@
$('#new-ingredient').empty(); $('#new-ingredient').empty();
<% if current_view == :nutrients %> <% if params[:view] == 'index' %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% else %>
$('#ingredients').html('<%= j render partial: 'ingredients/index' %>'); $('#ingredients').html('<%= j render partial: 'ingredients/index' %>');
<% else %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% end %> <% end %>

View File

@ -2,7 +2,7 @@ $('tr[id=ingredient-<%= @ingredient.id %>]').nextUntil('tr.primary', ':not(.ingr
.remove(); .remove();
var columns = $('table > thead > tr > th').length; var columns = $('table > thead > tr > th').length;
$('tr[id=ingredient-<%= @ingredient.id %>]').nextUntil('tr.primary').addBack().last().after( $('tr[id=ingredient-<%= @ingredient.id %>]').nextUntil('tr.primary').addBack().last().after(
'<tr><td class="form" colspan="'+columns+'">' + '<tr><td class="form" colspan="'+columns+'"><div id="edit-ingredient">' +
'<div id="edit-ingredient"><%= j render partial: "ingredients/edit_form" %></div>' + '<%= j render partial: 'ingredients/edit_form', locals: {view: params[:view]} %>' +
'</td></tr>' '</div></td></tr>'
); );

View File

@ -1,7 +1,7 @@
<div class="contextual"> <div class="contextual">
<%= link_to t(".heading_nutrient_view"), nutrients_project_ingredients_path(@project), <%= link_to t(".heading_nutrient_view"), nutrients_project_ingredients_path(@project),
class: 'icon icon-stats' %> class: 'icon icon-stats' %>
<%= render partial: 'ingredients/contextual' %> <%= render partial: 'ingredients/contextual', locals: {view: :index} %>
</div> </div>
<%= render partial: 'ingredients/import' %> <%= render partial: 'ingredients/import' %>

View File

@ -1,5 +1,5 @@
<% if current_view == :nutrients %> <% if params[:view] == 'index' %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% else %>
$('#ingredients').html('<%= j render partial: 'ingredients/index' %>'); $('#ingredients').html('<%= j render partial: 'ingredients/index' %>');
<% else %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% end %> <% end %>

View File

@ -1 +1,2 @@
$('#new-ingredient').html('<%= j render partial: 'ingredients/new_form' %>'); $('#new-ingredient')
.html('<%= j render partial: 'ingredients/new_form', locals: {view: params[:view]} %>');

View File

@ -1,7 +1,7 @@
<div class="contextual"> <div class="contextual">
<%= link_to t(".heading_ingredient_list"), project_ingredients_path(@project), <%= link_to t(".heading_ingredient_list"), project_ingredients_path(@project),
class: 'icon icon-list' %> class: 'icon icon-list' %>
<%= render partial: 'ingredients/contextual' %> <%= render partial: 'ingredients/contextual', locals: {view: :nutrients} %>
</div> </div>
<%= render partial: 'ingredients/import' %> <%= render partial: 'ingredients/import' %>

View File

@ -0,0 +1 @@
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');