Skip to content

Commit

Permalink
Added field on view
Browse files Browse the repository at this point in the history
  • Loading branch information
KseniaGovorun committed Nov 21, 2024
1 parent b2a1847 commit d9cc512
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/models/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
class Formula < ApplicationRecord
belongs_to :calculator

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) }
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="nested-fields">
<%= 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, label: "Formula Expression:" %>

<%= f.input :uk_unit, label: "Uk Unit Label:" %>
Expand Down
6 changes: 4 additions & 2 deletions spec/models/formula_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
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) { build(:formula, expression: "a + b", calculator: calculator, priority: 2) }
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 @@ -39,6 +40,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 Down Expand Up @@ -70,7 +72,7 @@
end

it "allows setting a specific priority value" do
expect(formula_with_priority.priority).to eq(2)
expect(formula_with_priority_2.priority).to eq(2)
end
end

Expand Down

0 comments on commit d9cc512

Please sign in to comment.