diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index f97a4d3..7da1b49 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,6 +1,10 @@ class IngredientsController < ApplicationController + before_action :find_project_by_project_id, only: [:index, :create] + before_action :authorize def index + @ingredient = Ingredient.new + @ingredients = @project.ingredients end def create diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index dd54783..8ddcfa0 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -1,2 +1,14 @@ module IngredientsHelper + def unit_options + @project.units.map do |u| + [u.shortname, u.id] + end + end + + def group_options + translations = t('.groups') + Ingredient.groups.map do |k,v| + [translations[k.to_sym], k] + end + end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 700e9ec..8fda20d 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -1,2 +1,13 @@ class Ingredient < ActiveRecord::Base + enum group: { + meat: 0 + } + + belongs_to :project + + validates :project, associated: true + validates :name, presence: true, uniqueness: {scope: :project_id} + validates :ref_amount, numericality: {greater_than: 0} + validates :ref_unit, presence: true, associated: true + validates :group, inclusion: {in: groups.keys} end diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb new file mode 100644 index 0000000..63dd3cd --- /dev/null +++ b/app/views/ingredients/_form.html.erb @@ -0,0 +1,10 @@ +<%= error_messages_for @ingredient %> + +
+

<%= f.text_field :name, size: 40, required: true %>

+

+ <%= f.number_field :ref_amount, size: 8, required: true, min: 0 %> + <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %> +

+

<%= f.select :group, group_options, required: true %>

+
diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index 01c25b5..b31b83e 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -4,40 +4,45 @@
<% if User.current.allowed_to?(:manage_common, @project) %> - <%= link_to t(".heading_new_unit"), '#', :class => 'icon icon-add', - :onclick => 'showAndScrollTo("add-unit", "unit_shortname"); return false;' %> + <%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add', + :onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %> <% end %>
-
> -

<%= t ".heading_new_unit" %>

+
> +

<%= t ".heading_new_ingredient" %>

- <%= labelled_form_for @unit, - :url => project_units_path(@project), - :html => {:id => 'unit-form'} do |f| %> - <%= render :partial => 'units/form', :locals => { :f => f } %> + <%= labelled_form_for @ingredient, + :url => project_ingredients_path(@project), + :html => {:id => 'ingredient-form'} do |f| %> + <%= render :partial => 'ingredients/form', :locals => { :f => f } %> <%= submit_tag l(:button_create) %> - <%= link_to l(:button_cancel), "#", :onclick => '$("#add-unit").hide()' %> + <%= link_to l(:button_cancel), "#", :onclick => '$("#add-ingredient").hide()' %> <% end %>

<%= t ".heading" %>

-<% if @units.any? %> +<% if @ingredients.any? %> - + + + + - <% @units.each do |u| %> - - - - + <% @ingredients.each do |i| %> + + + + + + <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 41127d3..79b8dc7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,10 +1,12 @@ # English strings go here for Rails i18n en: body_trackers_menu_caption: 'Body trackers' - field_shortname: 'Short name' field_action: 'Action' + field_ref_amount: 'Reference amount' + field_group: 'Group' field_domain: 'Domain' field_parent_quantity: 'Parent' + field_shortname: 'Short name' activerecord: errors: models: @@ -25,6 +27,13 @@ en: link_units: 'Units' link_defaults: 'Load defaults' confirm_defaults: 'This will load default quantities and units. Continue?' + ingredients: + index: + heading: 'Ingredients' + heading_new_ingredient: 'New ingredient' + form: + groups: + meat: 'meat' quantities: index: heading: 'Quantities' diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index c07cab2..8bd7cca 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -22,9 +22,9 @@ class CreateUnits < ActiveRecord::Migration t.string :name t.decimal :ref_amount t.references :ref_unit - t.boolean :hidden - t.references :source t.integer :group + t.references :source + t.boolean :hidden end create_table :nutrients do |t| diff --git a/init.rb b/init.rb index a59b41b..e649544 100644 --- a/init.rb +++ b/init.rb @@ -13,13 +13,15 @@ Redmine::Plugin.register :body_tracking do project_module :body_tracking do permission :view_body_trackers, { :body_trackers => [:index], + :ingredients => [:index], + :quantities => [:index], :units => [:index], - :quantities => [:index] }, read: true permission :manage_common, { :body_trackers => [:defaults], + :ingredients => [:create, :destroy], + :quantities => [:create, :destroy], :units => [:create, :destroy], - :quantities => [:create, :destroy] }, require: :loggedin end diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index 9b04dcf..7a3ba46 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -1,8 +1,10 @@ module BodyTracking module ProjectPatch Project.class_eval do - has_many :units, dependent: :destroy + has_many :ingredients, dependent: :destroy + has_many :quantities, -> { order "lft" }, dependent: :destroy + has_many :units, dependent: :destroy end end end
<%= l(:field_shortname) %> <%= l(:field_name) %><%= l(:field_ref_amount) %><%= l(:field_ref_unit) %><%= l(:field_group) %><%= l(:field_source) %> <%= l(:field_action) %>
<%= u.shortname %><%= u.name %><%= delete_link unit_path(u), data: {} %>
<%= i.name %><%= i.ref_amount %> [<%= i.ref_unit %>]<%= i.group %><%= i.source %><%= delete_link ingredient_path(i), data: {} %>