From 70ce75579268ea644e797eee9e588865bd8acedf Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Tue, 26 Nov 2024 17:45:53 +0200 Subject: [PATCH 01/13] Style show page --- app/assets/stylesheets/pages/calculator.scss | 5 +++ app/controllers/calculators_controller.rb | 2 ++ .../calculators/calculation_service.rb | 8 ++++- .../partials/_calculation_results.html.erb | 27 +++++++-------- app/views/calculators/show.html.erb | 34 +++++++++++++------ 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/app/assets/stylesheets/pages/calculator.scss b/app/assets/stylesheets/pages/calculator.scss index 0dede5e06..2c425ccab 100644 --- a/app/assets/stylesheets/pages/calculator.scss +++ b/app/assets/stylesheets/pages/calculator.scss @@ -61,6 +61,11 @@ margin-right: 0px; } +.calculator-field { + background-color: $light_gray !important; + border: 0; +} + .flex-item { display: flex; flex-wrap: wrap; diff --git a/app/controllers/calculators_controller.rb b/app/controllers/calculators_controller.rb index 8e87d0e92..0901c96ed 100644 --- a/app/controllers/calculators_controller.rb +++ b/app/controllers/calculators_controller.rb @@ -14,6 +14,8 @@ def index def show @calculator = resource + add_breadcrumb t("breadcrumbs.home"), root_path + add_breadcrumb @calculator.name end def calculate diff --git a/app/services/calculators/calculation_service.rb b/app/services/calculators/calculation_service.rb index 081092b0f..ea2b89f1f 100644 --- a/app/services/calculators/calculation_service.rb +++ b/app/services/calculators/calculation_service.rb @@ -1,4 +1,6 @@ class Calculators::CalculationService + include ApplicationHelper + def initialize(calculator, inputs) @calculator = calculator @inputs = inputs.to_unsafe_h @@ -10,7 +12,11 @@ def perform @calculator.formulas.map do |formula| result = @dentaku.evaluate(formula.expression, @inputs) - { label: formula.en_label, result: result } + 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 end end end diff --git a/app/views/calculators/partials/_calculation_results.html.erb b/app/views/calculators/partials/_calculation_results.html.erb index 72c1d9215..5e6684407 100644 --- a/app/views/calculators/partials/_calculation_results.html.erb +++ b/app/views/calculators/partials/_calculation_results.html.erb @@ -1,14 +1,13 @@ -

Calculation Results

-
- <% results.each do |result| %> -
-

- <%= result[:label] %> -

- = -

- <%= result[:result] %> -

-
- <% end %> -
+
+

Calculation Results

+
+ <% results.each do |result| %> +
+ <%= image_tag "money_to_spent_2.png", class: "img-margin", alt: "icon" %> +

<%= result[:result] %>

+

<%= result[:en_unit] %>

+

<%= result[:label] %>

+
+ <% end %> +
+
\ No newline at end of file diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb index 489f4f024..ade816dde 100644 --- a/app/views/calculators/show.html.erb +++ b/app/views/calculators/show.html.erb @@ -1,21 +1,33 @@ -
-

Calculator <%= @calculator.name %>


+
+

Calculator <%= @calculator.name %> <%= @text %>

+
- <%= form_with url: calculate_calculator_path(@calculator) do |form| %> + <%= form_with html: { class: "simple_form_calculator"}, url: calculate_calculator_path(@calculator) do |form| %> +
<% @calculator.fields.each do |field| %> -
-

<%= form.label field.var_name, field.label %>

+
<%= form.label field.var_name, field.label %>
<% if field.kind == 'number' %> - <%= form.number_field "inputs[#{field.var_name}]", placeholder: field.label %> +
+ <%= form.number_field "inputs[#{field.var_name}]", + placeholder: field.label, + class: "required rounded w-100 calculator-field" + %> +
<% else %> - <%= form.select "inputs[#{field.var_name}]", options_from_collection_for_select(field.categories, :price, :name) %> + <%= form.select "inputs[#{field.var_name}]", + options_from_collection_for_select(field.categories, :price, :name), + {}, + class: "flex-row rounded flex-item w-100 form_fild calculator-field" %> <% end %> -

<% end %> -
- <%= form.submit "Calculate", class: "bg-blue-500 text-white font-semibold px-4 py-2 rounded hover:bg-blue-700" %> + <%= form.submit "Calculate", class: "calculate-btn result-btn" %>
<% end %> + <%= image_tag "scales.png", class: "scales_img", alt: "Scales" %> - <%= turbo_frame_tag "calc-results" %> +
+ +<%= turbo_frame_tag "calc-results" %> + +<%= render "layouts/description_block" %> \ No newline at end of file From 8744fe7506c3dc46820acc5c45222e563e854a27 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Tue, 26 Nov 2024 17:58:47 +0200 Subject: [PATCH 02/13] Fix formatting --- app/views/calculators/partials/_calculation_results.html.erb | 2 +- app/views/calculators/show.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/calculators/partials/_calculation_results.html.erb b/app/views/calculators/partials/_calculation_results.html.erb index 5e6684407..f436abc40 100644 --- a/app/views/calculators/partials/_calculation_results.html.erb +++ b/app/views/calculators/partials/_calculation_results.html.erb @@ -10,4 +10,4 @@
<% end %>
- \ No newline at end of file + diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb index ade816dde..9a92be166 100644 --- a/app/views/calculators/show.html.erb +++ b/app/views/calculators/show.html.erb @@ -30,4 +30,4 @@ <%= turbo_frame_tag "calc-results" %> -<%= render "layouts/description_block" %> \ No newline at end of file +<%= render "layouts/description_block" %> From 3bf95ba352f2506cf13204ee35740fe78aeac2f0 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Wed, 27 Nov 2024 13:24:59 +0200 Subject: [PATCH 03/13] Added calculation service helper test --- .../calculation_service_helper_spec.rb | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 spec/helpers/calculators/calculation_service_helper_spec.rb diff --git a/spec/helpers/calculators/calculation_service_helper_spec.rb b/spec/helpers/calculators/calculation_service_helper_spec.rb new file mode 100644 index 000000000..d095e2bb3 --- /dev/null +++ b/spec/helpers/calculators/calculation_service_helper_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Calculators::CalculationService, type: :helper do + 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: "одиниці") + ] + 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) + end + + describe "#perform" do + subject { described_class.new(calculator, inputs).perform } + + context "when locale is English" do + let(:locale_en) { true } + + 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" } + ]) + end + end + + context "when locale is Ukrainian" do + let(:locale_en) { false } + + it "returns results with Ukrainian labels and units" do + expect(subject).to eq([ + { label: "Додавання", result: 8, en_unit: "одиниці" }, + { label: "Множення", result: 15, en_unit: "одиниці" } + ]) + end + end + end + end + \ No newline at end of file From fa90e507c7075fa4ffa634f6e9b2914fa2ad65f0 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Wed, 27 Nov 2024 14:22:57 +0200 Subject: [PATCH 04/13] Added change color logic --- app/assets/stylesheets/pages/calculator.scss | 14 ++++ .../stylesheets/pages/feature_flags.scss | 6 -- .../partials/_calculation_results.html.erb | 14 ++-- .../_constructor_calculator_description.erb | 21 ++++++ app/views/calculators/show.html.erb | 16 +++-- config/tailwind.config.js | 8 +++ .../calculation_service_helper_spec.rb | 71 +++++++++---------- 7 files changed, 99 insertions(+), 51 deletions(-) create mode 100644 app/views/calculators/partials/show/_constructor_calculator_description.erb diff --git a/app/assets/stylesheets/pages/calculator.scss b/app/assets/stylesheets/pages/calculator.scss index 2c425ccab..960acabe5 100644 --- a/app/assets/stylesheets/pages/calculator.scss +++ b/app/assets/stylesheets/pages/calculator.scss @@ -16,6 +16,20 @@ font-style: normal; transition: transform 0.5s ease-in-out; max-width: 300px; + @include transition(all 0.5s ease-in-out); + + &:hover { + background-color: $matte_lime_green; + } +} + +.btn-nonito { + font-size: 14px; + letter-spacing: 2px; + text-transform: uppercase; + font-weight: 400; + font-family: "Nunito", sans-serif; + font-style: normal; } #calc { diff --git a/app/assets/stylesheets/pages/feature_flags.scss b/app/assets/stylesheets/pages/feature_flags.scss index a68714537..53a8ce168 100644 --- a/app/assets/stylesheets/pages/feature_flags.scss +++ b/app/assets/stylesheets/pages/feature_flags.scss @@ -41,7 +41,6 @@ } input[type="submit"] { - background-color: $success; border: none; border-radius: 4px; color: #fff; @@ -49,11 +48,6 @@ input[type="submit"] { font-size: 16px; padding: 10px; min-width: 110px; - @include transition(all 0.5s ease-in-out); - - &:hover { - background-color: $matte_lime_green; - } } .btn-grey { diff --git a/app/views/calculators/partials/_calculation_results.html.erb b/app/views/calculators/partials/_calculation_results.html.erb index f436abc40..ea52d5516 100644 --- a/app/views/calculators/partials/_calculation_results.html.erb +++ b/app/views/calculators/partials/_calculation_results.html.erb @@ -1,11 +1,17 @@ +<%# TODO: Delete this and use user provided value %> +<% color = "blue" %> + +<%# TODO: Delete this and use user provided value%> +<% formula_image = "money_to_spent_2.png" %> +
-

