1
0

Added Quantity nesting with awesome_nested_set

This commit is contained in:
cryptogopher 2019-09-08 20:00:35 +02:00
parent 96509d5285
commit b31ab9d16a
8 changed files with 35 additions and 19 deletions

View File

@ -1,3 +1,5 @@
gem 'awesome_nested_set'
group :development do
gem "web-console"
end

View File

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

View File

@ -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("#{'&ensp;' * i.level}#{i.name}")
end
options.unshift([t('.null_parent'), nil])
end
end

View File

@ -1,4 +1,6 @@
class Quantity < ActiveRecord::Base
acts_as_nested_set dependent: :nullify, scope: :project
enum domain: {
diet: 0,
measurement: 1,

View File

@ -1,13 +1,10 @@
<%= error_messages_for @quantity %>
<div class="box tabular">
<div class="splitcontent">
<div class="splitcontentleft">
<p><%= f.text_field :name, size: 50, required: true %></p>
</div>
<div class="splitcontentright">
<p><%= f.select :domain, domain_options, required: true %></p>
</div>
</div>
<p><%= f.select :domain, domain_options, required: true %></p>
<p>
<%= f.select :parent_id, parent_options, required: true, label: :field_parent_quantity %>
</p>
<p><%= f.text_field :name, size: 25, required: true %></p>
<p><%= f.text_field :description, size: 200 %></p>
</div>

View File

@ -23,7 +23,7 @@
</div>
<h2><%= t ".heading" %></h2>
<% if @quantities.any? %>
<% if Quantity.roots.any? %>
<table class="list">
<thead>
<tr>
@ -34,13 +34,16 @@
</tr>
</thead>
<tbody>
<% @quantities.each do |q| %>
<tr id="quantity-<%= q.id %>" class="quantity">
<td class="quantityname"><%= q.name %></td>
<td class="domain"><%= q.domain %></td>
<td class="description"><%= q.description %></td>
<td><%= delete_link quantity_path(q), data: {} %></td>
</tr>
<% Quantity.roots.each do |r| %>
<% Quantity.each_with_level(r.self_and_descendants) do |q, level| %>
<tr id="quantity-<%= q.id %>"
class="quantity <%= "project idnt idnt-#{level}" if level > 0 %>">
<td class="name"><span><%= q.name %></span></td>
<td class="domain"><%= q.domain %></td>
<td class="description"><%= q.description %></td>
<td><%= delete_link quantity_path(q), data: {} %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>

View File

@ -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'

View File

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