From 58a23c57646cf2314618363f393bbbd15127a687 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sat, 16 Sep 2023 17:33:30 +0300 Subject: [PATCH 1/7] add revert button --- app/assets/stylesheets/pages/feature_flags.scss | 10 +++++++++- app/controllers/account/site_settings_controller.rb | 12 ++++++++++++ app/views/account/site_settings/edit.html.slim | 2 ++ config/locales/en/en.yml | 2 ++ config/locales/uk/uk.yml | 2 ++ config/routes.rb | 1 + 6 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/pages/feature_flags.scss b/app/assets/stylesheets/pages/feature_flags.scss index 0f7bbc1cd..0ca26910a 100644 --- a/app/assets/stylesheets/pages/feature_flags.scss +++ b/app/assets/stylesheets/pages/feature_flags.scss @@ -48,9 +48,17 @@ input[type="submit"] { cursor: pointer; font-size: 16px; padding: 10px; - min-width: 100px; + min-width: 110px; &:hover { background-color: $dark_green; } } + +.btn-revert { + background-color: rgb(255, 65, 65) !important; + + &:hover { + background-color: darkred !important; + } +} diff --git a/app/controllers/account/site_settings_controller.rb b/app/controllers/account/site_settings_controller.rb index cfa3eb167..56541ac4e 100644 --- a/app/controllers/account/site_settings_controller.rb +++ b/app/controllers/account/site_settings_controller.rb @@ -17,6 +17,18 @@ def update end end + def revert + @site_setting = resource + + @site_setting.update(title: "Zero Waste", favicon: { + io: File.open("app/assets/images/logo_zerowaste.png"), + filename: "logo_zerowaste.png", + content_type: "image/png" + }) + + redirect_to edit_account_site_setting_path, notice: t("notifications.site_setting_reverted") + end + private def resource diff --git a/app/views/account/site_settings/edit.html.slim b/app/views/account/site_settings/edit.html.slim index 9b191640b..994b1771a 100644 --- a/app/views/account/site_settings/edit.html.slim +++ b/app/views/account/site_settings/edit.html.slim @@ -11,6 +11,8 @@ fieldset.bordered = f.submit t("buttons.update") i.fa.fa-times-circle + = button_to t("buttons.revert"), account_site_settings_revert_path, method: :get, class: "btn-revert mt-2" + fieldset.bordered legend.admin-legend = t(".site_features") diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index 510dc0c2f..6fcb900a9 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -36,6 +36,7 @@ en: update: 'Update' create: 'Create' cancel: 'Cancel' + revert: 'Revert' calculators: new_calculator: form: @@ -487,6 +488,7 @@ en: category_deleted: "Category was successfully destroyed." feature_flags_updated: "Features updated successfully" site_setting_updated: "Setting was successfully updated" + site_setting_reverted: "Setting was successfully reverted" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 639f4ca54..0d184c8cb 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -5,6 +5,7 @@ uk: update: 'Оновити' create: 'Створити' cancel: 'Скасувати' + revert: 'Скинути' calculators: new_calculator: form: @@ -493,6 +494,7 @@ uk: category_deleted: "Категорію було успішно видалено." feature_flags_updated: "Оновлено успішно" site_setting_updated: "Налаштування було успішно оновлено" + site_setting_reverted: "Налаштування було успішно скинуто" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/config/routes.rb b/config/routes.rb index 4c538abf9..14fbef8e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,6 +43,7 @@ resource :app_config, only: [:edit, :update] patch "/feature_flags", to: "feature_flags#update", as: "features_flags" resource :site_setting, only: [:edit, :update] + get "site_settings/revert", to: "site_settings#revert" scope module: :calculators do resources :calculators, only: [], param: :slug do From 75a13acd73734dbcb53a77e5ae7d9896321155b7 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sat, 16 Sep 2023 18:06:21 +0300 Subject: [PATCH 2/7] add tests for reverting default settings --- .../account/site_settings_controller.rb | 2 +- spec/factories/site_settings.rb | 5 ++++ spec/requests/site_setting_spec.rb | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/controllers/account/site_settings_controller.rb b/app/controllers/account/site_settings_controller.rb index 56541ac4e..767758921 100644 --- a/app/controllers/account/site_settings_controller.rb +++ b/app/controllers/account/site_settings_controller.rb @@ -20,7 +20,7 @@ def update def revert @site_setting = resource - @site_setting.update(title: "Zero Waste", favicon: { + @site_setting.update(title: "ZeroWaste", favicon: { io: File.open("app/assets/images/logo_zerowaste.png"), filename: "logo_zerowaste.png", content_type: "image/png" diff --git a/spec/factories/site_settings.rb b/spec/factories/site_settings.rb index 54ab71163..039ef3893 100644 --- a/spec/factories/site_settings.rb +++ b/spec/factories/site_settings.rb @@ -22,5 +22,10 @@ title { "Test title" } favicon { Rack::Test::UploadedFile.new("app/assets/images/logo_zerowaste.png", "image/png") } end + + trait :custom_setting do + title { "Custom Waste" } + favicon { Rack::Test::UploadedFile.new("app/assets/images/user.png", "image/png") } + end end end diff --git a/spec/requests/site_setting_spec.rb b/spec/requests/site_setting_spec.rb index 37c57731a..4e39ac6f9 100644 --- a/spec/requests/site_setting_spec.rb +++ b/spec/requests/site_setting_spec.rb @@ -52,4 +52,29 @@ end end end + + describe "GET revert" do + include_context :authorize_admin + + context "with valid params" do + let(:site_setting) { SiteSetting.current } + let(:site_setting_params) { FactoryBot.attributes_for(:site_setting, :custom_setting) } + let(:site_setting_default_params) { FactoryBot.attributes_for(:site_setting, :with_valid_site_setting) } + + before { site_setting.update(site_setting_params) } + + it "reverts site setting" do + get account_site_settings_revert_path + + expect(response).to redirect_to(edit_account_site_setting_path) + expect(flash[:notice]).to eq(I18n.t("notifications.site_setting_reverted")) + + expect(SiteSetting.current).to be_valid + expect(SiteSetting.current.favicon.attached?).to be_truthy + + expect(SiteSetting.current.title).to eq(site_setting_default_params[:title]) + expect(SiteSetting.current.favicon.filename).to eq(site_setting_default_params[:favicon].original_filename) + end + end + end end From 5f760dd6638b6bbf766ef670b30ef00f121f1cc2 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sun, 17 Sep 2023 12:29:11 +0300 Subject: [PATCH 3/7] change locales due to isuue #516 --- config/locales/en/en.yml | 2 +- config/locales/uk/uk.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index 6fcb900a9..d063a5e09 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -488,7 +488,7 @@ en: category_deleted: "Category was successfully destroyed." feature_flags_updated: "Features updated successfully" site_setting_updated: "Setting was successfully updated" - site_setting_reverted: "Setting was successfully reverted" + site_setting_reverted: "The title and favicon have been successfully reverted to default" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 0d184c8cb..a33705cc1 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -494,7 +494,7 @@ uk: category_deleted: "Категорію було успішно видалено." feature_flags_updated: "Оновлено успішно" site_setting_updated: "Налаштування було успішно оновлено" - site_setting_reverted: "Налаштування було успішно скинуто" + site_setting_reverted: "Заголовок і піктограму сайту успішно відновлено до стандартних" # time: # formats: # long: '%A, %d %B, %Y' From 433774d88f0b40b78c6c81570891f6ebbfd5f5cd Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sun, 17 Sep 2023 13:11:50 +0300 Subject: [PATCH 4/7] add confim and locales to it, move revert button to right bottom of the container --- .../stylesheets/pages/feature_flags.scss | 5 +++++ app/views/account/site_settings/edit.html.slim | 18 +++++++++--------- config/locales/en/en.yml | 1 + config/locales/uk/uk.yml | 1 + config/routes.rb | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/assets/stylesheets/pages/feature_flags.scss b/app/assets/stylesheets/pages/feature_flags.scss index 0ca26910a..0f74fc90b 100644 --- a/app/assets/stylesheets/pages/feature_flags.scss +++ b/app/assets/stylesheets/pages/feature_flags.scss @@ -62,3 +62,8 @@ input[type="submit"] { background-color: darkred !important; } } + +.right-bottom { + right: 0; + bottom: 0; +} diff --git a/app/views/account/site_settings/edit.html.slim b/app/views/account/site_settings/edit.html.slim index 994b1771a..e2c5288e5 100644 --- a/app/views/account/site_settings/edit.html.slim +++ b/app/views/account/site_settings/edit.html.slim @@ -1,17 +1,17 @@ fieldset.bordered legend.admin-legend = t(".site_settings") - = simple_form_for @site_setting, url: account_site_setting_path, html: { data: { controller: "handle-browser-tab" } } do |f| - .form-group.row - .col-12.has-float-label.my-auto - = f.input :title, class: "form-control", input_html: { data: { action: "input->handle-browser-tab#setTitle" } } - = f.input :favicon, class: "form-control", input_html: { data: { action: "change->handle-browser-tab#setIcon" } } - = render "browser_tab", site_setting: @site_setting + div.position-relative + = simple_form_for @site_setting, url: account_site_setting_path, html: { data: { controller: "handle-browser-tab" } } do |f| + .form-group.row + .col-12.has-float-label.my-auto + = f.input :title, class: "form-control", input_html: { data: { action: "input->handle-browser-tab#setTitle" } } + = f.input :favicon, class: "form-control", input_html: { data: { action: "change->handle-browser-tab#setIcon" } } + = render "browser_tab", site_setting: @site_setting - = f.submit t("buttons.update") - i.fa.fa-times-circle + = f.submit t("buttons.update"), class: "inline" - = button_to t("buttons.revert"), account_site_settings_revert_path, method: :get, class: "btn-revert mt-2" + = button_to t("buttons.revert"), account_site_settings_revert_path, onclick: "return confirm('#{j(t('.confirm_default'))}')", class: "btn-revert position-absolute right-bottom" fieldset.bordered legend.admin-legend diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index d063a5e09..07893db40 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -247,6 +247,7 @@ en: edit: site_settings: Site settings site_features: Site features + confirm_default: Are you sure you want to revert the title and favicon to their default settings? calculators: index: search_placeholder: "Search" diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index a33705cc1..66f04a6b6 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -254,6 +254,7 @@ uk: edit: site_settings: Налаштування сайту site_features: Особливості сайту + confirm_default: Ви впевнені, що бажаєте повернути налаштування за замовчуванням для назви та значка сайту? feature_flags: submit_button: "Зберегти" new_calculator_design: diff --git a/config/routes.rb b/config/routes.rb index 14fbef8e3..c9dfea1f2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,7 +43,7 @@ resource :app_config, only: [:edit, :update] patch "/feature_flags", to: "feature_flags#update", as: "features_flags" resource :site_setting, only: [:edit, :update] - get "site_settings/revert", to: "site_settings#revert" + post "site_settings/revert", to: "site_settings#revert" scope module: :calculators do resources :calculators, only: [], param: :slug do From 42b35cb1543190370b162b0edc47bdf02520ad3f Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sun, 15 Oct 2023 15:02:08 +0300 Subject: [PATCH 5/7] add rescue to revert if site settings won't be updated, fix routes and tests --- .../account/site_settings_controller.rb | 18 ++++++++++++------ app/views/account/site_settings/edit.html.slim | 2 +- config/locales/en/en.yml | 1 + config/locales/uk/uk.yml | 1 + config/routes.rb | 6 ++++-- spec/requests/site_setting_spec.rb | 4 ++-- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/controllers/account/site_settings_controller.rb b/app/controllers/account/site_settings_controller.rb index 767758921..c579734a4 100644 --- a/app/controllers/account/site_settings_controller.rb +++ b/app/controllers/account/site_settings_controller.rb @@ -20,13 +20,19 @@ def update def revert @site_setting = resource - @site_setting.update(title: "ZeroWaste", favicon: { - io: File.open("app/assets/images/logo_zerowaste.png"), - filename: "logo_zerowaste.png", - content_type: "image/png" - }) + begin + @site_setting.update(title: "ZeroWaste", favicon: { + io: File.open("app/assets/images/logo_zerowaste.png"), + filename: "logo_zerowaste.png", + content_type: "image/png" + }) + + flash[:notice] = t("notifications.site_setting_reverted") + rescue Errno::ENOENT + flash[:alert] = t("notifications.site_setting_not_reverted") + end - redirect_to edit_account_site_setting_path, notice: t("notifications.site_setting_reverted") + redirect_to edit_account_site_setting_path end private diff --git a/app/views/account/site_settings/edit.html.slim b/app/views/account/site_settings/edit.html.slim index e2c5288e5..9cce6b673 100644 --- a/app/views/account/site_settings/edit.html.slim +++ b/app/views/account/site_settings/edit.html.slim @@ -11,7 +11,7 @@ fieldset.bordered = f.submit t("buttons.update"), class: "inline" - = button_to t("buttons.revert"), account_site_settings_revert_path, onclick: "return confirm('#{j(t('.confirm_default'))}')", class: "btn-revert position-absolute right-bottom" + = button_to t("buttons.revert"), revert_account_site_setting_path, onclick: "return confirm('#{j(t('.confirm_default'))}')", class: "btn-revert position-absolute right-bottom" fieldset.bordered legend.admin-legend diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index 07893db40..8a87ef34e 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -490,6 +490,7 @@ en: feature_flags_updated: "Features updated successfully" site_setting_updated: "Setting was successfully updated" site_setting_reverted: "The title and favicon have been successfully reverted to default" + site_setting_not_reverted: "Unable to revert title and favicon" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 66f04a6b6..b3d140319 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -496,6 +496,7 @@ uk: feature_flags_updated: "Оновлено успішно" site_setting_updated: "Налаштування було успішно оновлено" site_setting_reverted: "Заголовок і піктограму сайту успішно відновлено до стандартних" + site_setting_not_reverted: "Неможливо відновити заголовок і піктограму сайту" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/config/routes.rb b/config/routes.rb index c9dfea1f2..716954184 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,8 +42,10 @@ resources :messages, only: [:index, :show] resource :app_config, only: [:edit, :update] patch "/feature_flags", to: "feature_flags#update", as: "features_flags" - resource :site_setting, only: [:edit, :update] - post "site_settings/revert", to: "site_settings#revert" + + resource :site_setting, only: [:edit, :update] do + post :revert + end scope module: :calculators do resources :calculators, only: [], param: :slug do diff --git a/spec/requests/site_setting_spec.rb b/spec/requests/site_setting_spec.rb index 4e39ac6f9..ae9c5d14a 100644 --- a/spec/requests/site_setting_spec.rb +++ b/spec/requests/site_setting_spec.rb @@ -53,7 +53,7 @@ end end - describe "GET revert" do + describe "GET #revert" do include_context :authorize_admin context "with valid params" do @@ -64,7 +64,7 @@ before { site_setting.update(site_setting_params) } it "reverts site setting" do - get account_site_settings_revert_path + post revert_account_site_setting_path expect(response).to redirect_to(edit_account_site_setting_path) expect(flash[:notice]).to eq(I18n.t("notifications.site_setting_reverted")) From 21ba004cc11d56458137885c0799dc0a343fce68 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sun, 15 Oct 2023 16:21:10 +0300 Subject: [PATCH 6/7] removed code smell, added negative test for revert --- app/controllers/account/site_settings_controller.rb | 2 +- spec/factories/site_settings.rb | 8 +++++--- spec/requests/site_setting_spec.rb | 13 +++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/account/site_settings_controller.rb b/app/controllers/account/site_settings_controller.rb index c579734a4..e5e51d2a9 100644 --- a/app/controllers/account/site_settings_controller.rb +++ b/app/controllers/account/site_settings_controller.rb @@ -28,7 +28,7 @@ def revert }) flash[:notice] = t("notifications.site_setting_reverted") - rescue Errno::ENOENT + rescue flash[:alert] = t("notifications.site_setting_not_reverted") end diff --git a/spec/factories/site_settings.rb b/spec/factories/site_settings.rb index 039ef3893..6a4881cb0 100644 --- a/spec/factories/site_settings.rb +++ b/spec/factories/site_settings.rb @@ -7,11 +7,13 @@ # created_at :datetime not null # updated_at :datetime not null # +IMAGE_TYPE = "image/png" + FactoryBot.define do factory :site_setting do trait :with_valid_site_setting do title { "ZeroWaste" } - favicon { Rack::Test::UploadedFile.new("app/assets/images/logo_zerowaste.png", "image/png") } + favicon { Rack::Test::UploadedFile.new("app/assets/images/logo_zerowaste.png", IMAGE_TYPE) } end trait :invalid_site_setting do @@ -20,12 +22,12 @@ trait :new_title do title { "Test title" } - favicon { Rack::Test::UploadedFile.new("app/assets/images/logo_zerowaste.png", "image/png") } + favicon { Rack::Test::UploadedFile.new("app/assets/images/logo_zerowaste.png", IMAGE_TYPE) } end trait :custom_setting do title { "Custom Waste" } - favicon { Rack::Test::UploadedFile.new("app/assets/images/user.png", "image/png") } + favicon { Rack::Test::UploadedFile.new("app/assets/images/user.png", IMAGE_TYPE) } end end end diff --git a/spec/requests/site_setting_spec.rb b/spec/requests/site_setting_spec.rb index ae9c5d14a..a3aa619e2 100644 --- a/spec/requests/site_setting_spec.rb +++ b/spec/requests/site_setting_spec.rb @@ -60,6 +60,8 @@ let(:site_setting) { SiteSetting.current } let(:site_setting_params) { FactoryBot.attributes_for(:site_setting, :custom_setting) } let(:site_setting_default_params) { FactoryBot.attributes_for(:site_setting, :with_valid_site_setting) } + let(:old_file_name) { "app/assets/images/logo_zerowaste.png" } + let(:new_file_name) { "app/assets/images/new_logo_zerowaste.png" } before { site_setting.update(site_setting_params) } @@ -75,6 +77,17 @@ expect(SiteSetting.current.title).to eq(site_setting_default_params[:title]) expect(SiteSetting.current.favicon.filename).to eq(site_setting_default_params[:favicon].original_filename) end + + it "doesn't reverts site setting if file not exist" do + File.rename(old_file_name, new_file_name) + + post revert_account_site_setting_path + + expect(response).to redirect_to(edit_account_site_setting_path) + expect(flash[:alert]).to eq(I18n.t("notifications.site_setting_not_reverted")) + + File.rename(new_file_name, old_file_name) + end end end end From a677f0ccea29703bd82d2642eb987b2a62a6ea0c Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 20 Oct 2023 21:40:10 +0300 Subject: [PATCH 7/7] removed useless statement in site settings controller --- .../account/site_settings_controller.rb | 18 ++++++------------ config/locales/en/en.yml | 1 - config/locales/uk/uk.yml | 1 - spec/requests/site_setting_spec.rb | 11 ----------- 4 files changed, 6 insertions(+), 25 deletions(-) diff --git a/app/controllers/account/site_settings_controller.rb b/app/controllers/account/site_settings_controller.rb index e5e51d2a9..767758921 100644 --- a/app/controllers/account/site_settings_controller.rb +++ b/app/controllers/account/site_settings_controller.rb @@ -20,19 +20,13 @@ def update def revert @site_setting = resource - begin - @site_setting.update(title: "ZeroWaste", favicon: { - io: File.open("app/assets/images/logo_zerowaste.png"), - filename: "logo_zerowaste.png", - content_type: "image/png" - }) - - flash[:notice] = t("notifications.site_setting_reverted") - rescue - flash[:alert] = t("notifications.site_setting_not_reverted") - end + @site_setting.update(title: "ZeroWaste", favicon: { + io: File.open("app/assets/images/logo_zerowaste.png"), + filename: "logo_zerowaste.png", + content_type: "image/png" + }) - redirect_to edit_account_site_setting_path + redirect_to edit_account_site_setting_path, notice: t("notifications.site_setting_reverted") end private diff --git a/config/locales/en/en.yml b/config/locales/en/en.yml index c3a5e35cb..2de4617f4 100644 --- a/config/locales/en/en.yml +++ b/config/locales/en/en.yml @@ -492,7 +492,6 @@ en: feature_flags_updated: "Features updated successfully" site_setting_updated: "Setting was successfully updated" site_setting_reverted: "The title and favicon have been successfully reverted to default" - site_setting_not_reverted: "Unable to revert title and favicon" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/config/locales/uk/uk.yml b/config/locales/uk/uk.yml index 8d533443b..8cc029b45 100644 --- a/config/locales/uk/uk.yml +++ b/config/locales/uk/uk.yml @@ -498,7 +498,6 @@ uk: feature_flags_updated: "Оновлено успішно" site_setting_updated: "Налаштування було успішно оновлено" site_setting_reverted: "Заголовок і піктограму сайту успішно відновлено до стандартних" - site_setting_not_reverted: "Неможливо відновити заголовок і піктограму сайту" # time: # formats: # long: '%A, %d %B, %Y' diff --git a/spec/requests/site_setting_spec.rb b/spec/requests/site_setting_spec.rb index a3aa619e2..168b0de6c 100644 --- a/spec/requests/site_setting_spec.rb +++ b/spec/requests/site_setting_spec.rb @@ -77,17 +77,6 @@ expect(SiteSetting.current.title).to eq(site_setting_default_params[:title]) expect(SiteSetting.current.favicon.filename).to eq(site_setting_default_params[:favicon].original_filename) end - - it "doesn't reverts site setting if file not exist" do - File.rename(old_file_name, new_file_name) - - post revert_account_site_setting_path - - expect(response).to redirect_to(edit_account_site_setting_path) - expect(flash[:alert]).to eq(I18n.t("notifications.site_setting_not_reverted")) - - File.rename(new_file_name, old_file_name) - end end end end