Calculation Results

+

Calculation Results

<% results.each do |result| %>
- <%= image_tag "money_to_spent_2.png", class: "img-margin", alt: "icon" %> -

<%= result[:result] %>

-

<%= result[:en_unit] %>

+ <%= image_tag "#{formula_image}", class: "img-margin", alt: "icon" %> +

<%= result[:result] %>

+

<%= result[:en_unit] %>

<%= result[:label] %>

<% end %> diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb new file mode 100644 index 000000000..46d09286b --- /dev/null +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -0,0 +1,21 @@ +<%# TODO: Delete this and use user provided value %> +<% color = "blue" %> + +
+
+
+

+ <%# TODO: Change description text and links %> + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus faucibus arcu iaculis placerat euismod. Pellentesque nibh arcu, varius ac arcu a, eleifend semper leo. Aliquam ultricies lacus at mi ornare, sit amet vestibulum est fermentum. Praesent sed dapibus elit. Curabitur congue ante non metus posuere porttitor. Etiam non lacus imperdiet turpis pellentesque viverra. Nulla in velit mi. Suspendisse molestie tempor ornare. + <%= link_to_external(text: "Calculate Method", url: t("layouts.description_block.method_link"), class: "text-#{color}") %> or + <%= link_to_external(text: "Contact Us", url: t("layouts.description_block.contact_link"), class: "text-#{color}") %> +

