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.
This commit is contained in:
Javi Martín
2018-10-10 16:21:05 +02:00
parent 759de935ee
commit d1249d0b4f
6 changed files with 18 additions and 10 deletions

View File

@@ -56,8 +56,8 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
private private
def question_params def question_params
attributes = [:poll_id, :title, :question, :proposal_id] attributes = [:poll_id, :question, :proposal_id]
params.require(:poll_question).permit(*attributes, *translation_params(Poll::Question)) params.require(:poll_question).permit(*attributes, translation_params(Poll::Question))
end end
def search_params def search_params

View File

@@ -7,6 +7,7 @@ class Poll::Question < ActiveRecord::Base
translates :title, touch: true translates :title, touch: true
globalize_accessors globalize_accessors
accepts_nested_attributes_for :translations, allow_destroy: true
belongs_to :poll belongs_to :poll
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' 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 has_many :partial_results
belongs_to :proposal 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 :author, presence: true
validates :poll_id, presence: true validates :poll_id, presence: true
validates :title, length: { minimum: 4 }
scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) } scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) }
scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)} 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 = proposal.author
self.author_visible_name = proposal.author.name self.author_visible_name = proposal.author.name
self.proposal_id = proposal.id self.proposal_id = proposal.id
self.title = proposal.title send(:"title_#{Globalize.locale}=", proposal.title)
end end
end end

View File

@@ -15,7 +15,9 @@
label: t("admin.questions.new.poll_label") %> label: t("admin.questions.new.poll_label") %>
</div> </div>
<%= f.translatable_text_field :title %> <%= f.translatable_fields do |translations_form| %>
<%= translations_form.text_field :title %>
<% end %>
<div class="small-12 medium-4 large-2 margin-top"> <div class="small-12 medium-4 large-2 margin-top">
<%= f.submit(class: "button success expanded", value: t("shared.save")) %> <%= f.submit(class: "button success expanded", value: t("shared.save")) %>

View File

@@ -194,6 +194,8 @@ en:
summary: "Summary" summary: "Summary"
description: "Description" description: "Description"
external_url: "Link to additional documentation" external_url: "Link to additional documentation"
poll/question/translation:
title: "Question"
signature_sheet: signature_sheet:
signable_type: "Signable type" signable_type: "Signable type"
signable_id: "Signable ID" signable_id: "Signable ID"

View File

@@ -194,6 +194,8 @@ es:
summary: "Resumen" summary: "Resumen"
description: "Descripción" description: "Descripción"
external_url: "Enlace a documentación adicional" external_url: "Enlace a documentación adicional"
poll/question/translation:
title: "Pregunta"
signature_sheet: signature_sheet:
signable_type: "Tipo de hoja de firmas" signable_type: "Tipo de hoja de firmas"
signable_id: "ID Propuesta ciudadana/Propuesta inversión" signable_id: "ID Propuesta ciudadana/Propuesta inversión"

View File

@@ -46,7 +46,7 @@ feature 'Admin poll questions' do
click_link "Create question" click_link "Create question"
select 'Movies', from: 'poll_question_poll_id' select 'Movies', from: 'poll_question_poll_id'
fill_in 'poll_question_title_en', with: title fill_in 'Question', with: title
click_button 'Save' click_button 'Save'
@@ -61,7 +61,7 @@ feature 'Admin poll questions' do
click_link "Create question" click_link "Create question"
expect(page).to have_current_path(new_admin_question_path, ignore_query: true) 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' select 'Proposals', from: 'poll_question_poll_id'
@@ -84,7 +84,7 @@ feature 'Admin poll questions' do
old_title = question1.title old_title = question1.title
new_title = "Potatoes are great and everyone should have one" 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' click_button 'Save'