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 @@
<%= f.input :en_label, label: "Formula Label:" %> <%= f.input :uk_label, label: "Uk Formula Label:" %> + <%= f.input :priority, collection: Formula::PRIORITY_RANGE, wrapper: :custom_vertical_select %> <%= f.input :expression, as: :text, label: "Formula Expression:", input_html: { class: "formula", data: { controller: "textarea", action: "input->textarea#input" } } %> diff --git a/db/migrate/20241119142141_add_priority_to_formulas.rb b/db/migrate/20241119142141_add_priority_to_formulas.rb new file mode 100644 index 000000000..c415c36ea --- /dev/null +++ b/db/migrate/20241119142141_add_priority_to_formulas.rb @@ -0,0 +1,5 @@ +class AddPriorityToFormulas < ActiveRecord::Migration[7.2] + def change + add_column :formulas, :priority, :integer, default: 0, null: false + end +end diff --git a/spec/models/formula_spec.rb b/spec/models/formula_spec.rb index 5a36ad600..a1f3761f4 100644 --- a/spec/models/formula_spec.rb +++ b/spec/models/formula_spec.rb @@ -27,6 +27,7 @@ let(:local_prefix_formula) { "activerecord.errors.models.formula.attributes" } let(:calculator) { create(:calculator) } let!(:formula) { build(:formula, expression: "a + b", calculator: calculator) } + let!(:formula_with_priority_2) { build(:formula, expression: "a + b", calculator: calculator, priority: 2) } describe "validations" do it { is_expected.to validate_presence_of(:uk_label) } @@ -38,6 +39,7 @@ it { is_expected.to validate_presence_of(:en_unit) } it { is_expected.to validate_length_of(:en_unit).is_at_least(1).is_at_most(30) } it { is_expected.to validate_presence_of(:expression) } + it { is_expected.to validate_numericality_of(:priority).is_greater_than_or_equal_to(0) } context "formula has all fields initialized" do before do @@ -63,6 +65,14 @@ expect(formula).to_not be_valid expect(formula.errors[:expression]).to include(I18n.t("#{local_prefix_formula}.expression.mathematically_invalid")) end + + it "ensures priority has a default value of 0" do + expect(formula.priority).to eq(0) + end + + it "allows setting a specific priority value" do + expect(formula_with_priority_2.priority).to eq(2) + end end describe "associations" do