+
+
+ <%# TODO: Change link here %> + <%= link_to_external(text: t('layouts.description_block.use_less'), + url: t("layouts.description_block.use_less_link"), + class: "action-button rounded bg-#{color} text-white") %> +
+
+
diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb index 9a92be166..c29a6c530 100644 --- a/app/views/calculators/show.html.erb +++ b/app/views/calculators/show.html.erb @@ -1,5 +1,11 @@ +<%# TODO: Delete this and use user provided value %> +<% color = "blue" %> + +<%# TODO: Delete this and use user provided value %> +<% calculator_image = "scales.png" %> +
-

Calculator <%= @calculator.name %> <%= @text %>

+

Calculator <%= @calculator.name %> <%= @text %>

<%= form_with html: { class: "simple_form_calculator"}, url: calculate_calculator_path(@calculator) do |form| %> @@ -15,19 +21,19 @@
<% else %> <%= form.select "inputs[#{field.var_name}]", - options_from_collection_for_select(field.categories, :price, :name), + options_from_collection_for_select(field.categories, :price, :name), {}, class: "flex-row rounded flex-item w-100 form_fild calculator-field" %> <% end %> <% end %> - <%= form.submit "Calculate", class: "calculate-btn result-btn" %> + <%= form.submit "Calculate", class: "btn-nonito result-btn bg-#{color}" %>
<% end %> - <%= image_tag "scales.png", class: "scales_img", alt: "Scales" %> + <%= image_tag "#{calculator_image}", class: "scales_img", alt: "Scales" %>
<%= turbo_frame_tag "calc-results" %> -<%= render "layouts/description_block" %> +<%= render "calculators/partials/show/constructor_calculator_description" %> diff --git a/config/tailwind.config.js b/config/tailwind.config.js index 1f926b862..8e7fa7416 100644 --- a/config/tailwind.config.js +++ b/config/tailwind.config.js @@ -7,6 +7,14 @@ module.exports = { './app/javascript/**/*.js', './app/views/**/*.erb' ], + safelist: [ + { + pattern: /bg-.*/, + }, + { + pattern: /text-.*/, + }, + ], theme: { extend: { fontFamily: { diff --git a/spec/helpers/calculators/calculation_service_helper_spec.rb b/spec/helpers/calculators/calculation_service_helper_spec.rb index d095e2bb3..a5be8a8d0 100644 --- a/spec/helpers/calculators/calculation_service_helper_spec.rb +++ b/spec/helpers/calculators/calculation_service_helper_spec.rb @@ -3,43 +3,42 @@ require "rails_helper" RSpec.describe Calculators::CalculationService, type: :helper do - 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: "одиниці") - ] - 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) - end - - describe "#perform" do - subject { described_class.new(calculator, inputs).perform } - - context "when locale is English" do - let(:locale_en) { true } - - 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" } - ]) - end + 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: "одиниці") + ] + 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) + end + + describe "#perform" do + subject { described_class.new(calculator, inputs).perform } + + context "when locale is English" do + let(:locale_en) { true } + + 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" } + ]) end - - context "when locale is Ukrainian" do - let(:locale_en) { false } - - it "returns results with Ukrainian labels and units" do - expect(subject).to eq([ - { label: "Додавання", result: 8, en_unit: "одиниці" }, - { label: "Множення", result: 15, en_unit: "одиниці" } - ]) - end + end + + context "when locale is Ukrainian" do + let(:locale_en) { false } + + it "returns results with Ukrainian labels and units" do + expect(subject).to eq([ + { label: "Додавання", result: 8, en_unit: "одиниці" }, + { label: "Множення", result: 15, en_unit: "одиниці" } + ]) end end end - \ No newline at end of file +end From fca482e6dc2928a1d1686ce9b193097759595409 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Wed, 27 Nov 2024 19:03:47 +0200 Subject: [PATCH 05/13] fix dynamic colors adding --- app/assets/stylesheets/pages/calculator.scss | 8 ++++++++ .../calculators/partials/_calculation_results.html.erb | 10 +++++----- .../show/_constructor_calculator_description.erb | 10 +++++----- app/views/calculators/show.html.erb | 8 ++++---- config/tailwind.config.js | 8 -------- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/assets/stylesheets/pages/calculator.scss b/app/assets/stylesheets/pages/calculator.scss index 960acabe5..1c8178664 100644 --- a/app/assets/stylesheets/pages/calculator.scss +++ b/app/assets/stylesheets/pages/calculator.scss @@ -32,6 +32,14 @@ font-style: normal; } +.dynamic-text-color { + color: var(--calculator-color); +} + +.dynamic-background-color { + background-color: var(--calculator-color); +} + #calc { padding-top: 2px; margin-right: 10px; diff --git a/app/views/calculators/partials/_calculation_results.html.erb b/app/views/calculators/partials/_calculation_results.html.erb index ea52d5516..cd505d052 100644 --- a/app/views/calculators/partials/_calculation_results.html.erb +++ b/app/views/calculators/partials/_calculation_results.html.erb @@ -1,17 +1,17 @@ <%# TODO: Delete this and use user provided value %> -<% color = "blue" %> +<% color = "#088F8F" %> <%# TODO: Delete this and use user provided value%> <% formula_image = "money_to_spent_2.png" %> -
-

