1
0

Replaced Quantity 'primary' attr with ColumnView model

This commit is contained in:
cryptogopher
2019-12-14 23:56:36 +01:00
parent ccf26d1830
commit 0c0ca1d286
19 changed files with 91 additions and 65 deletions

View File

@@ -6,7 +6,7 @@ class IngredientsController < ApplicationController
before_action :init_session_filters
before_action :find_project_by_project_id,
only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients]
before_action :find_quantity, only: [:toggle_nutrient_column]
before_action :find_quantity_by_quantity_id, only: [:toggle_column]
before_action :find_ingredient, only: [:destroy, :toggle]
before_action :authorize
@@ -25,8 +25,8 @@ class IngredientsController < ApplicationController
prepare_nutrients
end
def toggle_nutrient_column
@quantity.toggle_primary!
def toggle_column
@project.nutrients_column_view.toggle_column!(@quantity)
prepare_nutrients
end
@@ -207,7 +207,7 @@ class IngredientsController < ApplicationController
end
def prepare_nutrients
@quantities = @project.quantities.diet.where(primary: true)
@quantities = @project.nutrients_column_view.quantities
ingredients, requested_n, extra_n, @formula_q = @project.ingredients
.filter(@project, session[:i_filters], @quantities)

View File

@@ -3,8 +3,9 @@ class MeasurementsController < ApplicationController
before_action :init_session_filters
before_action :find_project_by_project_id, only: [:index, :new, :create]
before_action :find_quantity, only: [:toggle_quantity]
before_action :find_measurement, only: [:edit, :update, :destroy, :retake, :readouts]
before_action :find_quantity_by_quantity_id, only: [:toggle_column]
before_action :find_measurement,
only: [:edit, :update, :destroy, :retake, :readouts, :toggle_column]
before_action :authorize
def index
@@ -61,8 +62,8 @@ class MeasurementsController < ApplicationController
prepare_readouts
end
def toggle_quantity
@quantity.toggle_primary!
def toggle_column
@measurement.column_view.toggle_column!(@quantity)
prepare_readouts
end
@@ -103,7 +104,7 @@ class MeasurementsController < ApplicationController
end
def prepare_readouts
@quantities = @project.quantities.measurement.where(primary: true)
@quantities = @measurement.column_view.quantities
@measurements, @requested_r, @extra_r, @formula_q = @project.measurements
.includes(:source)
.filter(session[:m_filters], @quantities)

View File

@@ -3,7 +3,7 @@ class QuantitiesController < ApplicationController
before_action :init_session_filters
before_action :find_project_by_project_id, only: [:index, :parents, :create, :filter]
before_action :find_quantity, only: [:edit, :update, :destroy, :toggle, :move]
before_action :find_quantity, only: [:edit, :update, :destroy, :move]
before_action :authorize
def index
@@ -55,11 +55,6 @@ class QuantitiesController < ApplicationController
render :index
end
def toggle
@quantity.toggle_primary!
prepare_quantities
end
def move
direction = params[:direction].to_sym
case direction
@@ -90,8 +85,7 @@ class QuantitiesController < ApplicationController
:parent_id,
:name,
:description,
:formula,
:primary
:formula
)
end

View File

@@ -5,10 +5,11 @@ module IngredientsHelper
end
end
def nutrient_column_options
def toggle_column_options
disabled = []
enabled_columns = @project.nutrients_column_view.quantities
options = nested_set_options(@project.quantities.diet) do |q|
disabled << q.id if q.primary
disabled << q.id if enabled_columns.include?(q)
raw("#{'&ensp;' * q.level}#{q.name}")
end
options_for_select(options, disabled: disabled)

View File

@@ -12,10 +12,11 @@ module MeasurementsHelper
amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"
end
def quantity_toggle_options
def toggle_column_options
disabled = []
enabled_columns = @measurement.column_view.quantities
options = nested_set_options(@project.quantities.measurement) do |q|
disabled << q.id if q.primary
disabled << q.id if enabled_columns.include?(q)
raw("#{'&ensp;' * q.level}#{q.name}")
end
options_for_select(options, disabled: disabled)

View File

@@ -1,2 +1,17 @@
class ColumnView < ActiveRecord::Base
enum domain: Quantity.domains
belongs_to :project, required: true
has_and_belongs_to_many :quantities
validates :name, presence: true, uniqueness: {scope: :domain}
validates :domain, inclusion: {in: domains.keys}
# TODO: enforce column_view - quantity 'domain' identity
def toggle_column!(q)
column = self.quantities.find(q.id)
self.quantites.destroy(column)
rescue ActiveRecord::RecordNotFound
self.quantities.create!(quantity: q)
end
end

