Skip to content

Commit

Permalink
use around action instead of redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Feb 21, 2024
1 parent dd84b4d commit 7168960
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
20 changes: 11 additions & 9 deletions app/controllers/concerns/amendments_enforce_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ module AmendmentsEnforceLocale

included do
# rubocop:disable Rails/LexicallyScopedActionFilter
before_action :enforce_locale, only: [:new, :create]
around_action :enforce_amendment_locale, except: [:reject, :promote, :review, :accept, :withdraw]
# rubocop:enable Rails/LexicallyScopedActionFilter

def enforce_locale
return unless amendable.component.settings.try(:amendments_enabled)
return unless amendable.component.current_settings.try(:amendment_creation_enabled)
return unless Rails.application.secrets.gpc[:enforce_original_amendments_locale]
def enforce_amendment_locale(&action)
if amendable.component.settings.try(:amendments_enabled) &&
amendable.component.current_settings.try(:amendment_creation_enabled) &&
Rails.application.secrets.gpc[:enforce_original_amendments_locale]
amendable_locale = amendable.title.keys.first

amendable_locale = amendable.title.keys.first
return if current_locale.to_s == amendable_locale
flash[:alert] = I18n.t("gpc.amendments.enforced_locale", lang: I18n.t("locale.name", locale: amendable_locale)) if current_locale.to_s != amendable_locale

flash[:alert] = I18n.t("gpc.amendments.enforced_locale", lang: I18n.t("locale.name", locale: amendable_locale))
redirect_to new_amend_path(amendable_gid: amendable_gid, locale: amendable.title.keys.first)
I18n.with_locale(amendable_locale, &action)
else
action.call
end
end
end
end
3 changes: 3 additions & 0 deletions spec/system/limit_amendments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def amendment_path
expect(page).not_to have_content("CRÉER UN PROJET D'AMENDEMENT")
expect(page).to have_field(with: "Proposal in english language")
expect(page).not_to have_field(with: "Proposition en langue anglaise")
fill_in "Title", with: "New Proposal in english language"
click_button "Create"
expect(page).to have_content("No similar emendations found")
end

context "and not enforced" do
Expand Down

0 comments on commit 7168960

Please sign in to comment.