From 51cda511553ec050c719ad76bd6dc6d19e619a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Sun, 23 Dec 2018 18:31:36 +0100 Subject: [PATCH] Add debates translation interface Also fix broken spec after removing translatable attributes from strong_parameters definition. Now we need to send these attributes as nested translations attributes. Use activerecord.yml title attribute label so form helper could load it from default location. --- app/controllers/debates_controller.rb | 4 ++- app/views/debates/_form.html.erb | 32 ++++++++++++++------- config/locales/en/activerecord.yml | 2 +- config/locales/es/activerecord.yml | 2 +- spec/controllers/debates_controller_spec.rb | 19 ++++++------ spec/features/debates_spec.rb | 5 ++++ 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index b10e28b88..c0def9803 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -2,6 +2,7 @@ class DebatesController < ApplicationController include FeatureFlags include CommentableActions include FlagActions + include Translatable before_action :parse_tag_filter, only: :index before_action :authenticate_user!, except: [:index, :show, :map] @@ -55,7 +56,8 @@ class DebatesController < ApplicationController private def debate_params - params.require(:debate).permit(:title, :description, :tag_list, :terms_of_service) + attributes = [:tag_list, :terms_of_service] + params.require(:debate).permit(attributes, translation_params(Debate)) end def resource_model diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index 27884b47e..666c73149 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -1,17 +1,29 @@ -<%= form_for(@debate) do |f| %> +<%= render "admin/shared/globalize_locales", resource: @debate %> + +<%= translatable_form_for(@debate) do |f| %> <%= render "shared/errors", resource: @debate %>
-
- <%= f.label :title, t("debates.form.debate_title") %> - <%= f.text_field :title, maxlength: Debate.title_max_length, placeholder: t("debates.form.debate_title"), label: false, data: {js_suggest_result: "js_suggest_result", js_suggest: "#js-suggest", js_url: suggest_debates_path}%> -
-
-
- <%= f.label :description, t("debates.form.debate_text") %> - <%= f.cktext_area :description, maxlength: Debate.description_max_length, ckeditor: { language: I18n.locale }, label: false %> -
+ + <%= f.translatable_fields do |translations_form| %> +
+ <%= translations_form.text_field :title, + maxlength: Debate.title_max_length, + placeholder: t("debates.form.debate_title"), + data: { js_suggest_result: "js_suggest_result", + js_suggest: "#js-suggest", + js_url: suggest_debates_path } %> +
+
+ +
+ <%= translations_form.cktext_area :description, + maxlength: Debate.description_max_length, + ckeditor: { language: I18n.locale }, + label: t("debates.form.debate_text") %> +
+ <% end %> <%= f.invisible_captcha :subtitle %> diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 2faf6b553..3949fd56d 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -181,7 +181,7 @@ en: author: "Author" description: "Opinion" terms_of_service: "Terms of service" - title: "Title" + title: "Debate title" proposal: author: "Author" title: "Title" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index 116893436..37513245e 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -183,7 +183,7 @@ es: author: "Autor" description: "Opinión" terms_of_service: "Términos de servicio" - title: "Título" + title: "Título del debate" proposal: author: "Autor" title: "Título" diff --git a/spec/controllers/debates_controller_spec.rb b/spec/controllers/debates_controller_spec.rb index 60c977689..2e8b111d2 100644 --- a/spec/controllers/debates_controller_spec.rb +++ b/spec/controllers/debates_controller_spec.rb @@ -12,16 +12,19 @@ describe DebatesController do end it "creates an ahoy event" do - + debate_attributes = { + terms_of_service: "1", + translations_attributes: { + "0" => { + title: "A sample debate", + description: "this is a sample debate", + locale: "en" + } + } + } sign_in create(:user) - post :create, params: { - debate: { - title: "A sample debate", - description: "this is a sample debate", - terms_of_service: 1 - } - } + post :create, debate: debate_attributes expect(Ahoy::Event.where(name: :debate_created).count).to eq 1 expect(Ahoy::Event.last.properties["debate_id"]).to eq Debate.last.id end diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 579ede59d..ed09e271c 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -11,6 +11,11 @@ describe "Debates" do context "Concerns" do it_behaves_like "notifiable in-app", Debate it_behaves_like "relationable", Debate + it_behaves_like "translatable", + "debate", + "edit_debate_path", + %w[title], + { "description" => :ckeditor } end scenario "Index" do