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 %>

diff --git a/app/views/quantities/index.html.erb b/app/views/quantities/index.html.erb index abfb4c9..43c0666 100644 --- a/app/views/quantities/index.html.erb +++ b/app/views/quantities/index.html.erb @@ -23,7 +23,7 @@

<%= t ".heading" %>

-<% if @quantities.any? %> +<% if Quantity.roots.any? %> @@ -34,13 +34,16 @@ - <% @quantities.each do |q| %> - - - - - - + <% Quantity.roots.each do |r| %> + <% Quantity.each_with_level(r.self_and_descendants) do |q, level| %> + 0 %>"> + + + + + + <% end %> <% end %>
<%= q.name %><%= q.domain %><%= q.description %><%= delete_link quantity_path(q), data: {} %>
<%= q.name %><%= q.domain %><%= q.description %><%= delete_link quantity_path(q), data: {} %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 276aad5..439c164 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,6 +4,7 @@ en: field_shortname: 'Short name' field_action: 'Action' field_domain: 'Domain' + field_parent_quantity: 'Parent' body_trackers: index: heading: 'Summary' @@ -24,6 +25,7 @@ en: diet: 'diet' measurement: 'measurement' exercise: 'exercise' + null_parent: '- none -' units: index: heading: 'Units' diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index 93fd1bc..94ea071 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -11,6 +11,10 @@ class CreateUnits < ActiveRecord::Migration t.string :name t.string :description t.integer :domain + # fields for awesome_nested_set + t.references :parent + t.integer :lft, :null => false, :index => true + t.integer :rgt, :null => false, :index => true end create_table :ingredients do |t|