From d1249d0b4fd17ac685eb1ab5434578c1804c51aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 10 Oct 2018 16:21:05 +0200 Subject: [PATCH] Update poll questions translatable fields We need to replace ".title=" by ".title_#{locale}=" in one place because for some reason globalize builds a new translation record when using the latter but it doesn't build one when using the former. --- app/controllers/admin/poll/questions_controller.rb | 4 ++-- app/models/poll/question.rb | 10 ++++++---- app/views/admin/poll/questions/_form.html.erb | 4 +++- config/locales/en/activerecord.yml | 2 ++ config/locales/es/activerecord.yml | 2 ++ spec/features/admin/poll/questions_spec.rb | 6 +++--- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 913e6824a..f30a8f096 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -56,8 +56,8 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController private def question_params - attributes = [:poll_id, :title, :question, :proposal_id] - params.require(:poll_question).permit(*attributes, *translation_params(Poll::Question)) + attributes = [:poll_id, :question, :proposal_id] + params.require(:poll_question).permit(*attributes, translation_params(Poll::Question)) end def search_params diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index bc20ce166..e8a3e0dd7 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -7,6 +7,7 @@ class Poll::Question < ActiveRecord::Base translates :title, touch: true globalize_accessors + accepts_nested_attributes_for :translations, allow_destroy: true belongs_to :poll belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' @@ -17,12 +18,13 @@ class Poll::Question < ActiveRecord::Base has_many :partial_results belongs_to :proposal - validates :title, presence: true + translation_class.instance_eval do + validates :title, presence: true, length: { minimum: 4 } + end + validates :author, presence: true validates :poll_id, presence: true - validates :title, length: { minimum: 4 } - scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) } scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)} @@ -47,7 +49,7 @@ class Poll::Question < ActiveRecord::Base self.author = proposal.author self.author_visible_name = proposal.author.name self.proposal_id = proposal.id - self.title = proposal.title + send(:"title_#{Globalize.locale}=", proposal.title) end end diff --git a/app/views/admin/poll/questions/_form.html.erb b/app/views/admin/poll/questions/_form.html.erb index 0f9065262..9637c14f8 100644 --- a/app/views/admin/poll/questions/_form.html.erb +++ b/app/views/admin/poll/questions/_form.html.erb @@ -15,7 +15,9 @@ label: t("admin.questions.new.poll_label") %> - <%= f.translatable_text_field :title %> + <%= f.translatable_fields do |translations_form| %> + <%= translations_form.text_field :title %> + <% end %>
<%= f.submit(class: "button success expanded", value: t("shared.save")) %> diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 9bd206502..28452f71f 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -194,6 +194,8 @@ en: summary: "Summary" description: "Description" external_url: "Link to additional documentation" + poll/question/translation: + title: "Question" signature_sheet: signable_type: "Signable type" signable_id: "Signable ID" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index f0642a610..a82ea1d97 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -194,6 +194,8 @@ es: summary: "Resumen" description: "Descripción" external_url: "Enlace a documentación adicional" + poll/question/translation: + title: "Pregunta" signature_sheet: signable_type: "Tipo de hoja de firmas" signable_id: "ID Propuesta ciudadana/Propuesta inversión" diff --git a/spec/features/admin/poll/questions_spec.rb b/spec/features/admin/poll/questions_spec.rb index 230ba5c28..e5ed6bf96 100644 --- a/spec/features/admin/poll/questions_spec.rb +++ b/spec/features/admin/poll/questions_spec.rb @@ -46,7 +46,7 @@ feature 'Admin poll questions' do click_link "Create question" select 'Movies', from: 'poll_question_poll_id' - fill_in 'poll_question_title_en', with: title + fill_in 'Question', with: title click_button 'Save' @@ -61,7 +61,7 @@ feature 'Admin poll questions' do click_link "Create question" expect(page).to have_current_path(new_admin_question_path, ignore_query: true) - expect(page).to have_field('poll_question_title_en', with: proposal.title) + expect(page).to have_field('Question', with: proposal.title) select 'Proposals', from: 'poll_question_poll_id' @@ -84,7 +84,7 @@ feature 'Admin poll questions' do old_title = question1.title new_title = "Potatoes are great and everyone should have one" - fill_in 'poll_question_title_en', with: new_title + fill_in 'Question', with: new_title click_button 'Save'