Skip to content

Commit

Permalink
Added priority for formulas (#957)
Browse files Browse the repository at this point in the history
* Added priority for formuls

* Added field on view

* sorting

* fix

* remove redundant variable

---------

Co-authored-by: DanielVajnagi <[email protected]>
  • Loading branch information
KseniaGovorun and DanielVajnagi authored Jan 17, 2025
1 parent cdf3740 commit e558ac1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
)
Expand Down
2 changes: 1 addition & 1 deletion app/models/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/models/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div>
<%= 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" }
} %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241119142141_add_priority_to_formulas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPriorityToFormulas < ActiveRecord::Migration[7.2]
def change
add_column :formulas, :priority, :integer, default: 0, null: false
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
t.bigint "calculator_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "priority", default: 0, null: false
t.index ["calculator_id"], name: "index_formulas_on_calculator_id"
end

Expand Down
10 changes: 10 additions & 0 deletions spec/models/formula_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e558ac1

Please sign in to comment.