From 446340be6675c4626565944da1992dc06fc2b716 Mon Sep 17 00:00:00 2001 From: Artem Stepanchenko Date: Tue, 3 Dec 2024 13:44:15 +0200 Subject: [PATCH 1/3] created edit functionality --- app/views/account/calculators/edit.html.erb | 84 +------------------ .../calculators/partials/_form.html.erb | 2 +- config/locales/en/en.yml | 4 +- config/locales/uk/uk.yml | 2 +- 4 files changed, 6 insertions(+), 86 deletions(-) diff --git a/app/views/account/calculators/edit.html.erb b/app/views/account/calculators/edit.html.erb index 10d648a0a..a52b5cdc4 100644 --- a/app/views/account/calculators/edit.html.erb +++ b/app/views/account/calculators/edit.html.erb @@ -1,83 +1,3 @@ -
- <%= simple_form_for @calculator, url: { action: 'update' }, wrapper: :horizontal_form, html: { novalidate: false } do |f| %> -
-
- <%= f.input :name, class: 'form-control', required: true %> -
-
- <%= f.input :preferable, class: 'form-control' %> -
-
- -

<%= t('.add_new_field_label') %>

- -
-
- <%= select_tag :kind, - options_from_collection_for_select(Field.kinds.keys, :itself, :capitalize), - id: 'calculator_fields_kind', - prompt: t('.form.select_field_kind_label'), - class: 'custom-select', - data: { 'fields-list' => { form: [NamedValue.name, Select.name], - parameter: [Value.name, RangeField.name, Select.name, Calculation.name], - result: [Calculation.name] } } %> -
-
- <%= select_tag :type, '', id: 'calculator_fields_type', - prompt: t('.form.select_field_type_label'), - class: 'custom-select', - disabled: true %> -
-
- <%= link_to t('buttons.create'), '#', - class: 'btn btn-success text-white disabled', - id: 'add-calculator-field', - data: { url: new_account_calculator_field_path(calculator_slug: @calculator) } %> -
-
- -
-

<%= t('.form.form_label') %>

-
- <% if @form_fields.any? %> - <%= f.fields_for :fields, @form_fields do |ff| %> - <%= render 'account/calculators/fields/form', f: ff %> - <% end %> - <% else %> -

<%= t('.form.no_fields_yet_label') %>

- <% end %> -
- -

<%= t('.form.parameters_label') %>

-
- <% if @parameter_fields.any? %> - <%= f.fields_for :fields, @parameter_fields do |ff| %> - <%= render 'account/calculators/fields/form', f: ff %> - <% end %> - <% else %> -

<%= t('.form.no_fields_yet_label') %>

- <% end %> -
- -

<%= t('.form.results_label') %>

-
- <% if @result_fields.any? %> - <%= f.fields_for :fields, @result_fields do |ff| %> - <%= render 'account/calculators/fields/form', f: ff %> - <% end %> - <% else %> -

<%= t('.form.no_fields_yet_label') %>

- <% end %> -
-
- -
-
- <%= f.button :submit, t('.form.update_calculator_button'), class: 'btn btn-green me-2' %> - <%= link_to account_calculators_path, class: 'btn btn-danger d-flex align-items-center justify-content-center' do %> - <%= t('buttons.cancel') %> - <% end %> -
-
- <% end %> +
+ <%= render "account/calculators/partials/form", calculator: @calculator %>
diff --git a/app/views/account/calculators/partials/_form.html.erb b/app/views/account/calculators/partials/_form.html.erb index 438b34288..d06262ad3 100644 --- a/app/views/account/calculators/partials/_form.html.erb +++ b/app/views/account/calculators/partials/_form.html.erb @@ -1,4 +1,4 @@ -<%= simple_form_for(@calculator, url: account_calculators_path) do |f| %> +<%= simple_form_for(calculator, url: polymorphic_path([:account, calculator], locale: I18n.locale)) do |f| %>
diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index 1ccf13530..22f6103b6 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -376,7 +376,7 @@ en: no_fields_yet_label: "No fields yet" update_calculator_button: "Update calculator" new: - create_calculator_button: "Create calculator" + create_calculator_button: "Save calculator" cancel_button: "Сancel" prohibited_to_update: " prevent the calculator from updating" prohibited_to_save: " перешкоджають збереженню калькулятора" @@ -473,7 +473,7 @@ en: no_fields_yet_label: "No fields yet" update_calculator_button: "Update calculator" new: - create_calculator_button: "Create calculator" + create_calculator_button: "Save calculator" feature_flags: submit_button: "Save" new_calculator_design: diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 06f683b9e..7f8fa9777 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -356,7 +356,7 @@ uk: no_fields_yet_label: "Полів поки нема" update_calculator_button: "Оновити калькулятор" new: - create_calculator_button: "Створити калькулятор" + create_calculator_button: "Зберегти калькулятор" cancel_button: "Скасувати" prohibited_to_update: " перешкоджають оновленню калькулятора" prohibited_to_save: " перешкоджають збереженню калькулятора" From 0c63949a6b7e26b6e9eab06dbbc37bed9b18c4c2 Mon Sep 17 00:00:00 2001 From: Bohdan Date: Sun, 8 Dec 2024 12:56:59 +0200 Subject: [PATCH 2/3] added specs for the update --- spec/requests/account/calculators_spec.rb | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/spec/requests/account/calculators_spec.rb b/spec/requests/account/calculators_spec.rb index c4f45a524..e38cc27d8 100644 --- a/spec/requests/account/calculators_spec.rb +++ b/spec/requests/account/calculators_spec.rb @@ -7,7 +7,8 @@ include_context :enable_calculators_constructor let!(:calculator) { create(:calculator) } - + let!(:new_attributes) { { calculator: { en_name: "new name" } } } + let!(:invalid_attributes) { { calculator: { en_name: nil } } } let(:user) { create(:user) } let(:locale) { "en" } let(:new_path) { new_account_calculator_path(locale: locale) } @@ -88,4 +89,27 @@ end end end + + describe "PATCH #update" do + context "with valid parameters" do + it "is successful" do + patch account_calculator_path(calculator), params: new_attributes + calculator.reload + + expect(calculator.en_name).to eq("new name") + expect(flash[:notice]).to eq(I18n.t("notifications.calculator_updated")) + end + end + + context "with invalid parameters" do + it "is not successful" do + expect do + patch account_calculator_path(calculator), params: invalid_attributes + end.not_to change(calculator, :en_name) + + expect(response).to be_unprocessable + expect(response).to render_template(:edit) + end + end + end end From fcca68e1e9deba84210d4a05f2f2ba83ce164eb6 Mon Sep 17 00:00:00 2001 From: Bohdan Date: Sun, 8 Dec 2024 12:58:29 +0200 Subject: [PATCH 3/3] fixed offenses --- spec/requests/account/calculators_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/requests/account/calculators_spec.rb b/spec/requests/account/calculators_spec.rb index e38cc27d8..875d3ea56 100644 --- a/spec/requests/account/calculators_spec.rb +++ b/spec/requests/account/calculators_spec.rb @@ -7,8 +7,8 @@ include_context :enable_calculators_constructor let!(:calculator) { create(:calculator) } - let!(:new_attributes) { { calculator: { en_name: "new name" } } } - let!(:invalid_attributes) { { calculator: { en_name: nil } } } + let!(:new_attributes) { { calculator: { en_name: "new name" }} } + let!(:invalid_attributes) { { calculator: { en_name: nil }} } let(:user) { create(:user) } let(:locale) { "en" } let(:new_path) { new_account_calculator_path(locale: locale) }