Skip to content

Commit

Permalink
prevent changing locales on accepting amendments
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Feb 22, 2024
1 parent e232050 commit f67c330
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
16 changes: 14 additions & 2 deletions decidim-core/app/commands/decidim/amendable/accept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,20 @@ def update_amendable!
visibility: "public-only"
) do
@amendable.assign_attributes(form.emendation_params)
@amendable.title = { I18n.locale => form.emendation_params.with_indifferent_access[:title] }
@amendable.body = { I18n.locale => form.emendation_params.with_indifferent_access[:body] }
if @amendable.official?
# official proposals can be translated, when amending them, we want to keep the translations
# (admins will have to update it afterwards if needed) so we assign the original attributes first
@amendable.title = @emendation.amendable.title
@amendable.title[I18n.locale.to_s] = form.emendation_params.with_indifferent_access[:title]
@amendable.body = @emendation.amendable.body
@amendable.body[I18n.locale.to_s] = form.emendation_params.with_indifferent_access[:body]
else
# For non-official proposals, we only allow one locale, so we just assign the new values.
# Note that this will trigger the automatic translation if enabled
# effectively changing the original language in what the proposal was writen
@amendable.title = { I18n.locale => form.emendation_params.with_indifferent_access[:title] }
@amendable.body = { I18n.locale => form.emendation_params.with_indifferent_access[:body] }
end
@amendable.save!
@amendable
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class AmendmentsController < Decidim::ApplicationController
helper_method :amendment, :amendable, :emendation, :similar_emendations
before_action :ensure_is_draft_from_user, only: [:compare_draft, :edit_draft, :update_draft, :destroy_draft, :preview_draft, :publish_draft]

around_action :ensure_locale_on_review, only: [:review]

def new
raise ActionController::RoutingError, "Not Found" unless amendable

Expand Down Expand Up @@ -192,6 +194,16 @@ def withdraw

private

def ensure_locale_on_review
emendation_locale = (emendation.title.keys - ["machine_translations"]).first
if current_locale.to_s != emendation_locale
flash[:alert] = t("locale_changed", scope: "decidim.amendments.review", lang: t("locale.name", locale: emendation_locale))
end
I18n.with_locale(emendation_locale) do
yield
end
end

# GlobalID::SignedGlobalID parameter to locate the amendable resource.
# Needed for actions :new and :create, when there is no amendment yet.
def amendable_gid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= form.fields_for :emendation_params, form.object.emendation_params do |emendation_form| %>
<%= form_required_explanation %>

<% console %>
<% amendable.attributes.each do |key, value| %>
<% key = key.to_sym %>
<% if amendable.amendable_fields.include?(key) %>
Expand Down
1 change: 1 addition & 0 deletions decidim-core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ en:
back: Back
heading: Review the amendment
help_text: You are reviewing an amendment to the %{model_name}
locale_changed: This amendment is being made for the "%{lang}" language. The display language has been changed to prevent misunderstandings.
send: Accept amendment
update_draft:
error: There was a problem updating the amendment draft.
Expand Down

0 comments on commit f67c330

Please sign in to comment.