diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 6cdd56d..cba1cc9 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -4,43 +4,47 @@ class IngredientsController < ApplicationController menu_item :body_trackers helper :body_trackers + helper_method :current_view before_action :init_session_filters before_action :find_project_by_project_id, - only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients] + only: [:index, :new, :create, :nutrients, :filter, :import] before_action :find_quantity_by_quantity_id, only: [:toggle_column] - before_action :find_ingredient, only: [:destroy, :toggle] + before_action :find_ingredient, only: [:edit, :update, :destroy, :toggle] before_action :authorize def index + self.current_view = :ingredients + prepare_items + end + + def new @ingredient = @project.ingredients.new # passing attr for Nutrient after_initialize @ingredient.nutrients.new(ingredient: @ingredient) - - prepare_ingredients - @ingredients << @ingredient - end - - def nutrients - @ingredient = @project.ingredients.new - @ingredient.nutrients.new(ingredient: @ingredient) - prepare_nutrients - end - - def toggle_column - @project.nutrients_column_view.toggle_column!(@quantity) - prepare_nutrients end def create @ingredient = @project.ingredients.new(ingredient_params) if @ingredient.save flash[:notice] = 'Created new ingredient' - redirect_to :back + prepare_items else - prepare_ingredients @ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty? + render :new + end + end + + def edit + end + + def update + if @ingredient.update(ingredient_params) + flash[:notice] = 'Updated ingredient' + prepare_items render :index + else + render :edit end end @@ -48,25 +52,28 @@ class IngredientsController < ApplicationController if @ingredient.destroy flash[:notice] = 'Deleted ingredient' end - prepare_ingredients - render :toggle end def toggle @ingredient.toggle_hidden! - prepare_ingredients + prepare_items + end + + def nutrients + self.current_view = :nutrients + prepare_items + end + + def toggle_column + @project.nutrients_column_view.toggle_column!(@quantity) + prepare_items + render :index end def filter session[:i_filters] = params[:filters] - prepare_ingredients - render :toggle - end - - def filter_nutrients - session[:i_filters] = params[:filters] - prepare_nutrients - render :toggle_nutrient_column + prepare_items + render :index end def import @@ -216,4 +223,15 @@ 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 diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index 3c9a476..926823f 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -40,6 +40,8 @@ module IngredientsHelper end def action_links(i) + link_to(l(:button_edit), edit_ingredient_path(i, view_mode: current_view), + {remote: true, class: "icon icon-edit"}) + delete_link(ingredient_path(i), {remote: true, data: {}}) end end diff --git a/app/views/ingredients/_contextual.html.erb b/app/views/ingredients/_contextual.html.erb index 84eb9cf..0c22fcf 100644 --- a/app/views/ingredients/_contextual.html.erb +++ b/app/views/ingredients/_contextual.html.erb @@ -1,6 +1,7 @@ <% 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"), '#', class: 'icon icon-add', - onclick: '$("#add-ingredient").show(); $("#ingredient_name").focus(); return false;' %> + <%= link_to t(".link_new_ingredient"), + new_project_ingredient_path(@project, view_mode: current_view), + {remote: true, class: 'icon icon-add'} %> <% end %> diff --git a/app/views/ingredients/_edit_form.html.erb b/app/views/ingredients/_edit_form.html.erb new file mode 100644 index 0000000..bba364e --- /dev/null +++ b/app/views/ingredients/_edit_form.html.erb @@ -0,0 +1,15 @@ +<%= labelled_form_for @ingredient, + url: ingredient_path(@ingredient, view_mode: current_view), + method: :patch, remote: true, + html: {id: 'ingredient-edit-form', name: 'ingredient-edit-form'} do |f| %> + + <%= render partial: 'ingredients/form', locals: {f: f} %> + +
+ <%= submit_tag l(:button_save) %> + <%= link_to l(:button_cancel), "#", + onclick: '$(this).closest("tr").remove(); return false;' %> +
+<%= f.text_field :name, size: 40, required: true %>
-<%= f.text_area :comment, cols: 40, rows: 3, required: false, - style: "width: 100%;" %>
-- <%= f.number_field :ref_amount, size: 8, required: true, min: 0, - label: :field_reference %> - <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %> -
-<%= f.select :group, group_options, required: true %>
-- <%= f.select :source_id, source_options, required: false, include_blank: true %> -
-<%= f.text_field :source_ident, size: 25, required: false %>
-- <%= ff.select :quantity_id, quantity_options, - {include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %> - <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> - <%= ff.select :unit_id, unit_options, {label: ''} %> - <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> - <%= link_to t(".button_delete_nutrient"), '#', - class: 'icon icon-del', - style: (@ingredient.nutrients.length > 1 ? "" : "display:none"), - onclick: "deleteNutrient(); return false;" %> -
- <% end %> - <% end %> -- <%= link_to t(".button_add_nutrient"), '#', class: 'icon icon-add', - onclick: 'addNutrient(); return false;' %> -
+<%= f.text_field :name, size: 40, required: true %>
+<%= f.text_area :comment, cols: 40, rows: 3, required: false, + style: "width: 100%;" %>
++ <%= f.number_field :ref_amount, size: 8, required: true, min: 0, + label: :field_reference %> + <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %> +
+<%= f.select :group, group_options, required: true %>
+<%= f.select :source_id, source_options, + {required: false, include_blank: t('.null_source')} %>
<%= f.text_field :source_ident, size: 25, required: false %>
++ <%= ff.hidden_field :id %> + <%= ff.select :quantity_id, quantity_options, + {include_blank: true, required: true, label: (index > 0 ? '' : :field_nutrients)} %> + <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> + <%= ff.select :unit_id, unit_options, {label: ''} %> + <%= ff.hidden_field :_destroy %> + <%= link_to t(".button_delete_nutrient"), '#', + class: 'icon icon-del', + style: (@ingredient.nutrients.length > 1 ? "" : "display:none"), + onclick: "deleteNutrient(); return false;" %> +
+ <% end %> <% end %> -+ <%= link_to t(".button_new_nutrient"), '#', class: 'icon icon-add', + onclick: 'newNutrient(); return false;' %> +