From 5dc0f7f0547bc6614bb4e3e22c257dde8196b981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Tue, 19 Feb 2019 13:13:19 +0100 Subject: [PATCH] Add :except and :only options to translatable_params method Allow to choose among resource model translatable attributes. --- app/controllers/concerns/translatable.rb | 13 ++++++++----- app/controllers/proposals_controller.rb | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/controllers/concerns/translatable.rb b/app/controllers/concerns/translatable.rb index 94cdcaaab..533b8db22 100644 --- a/app/controllers/concerns/translatable.rb +++ b/app/controllers/concerns/translatable.rb @@ -3,10 +3,13 @@ module Translatable private - def translation_params(resource_model) - { - translations_attributes: [:id, :_destroy, :locale] + - resource_model.translated_attribute_names - } + def translation_params(resource_model, options = {}) + attributes = [:id, :locale, :_destroy] + if options[:only] + attributes += [*options[:only]] + else + attributes += resource_model.translated_attribute_names + end + { translations_attributes: attributes - [*options[:except]] } end end diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 9bf5dc7fd..6f35d44be 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -104,12 +104,14 @@ class ProposalsController < ApplicationController documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], map_location_attributes: [:latitude, :longitude, :zoom]] - params.require(:proposal).permit(attributes, translation_params(Proposal)) + translations_attributes = translation_params(Proposal, except: :retired_explanation) + params.require(:proposal).permit(attributes, translations_attributes) end def retired_params attributes = [:retired_reason] - params.require(:proposal).permit(attributes, translation_params(Proposal)) + translations_attributes = translation_params(Proposal, only: :retired_explanation) + params.require(:proposal).permit(attributes, translations_attributes) end def resource_model