Skip to content

Commit

Permalink
enforce original locale in amendments
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Feb 1, 2024
1 parent 13d4234 commit dbdbad1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ These ENV vars modifies the behavior of the application in some ways:
| `PROCESS_POLICY` | If defined, shows a menu directly to the process indicated. Use to be `Policy2021` |
| `ASSEMBLY_LEADERSHIP` | If defined, shows a menu directly to that assembly (used when leadership race is on). It also applies some custom design to the assembly children (each being an assembly contestant), particularly, it adds a "donate" button. Used to be `leadership-campaigns` |
| `ASSEMBLY_LEADERSHIP_EVENTS` | If defined, redirects the specified assembly to the first published component instead of showing the main info page of the assembly. Used to be `events` |
| `ENFORCE_ORIGINAL_AMENDMENTS_LOCALE` | If defined, when creating an amendment it will enforce users to do it in the same language as the original proposal |
| `CONTESTANTS_COMPONENTS` | IDs of components (separated by spaces) that will hold contestants. This applies a special design to the proposals page, removing filters for instance. It also adds a link to "donate" money to this particular candidate. |
| `ALWAYS_SHOW_DONATE_BUTTON` | if `true`, all proposals will show a generic donate button in the sidebar |
| `TRANSLATOR_API_KEY` | If defined, automatic translations are enabled. We use [DeepL API](https://www.deepl.com/pro-api) service, only api keys from there are valid |
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/concerns/amendments_enforce_locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module AmendmentsEnforceLocale
extend ActiveSupport::Concern

included do
# rubocop:disable Rails/LexicallyScopedActionFilter
before_action :enforce_locale, only: [:new, :create]
# 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.enforce_original_amendments_locale

amendable_locale = amendable.title.keys.first
return if locale.to_s == amendable_locale

# flash[:alert] = t("pending_limit_reached", scope: "decidim.decidim_awesome.amendments", emendation: translated_attribute(emendation.title))
flash[:alert] = t("gpc.amendments.enforced_locale", locale: amendable_locale)
redirect_to new_amend_path(amendable_gid: amendable_gid, locale: amendable.title.keys.first)
end
end
end
3 changes: 3 additions & 0 deletions config/initializers/gpc_overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# disables inviting external users if enabled
Decidim::Meetings::Admin::InvitesController.include(InvitesControllerOverride)

# ensures same language is enforce on amendments to proposals
Decidim::AmendmentsController.include(AmendmentsEnforceLocale)

# sends notifications for answering surveys
Decidim::Forms::AnswerQuestionnaire.include(AnswerQuestionnaireOverride)

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ en:
email_not_registered: Sorry, the email %{email} is not registered. Only existing users are allowed to login.
gpc:
amendments:
enforced_locale: This proposal was originally created in %{locale}. You can only submit amendments in the same language.
limit_modal:
extra_html: You can also be notified when the current amendment is accepted or rejected by <a href="#" class="js-limit-amendment-follow-proposal">following this proposal</a> (you will receive a notification/email when the amendment is accepted or rejected depending on your notification preferences).
campaign_space: "%{name} Campaign"
Expand Down
1 change: 1 addition & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ default: &default
disable_external_invites: <%= ENV["DISABLE_EXTERNAL_INVITES"] %>
questionnaire_notify_emails: <%= Decidim::Env.new("QUESTIONNAIRE_NOTIFY_EMAILS").to_array(separator: ' ').to_json %>
always_show_donate_button: <%= Decidim::Env.new("ALWAYS_SHOW_DONATE_BUTTON", true).to_boolean_string %>
enforce_original_amendments_locale: <%= Decidim::Env.new("ENFORCE_ORIGINAL_AMENDMENTS_LOCALE", true).to_boolean_string %>
components:
contestants: <%= Decidim::Env.new("CONTESTANTS_COMPONENTS").to_array(separator: ' ').map(&:to_i).to_json %>
processes:
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
config.order = :random

config.before do
I18n.available_locales = [:en, :es, :ca]
I18n.available_locales = [:en, :fr, :ca]
I18n.default_locale = :en
Decidim.available_locales = [:en, :es, :ca]
Decidim.available_locales = [:en, :fr, :ca]
Decidim.default_locale = :en
Capybara.server = :puma
end
Expand Down
33 changes: 32 additions & 1 deletion spec/system/limit_amendments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
let(:amendments_enabled) { true }
let(:amendment_creation_enabled) { true }
let(:logged_user) { user }
let(:enforce_locale) { true }

before do
allow(Rails.application.secrets).to receive(:enforce_original_amendments_locale).and_return(enforce_locale)
switch_to_host(organization.host)
login_as logged_user, scope: :user
visit_component
click_link proposal.title["en"]
end

def visit_component
Expand All @@ -49,6 +50,7 @@ def amendment_path

context "when there's pending amendments" do
it "cannot create a new one" do
click_link proposal.title["en"]
expect(page).to have_content(proposal.title["en"])
expect(page).to have_content(emendation.title["en"])
click_link "Amend"
Expand All @@ -65,6 +67,7 @@ def amendment_path
let(:logged_user) { creator }

it "can be accepted" do
click_link proposal.title["en"]
click_link "An emendation for the proposal"
click_link "Accept"
perform_enqueued_jobs do
Expand All @@ -84,6 +87,7 @@ def amendment_path
end

it "can be rejected" do
click_link proposal.title["en"]
click_link "An emendation for the proposal"
perform_enqueued_jobs do
click_link "Reject"
Expand All @@ -105,7 +109,32 @@ def amendment_path
context "when amendments are not limited" do
let(:limit_pending_amendments) { false }

context "when proposal original locale is not the users locale" do
let(:proposal) { create :proposal, users: [creator], component: component, title: { fr: "Proposal in french" } }

it "Enforces the original locale" do
click_link proposal.title["fr"]
click_link "Amend"

expect(page).not_to have_content("CREATE AMENDMENT DRAFT")
expect(page).to have_content("CRÉER UN PROJET D'AMENDEMENT")
end

context "and not enforced" do
let(:enforce_locale) { false }

it "does not enforce the original locale" do
click_link proposal.title["fr"]
click_link "Amend"

expect(page).to have_content("CREATE AMENDMENT DRAFT")
expect(page).not_to have_content("CRÉER UN PROJET D'AMENDEMENT")
end
end
end

it "can create a new one" do
click_link proposal.title["en"]
expect(page).to have_content(proposal.title["en"])
expect(page).to have_content(emendation.title["en"])
click_link "Amend"
Expand All @@ -119,6 +148,7 @@ def amendment_path
let(:logged_user) { creator }

it "can be accepted" do
click_link proposal.title["en"]
click_link "An emendation for the proposal"
click_link "Accept"
perform_enqueued_jobs do
Expand All @@ -133,6 +163,7 @@ def amendment_path
end

it "can be rejected" do
click_link proposal.title["en"]
click_link "An emendation for the proposal"
perform_enqueued_jobs do
click_link "Reject"
Expand Down

0 comments on commit dbdbad1

Please sign in to comment.