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

View File

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

View File

@ -5,10 +5,4 @@ class Nutrient < ActiveRecord::Base
validates :quantity, uniqueness: {scope: :ingredient_id}
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

View File

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

View File

@ -1,5 +1,5 @@
<%= labelled_form_for @ingredient,
url: ingredient_path(@ingredient, view_mode: current_view),
url: ingredient_path(@ingredient, view: view),
method: :patch, remote: true,
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_area :notes, cols: 40, rows: 3, required: false,
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="splitcontentleft">
<p><%= f.select :source_id, source_options,
{required: false, include_blank: t('.null_source')} %></p>
<p><%= f.select :group, group_options, required: true %></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 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>
</div>
</div>

View File

@ -1,5 +1,5 @@
<%= 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? } %>
<%= error_messages_for @formula_q.formula if @formula_q %>
@ -35,7 +35,7 @@
<%= i.source.name if i.source.present? %>
<%= ", #{i.source_ident}" if i.source_ident.present? %>
</td>
<td class="action unwrappable"><%= action_links(i) %></td>
<td class="action unwrappable"><%= action_links(i, :index) %></td>
</tr>
<% end %>
</tbody>

View File

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

View File

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

View File

@ -1,7 +1,7 @@
<fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<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',
method: :post, remote: true do %>

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
<% if current_view == :nutrients %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% else %>
<% if params[:view] == 'index' %>
$('#ingredients').html('<%= j render partial: 'ingredients/index' %>');
<% else %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% 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">
<%= link_to t(".heading_ingredient_list"), project_ingredients_path(@project),
class: 'icon icon-list' %>
<%= render partial: 'ingredients/contextual' %>
<%= render partial: 'ingredients/contextual', locals: {view: :nutrients} %>
</div>
<%= render partial: 'ingredients/import' %>

View File

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