Having exceptions is better than having silent bugs.
There are a few methods I've kept the same way they were.
The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.
We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").
Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.
There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.
For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
64 lines
1.7 KiB
Ruby
64 lines
1.7 KiB
Ruby
class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomization::BaseController
|
|
before_action :delete_translations, only: [:update]
|
|
|
|
def index
|
|
@tab = params[:tab] || :basic
|
|
@content = I18nContent.content_for(@tab)
|
|
end
|
|
|
|
def update
|
|
content_params.each do |content|
|
|
values = content[:values].slice(*translation_params)
|
|
|
|
unless values.empty?
|
|
values.each do |key, value|
|
|
locale = key.split("_").last
|
|
|
|
if value == t(content[:id], locale: locale) || value.match(/translation missing/)
|
|
next
|
|
else
|
|
text = I18nContent.find_or_create_by!(key: content[:id])
|
|
Globalize.locale = locale
|
|
text.update!(value: value)
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
redirect_to admin_site_customization_information_texts_path,
|
|
notice: t("flash.actions.update.translation")
|
|
end
|
|
|
|
private
|
|
|
|
def resource
|
|
I18nContent.find(content_params[:id])
|
|
end
|
|
|
|
def content_params
|
|
params.require(:contents).values
|
|
end
|
|
|
|
def delete_translations
|
|
languages_to_delete = params[:enabled_translations].select { |_, v| v == "0" }
|
|
.keys
|
|
|
|
languages_to_delete.each do |locale|
|
|
I18nContentTranslation.where(locale: locale).destroy_all
|
|
end
|
|
end
|
|
|
|
def translation_params
|
|
I18nContent.translated_attribute_names.product(enabled_translations).map do |attr_name, loc|
|
|
I18nContent.localized_attr_name_for(attr_name, loc)
|
|
end
|
|
end
|
|
|
|
def enabled_translations
|
|
params.fetch(:enabled_translations, {})
|
|
.select { |_, v| v == "1" }
|
|
.keys
|
|
end
|
|
end
|