View File

@@ -28,6 +28,33 @@ class Measurement < ActiveRecord::Base
end
end
# Copy/rename ColumnView on Measurement rename
after_save do
old_column_view = self.project.column_views
.find_by(name: self.name_was, domain: :measurement)
return unless old_column_view
if self.project.measurements.exists?(name: self.name_was)
return unless old_column_view.quantities.exist?
self.column_view.quantities.create(old_column_view.quantities)
self.column_view.save!
else
old_column_view.name = self.name
old_column_view.save!
end
end, if: :name_changed?
# Destroy ColumnView after last Measurement destruction
after_destroy do
unless self.project.measurements.exists?(name: self.name)
self.column_view.destroy!
end
end
def column_view
self.project.column_views.find_or_create_by(name: self.name, domain: :measurement)
end
def toggle_hidden!
self.toggle!(:hidden)
end

View File

@@ -15,6 +15,7 @@ class Quantity < ActiveRecord::Base
acts_as_nested_set dependent: :destroy, scope: :project
belongs_to :project, required: false
has_and_belongs_to_many :column_views
validates :name, presence: true, uniqueness: {scope: :project_id}
validates :domain, inclusion: {in: domains.keys}
@@ -26,7 +27,6 @@ class Quantity < ActiveRecord::Base
after_initialize do
if new_record?
self.domain ||= :diet
self.primary = false if self.primary.nil?
end
end
@@ -46,10 +46,6 @@ class Quantity < ActiveRecord::Base
end
end
def toggle_primary!
self.toggle!(:primary)
end
def formula_quantities
Formula.new(self.project, self.formula).get_quantities
end

View File

@@ -1,15 +1,15 @@
<fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<div>
<%= form_tag toggle_nutrient_column_project_ingredients_path(@project),
id: 'nutrient-column-add-form', name: 'nutrient-column-add-form',
<%= form_tag toggle_column_project_ingredients_path(@project),
id: 'toggle-column-form', name: 'toggle-column-form',
method: :post, remote: true do %>
<table>
<tr>
<td style="width:100%"></td>
<td>
<%= select_tag 'id', nutrient_column_options %>
<%= select_tag 'id', toggle_column_options %>
</td>
<td>
<%= submit_tag l(:button_add) %>
@@ -27,7 +27,7 @@
<% @quantities.each do |q| %>
<td class="action" style="width:<%= 100/total_width %>%">
<%= link_to l(:button_hide),
toggle_nutrient_column_project_ingredients_path(@project, id: q.id),
toggle_column_project_ingredients_path(@project, quantity_id: q.id),
{class: "icon icon-close", method: :post, remote: true} %>
</td>
<% end %>

View File

@@ -1,15 +1,15 @@
<fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<div>
<%= form_tag toggle_quantity_project_measurements_path(@project),
id: 'quantity-toggle-form', name: 'quantity-toggle-form',
<%= form_tag toggle_column_measurement_path(@measurement),
id: 'toggle-column-form', name: 'toggle-column-form',
method: :post, remote: true do %>
<table>
<tr>
<td style="width:100%"></td>
<td>
<%= select_tag 'id', quantity_toggle_options %>
<%= select_tag 'id', toggle_column_options %>
</td>
<td>
<%= submit_tag l(:button_add) %>
@@ -27,7 +27,7 @@
<% @quantities.each do |q| %>
<td class="action" style="width:<%= 100/total_width %>%">
<%= link_to l(:button_hide),
toggle_quantity_project_measurements_path(@project, id: q.id),
toggle_column_measurement_path(@measurement, quantity_id: q.id),
{class: "icon icon-close", method: :post, remote: true} %>
</td>
<% end %>

View File

@@ -13,5 +13,4 @@
<p><%= f.text_field :description, style: "width: 100%;" %></p>
<p><%= f.text_field :formula, placeholder: t('.formula_placeholder'),
style: "width: 100%;" %></p>
<p><%= f.check_box :primary %></p>
</div>

View File

@@ -19,18 +19,9 @@
next if q.new_record?
quantity_class = "quantity"
quantity_class += " project idnt idnt-#{level+1}"
quantity_class += " primary" if q.primary
%>
<tr id="quantity-<%= q.id %>" class="<%= quantity_class %>">
<td class="name">
<%= link_to '', toggle_quantity_path(q), {
remote: true,
method: :post,
class: "icon #{q.primary ? "icon-fav" : "icon-fav-off"}"
}
%>
<%= q.name %>
</td>
<td class="name"><%= q.name %></td>
<td class="order">
<% [:up, :down, :left, :right].each do |direction| %>
<%=