Skip to content

Commit

Permalink
Change calculation_service logic
Browse files Browse the repository at this point in the history
  • Loading branch information
olexandervanzuriak committed Nov 27, 2024
1 parent fca482e commit 6e9fd0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app/models/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
# fk_rails_... (calculator_id => calculators.id)
#
class Formula < ApplicationRecord
include Translatable

belongs_to :calculator

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 }

translates :label, :unit
end
8 changes: 1 addition & 7 deletions app/services/calculators/calculation_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class Calculators::CalculationService
include ApplicationHelper

def initialize(calculator, inputs)
@calculator = calculator
@inputs = inputs.to_unsafe_h
Expand All @@ -12,11 +10,7 @@ def perform
@calculator.formulas.map do |formula|
result = @dentaku.evaluate(formula.expression, @inputs)

if current_locale?(:en)
{ label: formula.en_label, result: result, en_unit: formula.en_unit }
else
{ label: formula.uk_label, result: result, en_unit: formula.uk_unit }
end
{ label: formula.label, result: result, unit: formula.unit }
end
end
end
13 changes: 7 additions & 6 deletions spec/helpers/calculators/calculation_service_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
let(:calculator) { instance_double("Calculator", formulas: formulas) }
let(:formulas) do
[
instance_double("Formula", expression: "x + y", en_label: "Addition", en_unit: "units", uk_label: "Додавання", uk_unit: "одиниці"),
instance_double("Formula", expression: "x * y", en_label: "Multiplication", en_unit: "units", uk_label: "Множення", uk_unit: "одиниці")
Formula.new(en_label: "Addition", en_unit: "units", uk_label: "Додавання", uk_unit: "одиниці", expression: "x + y"),
Formula.new(en_label: "Multiplication", en_unit: "units", uk_label: "Множення", uk_unit: "одиниці", expression: "x * y")
]
end
let(:inputs) { ActionController::Parameters.new({ x: 5, y: 3 }) }

before do
allow_any_instance_of(ApplicationHelper).to receive(:current_locale?).with(:en).and_return(locale_en)
I18n.locale = locale_en ? :en : :uk
end

describe "#perform" do
Expand All @@ -24,8 +25,8 @@

it "returns results with English labels and units" do
expect(subject).to eq([
{ label: "Addition", result: 8, en_unit: "units" },
{ label: "Multiplication", result: 15, en_unit: "units" }
{ label: "Addition", result: 8, unit: "units" },
{ label: "Multiplication", result: 15, unit: "units" }
])
end
end
Expand All @@ -35,8 +36,8 @@

it "returns results with Ukrainian labels and units" do
expect(subject).to eq([
{ label: "Додавання", result: 8, en_unit: "одиниці" },
{ label: "Множення", result: 15, en_unit: "одиниці" }
{ label: "Додавання", result: 8, unit: "одиниці" },
{ label: "Множення", result: 15, unit: "одиниці" }
])
end
end
Expand Down

0 comments on commit 6e9fd0d

Please sign in to comment.