Skip to content

Commit

Permalink
Merge pull request #545 from ita-social-projects/feature/revert_site_…
Browse files Browse the repository at this point in the history
…settings

feature/revert site settings
  • Loading branch information
loqimean authored Nov 8, 2023
2 parents 266fe42 + 31e39bf commit 23a95f5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 12 deletions.
15 changes: 14 additions & 1 deletion app/assets/stylesheets/pages/feature_flags.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ 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;
}
}

.right-bottom {
right: 0;
bottom: 0;
}
12 changes: 12 additions & 0 deletions app/controllers/account/site_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ def update
end
end

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"
})

redirect_to edit_account_site_setting_path, notice: t("notifications.site_setting_reverted")
end

private

def resource
Expand Down
18 changes: 10 additions & 8 deletions app/views/account/site_settings/edit.html.slim
Original file line number Diff line number Diff line change
@@ -1,15 +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"), revert_account_site_setting_path, onclick: "return confirm('#{j(t('.confirm_default'))}')", class: "btn-revert position-absolute right-bottom"

fieldset.bordered
legend.admin-legend
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ en:
update: 'Update'
create: 'Create'
cancel: 'Cancel'
revert: 'Revert'
calculators:
new_calculator:
form:
Expand Down Expand Up @@ -248,6 +249,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"
Expand Down Expand Up @@ -489,6 +491,7 @@ en:
category_deleted: "Category was successfully destroyed."
feature_flags_updated: "Features updated successfully"
site_setting_updated: "Settings were successfully updated"
site_setting_reverted: "The title and favicon have been successfully reverted to default"
# time:
# formats:
# long: '%A, %d %B, %Y'
Expand Down
3 changes: 3 additions & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ uk:
update: 'Оновити'
create: 'Створити'
cancel: 'Скасувати'
revert: 'Скинути'
calculators:
new_calculator:
form:
Expand Down Expand Up @@ -255,6 +256,7 @@ uk:
edit:
site_settings: Налаштування сайту
site_features: Особливості сайту
confirm_default: Ви впевнені, що бажаєте повернути налаштування за замовчуванням для назви та значка сайту?
feature_flags:
submit_button: "Зберегти"
new_calculator_design:
Expand Down Expand Up @@ -495,6 +497,7 @@ uk:
category_deleted: "Категорію було успішно видалено."
feature_flags_updated: "Оновлено успішно"
site_setting_updated: "Налаштування було успішно оновлено"
site_setting_reverted: "Заголовок і піктограму сайту успішно відновлено до стандартних"
# time:
# formats:
# long: '%A, %d %B, %Y'
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +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]

resource :site_setting, only: [:edit, :update] do
post :revert
end

scope module: :calculators do
resources :calculators, only: [], param: :slug do
Expand Down
11 changes: 9 additions & 2 deletions spec/factories/site_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +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_TYPE) }
end
end
end
27 changes: 27 additions & 0 deletions spec/requests/site_setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,31 @@
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) }
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) }

it "reverts site setting" do
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"))

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

0 comments on commit 23a95f5

Please sign in to comment.