diff --git a/app/controllers/account/calculators_controller.rb b/app/controllers/account/calculators_controller.rb index e7ae8c8d3..e43b2211b 100644 --- a/app/controllers/account/calculators_controller.rb +++ b/app/controllers/account/calculators_controller.rb @@ -83,7 +83,7 @@ def collect_fields_for_kind(kind) def calculator_params params.require(:calculator).permit( :id, :en_name, :uk_name, :color, :logo_picture, - 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 b1f36b417..cfe73599f 100644 --- a/app/models/calculator.rb +++ b/app/models/calculator.rb @@ -28,7 +28,7 @@ class Calculator < ApplicationRecord has_one_attached :logo_picture 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 @@