diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index ab0cd0d..6989c14 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,5 +1,5 @@ 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 :authorize @@ -29,6 +29,9 @@ class IngredientsController < ApplicationController redirect_to project_ingredients_url(@project) end + def import + end + private def ingredient_params diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index d79ea5c..d65b3da 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -1,6 +1,7 @@ class Ingredient < ActiveRecord::Base enum group: { - meat: 0 + other: 0, + meat: 1 } belongs_to :project @@ -27,8 +28,10 @@ class Ingredient < ActiveRecord::Base after_initialize do if new_record? + self.ref_amount ||= 100 units = self.project.units self.ref_unit ||= units.find_by(shortname: 'g') || units.first + self.group ||= :other end end diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index d7d0ba1..8c55403 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -4,11 +4,34 @@
<% 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', :onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %> <% end %>
+ +
>

<%= t ".heading_new_ingredient" %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 8b9d343..53324f9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -36,11 +36,17 @@ en: ingredients: index: heading: 'Ingredients' + heading_import_ingredients: 'Import' 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: button_add_nutrient: 'Add nutrient' button_delete_nutrient: 'Delete' groups: + other: 'other' meat: 'meat' quantities: index: diff --git a/config/routes.rb b/config/routes.rb index 9cfaa3e..884543f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,9 @@ resources :projects do resources :body_trackers, :only => [:index] do post 'defaults', on: :collection 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 :units, :only => [:index, :create, :destroy] end diff --git a/init.rb b/init.rb index e649544..4772de5 100644 --- a/init.rb +++ b/init.rb @@ -19,7 +19,7 @@ Redmine::Plugin.register :body_tracking do }, read: true permission :manage_common, { :body_trackers => [:defaults], - :ingredients => [:create, :destroy], + :ingredients => [:create, :destroy, :import], :quantities => [:create, :destroy], :units => [:create, :destroy], }, require: :loggedin