diff --git a/Gemfile b/Gemfile index 223dde5..d52745d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +gem 'awesome_nested_set' + group :development do gem "web-console" end diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index bd0b097..454fc83 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -5,7 +5,6 @@ class QuantitiesController < ApplicationController def index @quantity = Quantity.new - @quantities = @project.quantities end def create @@ -14,7 +13,6 @@ class QuantitiesController < ApplicationController flash[:notice] = 'Created new quantity' redirect_to project_quantities_url(@project) else - @quantities = @project.quantities render :index end end @@ -32,7 +30,8 @@ class QuantitiesController < ApplicationController params.require(:quantity).permit( :name, :description, - :domain + :domain, + :parent_id ) end diff --git a/app/helpers/quantities_helper.rb b/app/helpers/quantities_helper.rb index 853aa12..6a4892c 100644 --- a/app/helpers/quantities_helper.rb +++ b/app/helpers/quantities_helper.rb @@ -5,4 +5,11 @@ module QuantitiesHelper [translations[k.to_sym], k] end end + + def parent_options + options = nested_set_options(Quantity, @quantity) do |i| + raw("#{' ' * i.level}#{i.name}") + end + options.unshift([t('.null_parent'), nil]) + end end diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 7343451..2d232f7 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -1,4 +1,6 @@ class Quantity < ActiveRecord::Base + acts_as_nested_set dependent: :nullify, scope: :project + enum domain: { diet: 0, measurement: 1, diff --git a/app/views/quantities/_form.html.erb b/app/views/quantities/_form.html.erb index 8c2b137..33cf33c 100644 --- a/app/views/quantities/_form.html.erb +++ b/app/views/quantities/_form.html.erb @@ -1,13 +1,10 @@ <%= error_messages_for @quantity %>
<%= f.text_field :name, size: 50, required: true %>
-<%= f.select :domain, domain_options, required: true %>
-<%= f.select :domain, domain_options, required: true %>
++ <%= f.select :parent_id, parent_options, required: true, label: :field_parent_quantity %> +
+<%= f.text_field :name, size: 25, required: true %>
<%= f.text_field :description, size: 200 %>
<%= q.name %> | -<%= q.domain %> | -<%= q.description %> | -<%= delete_link quantity_path(q), data: {} %> | -
<%= q.name %> | +<%= q.domain %> | +<%= q.description %> | +<%= delete_link quantity_path(q), data: {} %> | +