Calculation Results

+
+

Calculation Results

<% results.each do |result| %>
<%= image_tag "#{formula_image}", class: "img-margin", alt: "icon" %> -

<%= result[:result] %>

-

<%= result[:en_unit] %>

+

<%= result[:result] %>

+

<%= result[:unit] %>

<%= result[:label] %>

<% end %> diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index 46d09286b..c2db51c83 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -1,21 +1,21 @@ <%# TODO: Delete this and use user provided value %> -<% color = "blue" %> +<% color = "#088F8F" %> -
+

<%# TODO: Change description text and links %> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus faucibus arcu iaculis placerat euismod. Pellentesque nibh arcu, varius ac arcu a, eleifend semper leo. Aliquam ultricies lacus at mi ornare, sit amet vestibulum est fermentum. Praesent sed dapibus elit. Curabitur congue ante non metus posuere porttitor. Etiam non lacus imperdiet turpis pellentesque viverra. Nulla in velit mi. Suspendisse molestie tempor ornare. - <%= link_to_external(text: "Calculate Method", url: t("layouts.description_block.method_link"), class: "text-#{color}") %> or - <%= link_to_external(text: "Contact Us", url: t("layouts.description_block.contact_link"), class: "text-#{color}") %> + <%= link_to_external(text: "Calculate Method", url: t("layouts.description_block.method_link"), class: "dynamic-text-color") %> or + <%= link_to_external(text: "Contact Us", url: t("layouts.description_block.contact_link"), class: "dynamic-text-color") %>

