From e558ac17261c4bf6ea8a13dd91c424899684bbe6 Mon Sep 17 00:00:00 2001 From: KseniaGovorun <88275015+KseniaGovorun@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:03:20 +0200 Subject: [PATCH] Added priority for formulas (#957) * Added priority for formuls * Added field on view * sorting * fix * remove redundant variable --------- Co-authored-by: DanielVajnagi <82052651+DanielVajnagi@users.noreply.github.com> --- app/controllers/account/calculators_controller.rb | 2 +- app/models/calculator.rb | 2 +- app/models/formula.rb | 7 ++++++- .../calculators/partials/_formula_fields.html.erb | 1 + db/migrate/20241119142141_add_priority_to_formulas.rb | 5 +++++ db/schema.rb | 1 + spec/models/formula_spec.rb | 10 ++++++++++ 7 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20241119142141_add_priority_to_formulas.rb diff --git a/app/controllers/account/calculators_controller.rb b/app/controllers/account/calculators_controller.rb index 01c86a219..554d62fea 100644 --- a/app/controllers/account/calculators_controller.rb +++ b/app/controllers/account/calculators_controller.rb @@ -82,7 +82,7 @@ def collect_fields_for_kind(kind) def calculator_params params.require(:calculator).permit( :id, :en_name, :uk_name, :color, - formulas_attributes: [:id, :expression, :en_label, :uk_label, :calculator_id, :en_unit, :uk_unit, :_destroy], + formulas_attributes: [:id, :expression, :en_label, :uk_label, :calculator_id, :en_unit, :uk_unit, :priority, :_destroy], fields_attributes: [:id, :en_label, :uk_label, :var_name, :kind, :_destroy, categories_attributes: [:id, :en_name, :uk_name, :price, :_destroy]] ) diff --git a/app/models/calculator.rb b/app/models/calculator.rb index 393cf8cbb..08cb20ae1 100644 --- a/app/models/calculator.rb +++ b/app/models/calculator.rb @@ -26,7 +26,7 @@ class Calculator < ApplicationRecord translates :name has_many :fields, dependent: :destroy - has_many :formulas, dependent: :destroy + has_many :formulas, -> { ordered_by_priority }, dependent: :destroy, inverse_of: :calculator accepts_nested_attributes_for :fields, allow_destroy: true accepts_nested_attributes_for :formulas, allow_destroy: true diff --git a/app/models/formula.rb b/app/models/formula.rb index 47713fda2..a1faf8708 100644 --- a/app/models/formula.rb +++ b/app/models/formula.rb @@ -24,13 +24,18 @@ class Formula < ApplicationRecord include Translatable - belongs_to :calculator + belongs_to :calculator, inverse_of: :formulas + + PRIORITY_RANGE = 0..10 validates_with FormulaValidator validates :uk_label, :en_label, :uk_unit, :en_unit, :expression, presence: true validates :uk_label, :en_label, length: { minimum: 3, maximum: 50 } validates :en_unit, :uk_unit, length: { minimum: 1, maximum: 30 } + validates :priority, numericality: { greater_than_or_equal_to: 0 } + + scope :ordered_by_priority, -> { order(:priority) } translates :label, :unit end diff --git a/app/views/account/calculators/partials/_formula_fields.html.erb b/app/views/account/calculators/partials/_formula_fields.html.erb index bee217020..f359fa7d9 100644 --- a/app/views/account/calculators/partials/_formula_fields.html.erb +++ b/app/views/account/calculators/partials/_formula_fields.html.erb @@ -3,6 +3,7 @@