Added Ingredients import action
This commit is contained in:
parent
6e3cf2f189
commit
b69bdd6049
@ -1,5 +1,5 @@
|
|||||||
class IngredientsController < ApplicationController
|
class IngredientsController < ApplicationController
|
||||||
before_action :find_project_by_project_id, only: [:index, :create]
|
before_action :find_project_by_project_id, only: [:index, :create, :import]
|
||||||
before_action :find_ingredient, only: [:destroy]
|
before_action :find_ingredient, only: [:destroy]
|
||||||
before_action :authorize
|
before_action :authorize
|
||||||
|
|
||||||
@ -29,6 +29,9 @@ class IngredientsController < ApplicationController
|
|||||||
redirect_to project_ingredients_url(@project)
|
redirect_to project_ingredients_url(@project)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def import
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def ingredient_params
|
def ingredient_params
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
class Ingredient < ActiveRecord::Base
|
class Ingredient < ActiveRecord::Base
|
||||||
enum group: {
|
enum group: {
|
||||||
meat: 0
|
other: 0,
|
||||||
|
meat: 1
|
||||||
}
|
}
|
||||||
|
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
@ -27,8 +28,10 @@ class Ingredient < ActiveRecord::Base
|
|||||||
|
|
||||||
after_initialize do
|
after_initialize do
|
||||||
if new_record?
|
if new_record?
|
||||||
|
self.ref_amount ||= 100
|
||||||
units = self.project.units
|
units = self.project.units
|
||||||
self.ref_unit ||= units.find_by(shortname: 'g') || units.first
|
self.ref_unit ||= units.find_by(shortname: 'g') || units.first
|
||||||
|
self.group ||= :other
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -4,11 +4,34 @@
|
|||||||
|
|
||||||
<div class="contextual">
|
<div class="contextual">
|
||||||
<% if User.current.allowed_to?(:manage_common, @project) %>
|
<% if User.current.allowed_to?(:manage_common, @project) %>
|
||||||
|
<%= link_to t(".heading_import_ingredients"), '#', :class => 'icon icon-file',
|
||||||
|
:onclick => 'showAndScrollTo("import-ingredients", "filename"); return false;' %>
|
||||||
<%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add',
|
<%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add',
|
||||||
:onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %>
|
:onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="import-ingredients" style="display:none;">
|
||||||
|
<h2><%= t ".heading_import_ingredients" %></h2>
|
||||||
|
|
||||||
|
<%= form_tag import_project_ingredients_path(@project),
|
||||||
|
:multipart => true,
|
||||||
|
:id => 'import-form' do %>
|
||||||
|
<div class="box tabular">
|
||||||
|
<p>
|
||||||
|
<label><%= t ".label_import_select_csv_file" %></label>
|
||||||
|
<%= file_field_tag 'file' %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= t ".import_hints" %>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<%= submit_tag l(:button_import) %>
|
||||||
|
<%= link_to l(:button_cancel), "#", :onclick => '$("#import-ingredients").hide()' %>
|
||||||
|
<% end %>
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
|
<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
|
||||||
<h2><%= t ".heading_new_ingredient" %></h2>
|
<h2><%= t ".heading_new_ingredient" %></h2>
|
||||||
|
|
||||||
|
@ -36,11 +36,17 @@ en:
|
|||||||
ingredients:
|
ingredients:
|
||||||
index:
|
index:
|
||||||
heading: 'Ingredients'
|
heading: 'Ingredients'
|
||||||
|
heading_import_ingredients: 'Import'
|
||||||
heading_new_ingredient: 'New ingredient'
|
heading_new_ingredient: 'New ingredient'
|
||||||
|
label_import_select_csv_file: 'Select CSV file'
|
||||||
|
import_hints: 'CSV file has to include header with column names. Recognized column
|
||||||
|
names are: (1) ingredient attributes (name, ref_amount, ref_unit, group),
|
||||||
|
(2) quantities names with units short names in square brackets (e.g. proteins [g])'
|
||||||
form:
|
form:
|
||||||
button_add_nutrient: 'Add nutrient'
|
button_add_nutrient: 'Add nutrient'
|
||||||
button_delete_nutrient: 'Delete'
|
button_delete_nutrient: 'Delete'
|
||||||
groups:
|
groups:
|
||||||
|
other: 'other'
|
||||||
meat: 'meat'
|
meat: 'meat'
|
||||||
quantities:
|
quantities:
|
||||||
index:
|
index:
|
||||||
|
@ -6,7 +6,9 @@ resources :projects do
|
|||||||
resources :body_trackers, :only => [:index] do
|
resources :body_trackers, :only => [:index] do
|
||||||
post 'defaults', on: :collection
|
post 'defaults', on: :collection
|
||||||
end
|
end
|
||||||
resources :ingredients, :only => [:index, :create, :destroy]
|
resources :ingredients, :only => [:index, :create, :destroy] do
|
||||||
|
post 'import', on: :collection
|
||||||
|
end
|
||||||
resources :quantities, :only => [:index, :create, :destroy]
|
resources :quantities, :only => [:index, :create, :destroy]
|
||||||
resources :units, :only => [:index, :create, :destroy]
|
resources :units, :only => [:index, :create, :destroy]
|
||||||
end
|
end
|
||||||
|
2
init.rb
2
init.rb
@ -19,7 +19,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],
|
:ingredients => [:create, :destroy, :import],
|
||||||
:quantities => [:create, :destroy],
|
:quantities => [:create, :destroy],
|
||||||
:units => [:create, :destroy],
|
:units => [:create, :destroy],
|
||||||
}, require: :loggedin
|
}, require: :loggedin
|
||||||
|
Reference in New Issue
Block a user