Added Quantity nesting with awesome_nested_set
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class Quantity < ActiveRecord::Base
|
||||
acts_as_nested_set dependent: :nullify, scope: :project
|
||||
|
||||
enum domain: {
|
||||
diet: 0,
|
||||
measurement: 1,
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user