<%# TODO: Change link here %> <%= link_to_external(text: t('layouts.description_block.use_less'), url: t("layouts.description_block.use_less_link"), - class: "action-button rounded bg-#{color} text-white") %> + class: "action-button rounded dynamic-background-color text-white") %>
diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb index c29a6c530..41cf922a9 100644 --- a/app/views/calculators/show.html.erb +++ b/app/views/calculators/show.html.erb @@ -1,11 +1,11 @@ <%# TODO: Delete this and use user provided value %> -<% color = "blue" %> +<% color = "#088F8F" %> <%# TODO: Delete this and use user provided value %> <% calculator_image = "scales.png" %> -
-

Calculator <%= @calculator.name %> <%= @text %>

+
+

Calculator <%= @calculator.name %> <%= @text %>

<%= form_with html: { class: "simple_form_calculator"}, url: calculate_calculator_path(@calculator) do |form| %> @@ -26,7 +26,7 @@ class: "flex-row rounded flex-item w-100 form_fild calculator-field" %> <% end %> <% end %> - <%= form.submit "Calculate", class: "btn-nonito result-btn bg-#{color}" %> + <%= form.submit "Calculate", class: "btn-nonito result-btn dynamic-background-color" %>
<% end %> <%= image_tag "#{calculator_image}", class: "scales_img", alt: "Scales" %> diff --git a/config/tailwind.config.js b/config/tailwind.config.js index 8e7fa7416..1f926b862 100644 --- a/config/tailwind.config.js +++ b/config/tailwind.config.js @@ -7,14 +7,6 @@ module.exports = { './app/javascript/**/*.js', './app/views/**/*.erb' ], - safelist: [ - { - pattern: /bg-.*/, - }, - { - pattern: /text-.*/, - }, - ], theme: { extend: { fontFamily: { From 6e9fd0dc35807f221073b2e78264abaeb7381c4c Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Wed, 27 Nov 2024 19:08:42 +0200 Subject: [PATCH 06/13] Change calculation_service logic --- app/models/formula.rb | 4 ++++ app/services/calculators/calculation_service.rb | 8 +------- .../calculators/calculation_service_helper_spec.rb | 13 +++++++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/models/formula.rb b/app/models/formula.rb index c8ef422cb..47713fda2 100644 --- a/app/models/formula.rb +++ b/app/models/formula.rb @@ -22,6 +22,8 @@ # fk_rails_... (calculator_id => calculators.id) # class Formula < ApplicationRecord + include Translatable + belongs_to :calculator validates_with FormulaValidator @@ -29,4 +31,6 @@ class Formula < ApplicationRecord 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 diff --git a/app/services/calculators/calculation_service.rb b/app/services/calculators/calculation_service.rb index ea2b89f1f..e888d1f47 100644 --- a/app/services/calculators/calculation_service.rb +++ b/app/services/calculators/calculation_service.rb @@ -1,6 +1,4 @@ class Calculators::CalculationService - include ApplicationHelper - def initialize(calculator, inputs) @calculator = calculator @inputs = inputs.to_unsafe_h @@ -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 diff --git a/spec/helpers/calculators/calculation_service_helper_spec.rb b/spec/helpers/calculators/calculation_service_helper_spec.rb index a5be8a8d0..966d1f1c6 100644 --- a/spec/helpers/calculators/calculation_service_helper_spec.rb +++ b/spec/helpers/calculators/calculation_service_helper_spec.rb @@ -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 @@ -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 @@ -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 From 2a5b0c20002dfb3f6b31922c165b6faa86cb1386 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Wed, 27 Nov 2024 19:13:21 +0200 Subject: [PATCH 07/13] Fix calculation_service_helper test --- .../helpers/calculators/calculation_service_helper_spec.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/helpers/calculators/calculation_service_helper_spec.rb b/spec/helpers/calculators/calculation_service_helper_spec.rb index 966d1f1c6..1e81b43a8 100644 --- a/spec/helpers/calculators/calculation_service_helper_spec.rb +++ b/spec/helpers/calculators/calculation_service_helper_spec.rb @@ -14,11 +14,14 @@ 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 - subject { described_class.new(calculator, inputs).perform } + subject do + I18n.with_locale(locale_en ? :en : :uk) do + described_class.new(calculator, inputs).perform + end + end context "when locale is English" do let(:locale_en) { true } From 2314773f4397a77f9c674f7e176ed81b76448153 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Thu, 28 Nov 2024 19:37:39 +0200 Subject: [PATCH 08/13] Change constructor calculatior description partial --- .../_constructor_calculator_description.erb | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index c2db51c83..b55aca0a0 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -1,21 +1,8 @@ <%# TODO: Delete this and use user provided value %> <% color = "#088F8F" %> -
-
-
-

- <%# TODO: Change description text and links %> - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus faucibus arcu iaculis placerat euismod. Pellentesque nibh arcu, varius ac arcu a, eleifend semper leo. Aliquam ultricies lacus at mi ornare, sit amet vestibulum est fermentum. Praesent sed dapibus elit. Curabitur congue ante non metus posuere porttitor. Etiam non lacus imperdiet turpis pellentesque viverra. Nulla in velit mi. Suspendisse molestie tempor ornare. - <%= link_to_external(text: "Calculate Method", url: t("layouts.description_block.method_link"), class: "dynamic-text-color") %> or - <%= link_to_external(text: "Contact Us", url: t("layouts.description_block.contact_link"), class: "dynamic-text-color") %> -

-
-
- <%# TODO: Change link here %> - <%= link_to_external(text: t('layouts.description_block.use_less'), - url: t("layouts.description_block.use_less_link"), - class: "action-button rounded dynamic-background-color text-white") %> -
-
-
+<% if false %> +
+ <%# TODO: Add partial here %> +
+<% end %> \ No newline at end of file From 6944ef38290104bd852260bdf6735297bd14fd4d Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Thu, 28 Nov 2024 19:39:58 +0200 Subject: [PATCH 09/13] Added space --- .../partials/show/_constructor_calculator_description.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index b55aca0a0..7b3c87ff2 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -5,4 +5,4 @@
<%# TODO: Add partial here %>
-<% end %> \ No newline at end of file +<% end %> From e50181b087162c75ab1ab64851766e8695bb346d Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Thu, 28 Nov 2024 19:43:11 +0200 Subject: [PATCH 10/13] Change constructor calculator description partial --- .../show/_constructor_calculator_description.erb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index 7b3c87ff2..edcf7e277 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -1,8 +1,5 @@ -<%# TODO: Delete this and use user provided value %> -<% color = "#088F8F" %> - <% if false %> -
- <%# TODO: Add partial here %> -
+
+ <%# TODO: Add description partial here %> +
<% end %> From ba40fe91e26487a7002a9a8fa5e4ec158221936b Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Thu, 28 Nov 2024 19:51:38 +0200 Subject: [PATCH 11/13] Added comment --- .../partials/show/_constructor_calculator_description.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index edcf7e277..5107f5b74 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -1,3 +1,4 @@ +<%# TODO: Delete this if %> <% if false %>
<%# TODO: Add description partial here %> From 847f32edc12e9ccf9a645b494cc42c9dcfc99580 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Fri, 29 Nov 2024 14:54:25 +0200 Subject: [PATCH 12/13] Fix image and added alt I18n and change calculation result partial --- .../partials/_calculation_results.html.erb | 5 +- .../_constructor_calculator_description.erb | 13 ++++- app/views/calculators/show.html.erb | 53 ++++++++++--------- config/locales/en/en.yml | 1 + config/locales/uk/uk.yml | 1 + 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/app/views/calculators/partials/_calculation_results.html.erb b/app/views/calculators/partials/_calculation_results.html.erb index cd505d052..4e3312887 100644 --- a/app/views/calculators/partials/_calculation_results.html.erb +++ b/app/views/calculators/partials/_calculation_results.html.erb @@ -1,10 +1,7 @@ -<%# TODO: Delete this and use user provided value %> -<% color = "#088F8F" %> - <%# TODO: Delete this and use user provided value%> <% formula_image = "money_to_spent_2.png" %> -
+

Calculation Results

<% results.each do |result| %> diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index 5107f5b74..590b3cbbd 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -1,6 +1,15 @@ <%# TODO: Delete this if %> <% if false %> -
- <%# TODO: Add description partial here %> +
+
+
+

+ <%# TODO: Add description text and links %> +

+
+
+ <%# TODO: Add discription btn link here %> +
+
<% end %> diff --git a/app/views/calculators/show.html.erb b/app/views/calculators/show.html.erb index 41cf922a9..82295fe22 100644 --- a/app/views/calculators/show.html.erb +++ b/app/views/calculators/show.html.erb @@ -4,36 +4,37 @@ <%# TODO: Delete this and use user provided value %> <% calculator_image = "scales.png" %> -
-

Calculator <%= @calculator.name %> <%= @text %>

-
+
+
+

Calculator <%= @calculator.name %> <%= @text %>

+
- <%= form_with html: { class: "simple_form_calculator"}, url: calculate_calculator_path(@calculator) do |form| %> -
- <% @calculator.fields.each do |field| %> -
<%= form.label field.var_name, field.label %>
- <% if field.kind == 'number' %> -
- <%= form.number_field "inputs[#{field.var_name}]", - placeholder: field.label, - class: "required rounded w-100 calculator-field" - %> -
- <% else %> - <%= form.select "inputs[#{field.var_name}]", - options_from_collection_for_select(field.categories, :price, :name), - {}, - class: "flex-row rounded flex-item w-100 form_fild calculator-field" %> - <% end %> + <%= form_with html: { class: "simple_form_calculator"}, url: calculate_calculator_path(@calculator) do |form| %> +
+ <% @calculator.fields.each do |field| %> +
<%= form.label field.var_name, field.label %>
+ <% if field.kind == 'number' %> +
+ <%= form.number_field "inputs[#{field.var_name}]", + placeholder: field.label, + class: "required rounded w-100 calculator-field" + %> +
+ <% else %> + <%= form.select "inputs[#{field.var_name}]", + options_from_collection_for_select(field.categories, :price, :name), + {}, + class: "flex-row rounded flex-item w-100 form_fild calculator-field" %> + <% end %> + <% end %> + <%= form.submit "Calculate", class: "btn-nonito result-btn dynamic-background-color" %> +
<% end %> - <%= form.submit "Calculate", class: "btn-nonito result-btn dynamic-background-color" %> + <%= image_tag calculator_image, class: "scales_img", alt: t(".image_alt") %>
- <% end %> - <%= image_tag "#{calculator_image}", class: "scales_img", alt: "Scales" %> -
-
-<%= turbo_frame_tag "calc-results" %> + <%= turbo_frame_tag "calc-results" %> +
<%= render "calculators/partials/show/constructor_calculator_description" %> diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index 0c4a7bca0..728b2aea0 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -83,6 +83,7 @@ en: month_error_msg: "Please, select month" show: welcome_header: "Welcome to ZeroWaste" + image_alt: "Scales" buttons: to_main: "To main" calculate: "Calculate" diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 05a4d3bf8..cbbc7c58d 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -65,6 +65,7 @@ uk: will_buy_diapers: "Кількість підгузків, які ще будуть використані до моменту, коли дитина перейде на звичайну білизну" money_spent: "Вартість використаних підгузків" money_will_be_spent: "Вартість підгузків, які ще будуть використані до моменту, коли дитина перейде на звичайну білизну" + image_alt: "Ваги" buttons: to_main: "На головну" calculate: "Розрахувати" From e144e42e35ecf0bc03eb844162e848d76ca051c8 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Fri, 29 Nov 2024 17:24:00 +0200 Subject: [PATCH 13/13] Code formatting --- .../partials/_calculation_results.html.erb | 12 +++++++++--- .../show/_constructor_calculator_description.erb | 11 ++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/views/calculators/partials/_calculation_results.html.erb b/app/views/calculators/partials/_calculation_results.html.erb index 4e3312887..14199c497 100644 --- a/app/views/calculators/partials/_calculation_results.html.erb +++ b/app/views/calculators/partials/_calculation_results.html.erb @@ -7,9 +7,15 @@ <% results.each do |result| %>
<%= image_tag "#{formula_image}", class: "img-margin", alt: "icon" %> -

<%= result[:result] %>

-

<%= result[:unit] %>

-

<%= result[:label] %>

+

+ <%= result[:result] %> +

+

+ <%= result[:unit] %> +

+

+ <%= result[:label] %> +

<% end %>
diff --git a/app/views/calculators/partials/show/_constructor_calculator_description.erb b/app/views/calculators/partials/show/_constructor_calculator_description.erb index 590b3cbbd..fed35215a 100644 --- a/app/views/calculators/partials/show/_constructor_calculator_description.erb +++ b/app/views/calculators/partials/show/_constructor_calculator_description.erb @@ -1,15 +1,8 @@ <%# TODO: Delete this if %> <% if false %>
-
-
-

- <%# TODO: Add description text and links %> -

+
+ <%# TODO: ADD text here %>
-
- <%# TODO: Add discription btn link here %> -
-
<% end %>