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 group :development do
gem "web-console" gem "web-console"
end end

View File

@ -5,7 +5,6 @@ class QuantitiesController < ApplicationController
def index def index
@quantity = Quantity.new @quantity = Quantity.new
@quantities = @project.quantities
end end
def create def create
@ -14,7 +13,6 @@ class QuantitiesController < ApplicationController
flash[:notice] = 'Created new quantity' flash[:notice] = 'Created new quantity'
redirect_to project_quantities_url(@project) redirect_to project_quantities_url(@project)
else else
@quantities = @project.quantities
render :index render :index
end end
end end
@ -32,7 +30,8 @@ class QuantitiesController < ApplicationController
params.require(:quantity).permit( params.require(:quantity).permit(
:name, :name,
:description, :description,
:domain :domain,
:parent_id
) )
end end

View File

@ -5,4 +5,11 @@ module QuantitiesHelper
[translations[k.to_sym], k] [translations[k.to_sym], k]
end end
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 end

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ en:
field_shortname: 'Short name' field_shortname: 'Short name'
field_action: 'Action' field_action: 'Action'
field_domain: 'Domain' field_domain: 'Domain'
field_parent_quantity: 'Parent'
body_trackers: body_trackers:
index: index:
heading: 'Summary' heading: 'Summary'
@ -24,6 +25,7 @@ en:
diet: 'diet' diet: 'diet'
measurement: 'measurement' measurement: 'measurement'
exercise: 'exercise' exercise: 'exercise'
null_parent: '- none -'
units: units:
index: index:
heading: 'Units' heading: 'Units'

View File

@ -11,6 +11,10 @@ class CreateUnits < ActiveRecord::Migration
t.string :name t.string :name
t.string :description t.string :description
t.integer :domain 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 end
create_table :ingredients do |t| create_table :ingredients do |t|