Update poll question answers translatable fields

We needed to bring back support for CKEditor in our translatable form,
which we had temporarily remove.

And now we support CKEditor in our translatable specs, and so we can
remove the duplicated specs for poll question answers.
This commit is contained in:
Javi Martín
2018-10-10 17:53:13 +02:00
parent d1249d0b4f
commit e0b9c1bfdd
10 changed files with 56 additions and 169 deletions

View File

@@ -11,9 +11,10 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
def create
@answer = ::Poll::Question::Answer.new(answer_params)
@question = @answer.question
if @answer.save
redirect_to admin_question_path(@answer.question),
redirect_to admin_question_path(@question),
notice: t("flash.actions.create.poll_question_answer")
else
render :new
@@ -31,7 +32,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
redirect_to admin_question_path(@answer.question),
notice: t("flash.actions.save_changes.notice")
else
redirect_to :back
render :edit
end
end
@@ -50,8 +51,8 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
def answer_params
documents_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
attributes = [:title, :description, :question_id, documents_attributes: documents_attributes]
params.require(:poll_question_answer).permit(*attributes, *translation_params(Poll::Question::Answer))
attributes = [:question_id, documents_attributes: documents_attributes]
params.require(:poll_question_answer).permit(*attributes, translation_params(Poll::Question::Answer))
end
def load_answer

View File

@@ -53,9 +53,20 @@ module TranslatableFormHelper
define_method field do |attribute, options = {}|
final_options = translations_options(options)
label_help_text_and_field =
custom_label(attribute, final_options[:label], final_options[:label_options]) +
help_text(final_options[:hint]) +
super(attribute, final_options.merge(label: false, hint: false))
if field == :cktext_area
content_tag :div,
label_help_text_and_field,
class: "js-globalize-attribute",
style: @template.display_translation?(locale),
data: { locale: locale }
else
label_help_text_and_field
end
end
end

View File

@@ -5,6 +5,7 @@ class Poll::Question::Answer < ActiveRecord::Base
translates :title, touch: true
translates :description, touch: true
globalize_accessors
accepts_nested_attributes_for :translations, allow_destroy: true
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
@@ -14,7 +15,10 @@ class Poll::Question::Answer < ActiveRecord::Base
belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id'
has_many :videos, class_name: 'Poll::Question::Answer::Video'
translation_class.instance_eval do
validates :title, presence: true
end
validates :given_order, presence: true, uniqueness: { scope: :question_id }
before_validation :set_order, on: :create

View File

@@ -6,11 +6,13 @@
<%= f.hidden_field :question_id, value: @answer.question_id || @question.id %>
<%= f.translatable_text_field :title %>
<%= f.translatable_fields do |translations_form| %>
<%= translations_form.text_field :title %>
<div class="ckeditor">
<%= f.translatable_cktext_area :description, maxlength: Poll::Question.description_max_length %>
<%= translations_form.cktext_area :description, maxlength: Poll::Question.description_max_length %>
</div>
<% end %>
<div class="small-12 medium-4 large-2 margin-top">
<%= f.submit(class: "button success expanded", value: t("shared.save")) %>

View File

@@ -262,6 +262,9 @@ en:
poll/question/answer:
title: Answer
description: Description
poll/question/answer/translation:
title: Answer
description: Description
poll/question/answer/video:
title: Title
url: External video

View File

@@ -262,6 +262,9 @@ es:
poll/question/answer:
title: Respuesta
description: Descripción
poll/question/answer/translation:
title: Respuesta
description: Descripción
poll/question/answer/video:
title: Título
url: Vídeo externo

View File

@@ -7,6 +7,12 @@ feature 'Answers' do
login_as admin.user
end
it_behaves_like "translatable",
"poll_question_answer",
"edit_admin_answer_path",
%w[title],
{ "description" => :ckeditor }
scenario 'Create' do
question = create(:poll_question)
title = 'Whatever the question may be, the answer is always 42'
@@ -15,8 +21,8 @@ feature 'Answers' do
visit admin_question_path(question)
click_link 'Add answer'
fill_in 'poll_question_answer_title_en', with: title
fill_in 'poll_question_answer_description_en', with: description
fill_in 'Answer', with: title
fill_in 'Description', with: description
click_button 'Save'
@@ -33,8 +39,8 @@ feature 'Answers' do
visit admin_question_path(question)
click_link 'Add answer'
fill_in 'poll_question_answer_title_en', with: title
fill_in 'poll_question_answer_description_en', with: description
fill_in 'Answer', with: title
fill_in 'Description', with: description
click_button 'Save'
@@ -53,7 +59,7 @@ feature 'Answers' do
old_title = answer.title
new_title = 'Ex Machina'
fill_in 'poll_question_answer_title_en', with: new_title
fill_in 'Answer', with: new_title
click_button 'Save'

View File

@@ -28,8 +28,8 @@ feature 'Answers' do
visit admin_question_path(question)
click_link "Add answer"
fill_in "poll_question_answer_title_en", with: "¿Would you like to reform Central Park?"
fill_in "poll_question_answer_description_en", with: "Adding more trees, creating a play area..."
fill_in "Answer", with: "¿Would you like to reform Central Park?"
fill_in "Description", with: "Adding more trees, creating a play area..."
click_button "Save"
expect(page).to have_content "Answer created successfully"

View File

@@ -1,149 +0,0 @@
# coding: utf-8
require 'rails_helper'
feature "Translations" do
context "Polls" do
let(:poll) { create(:poll, name_en: "Name in English",
name_es: "Nombre en Español",
summary_en: "Summary in English",
summary_es: "Resumen en Español",
description_en: "Description in English",
description_es: "Descripción en Español") }
background do
admin = create(:administrator)
login_as(admin.user)
end
context "Questions" do
let(:question) { create(:poll_question, poll: poll,
title_en: "Question in English",
title_es: "Pregunta en Español") }
context "Answers" do
let(:answer) { create(:poll_question_answer, question: question,
title_en: "Answer in English",
title_es: "Respuesta en Español",
description_en: "Description in English",
description_es: "Descripción en Español") }
before do
@edit_answer_url = edit_admin_answer_path(answer)
end
scenario "Add a translation", :js do
visit @edit_answer_url
select "Français", from: "translation_locale"
fill_in 'poll_question_answer_title_fr', with: 'Answer en Français'
fill_in_ckeditor 'poll_question_answer_description_fr', with: 'Description en Français'
click_button 'Save'
expect(page).to have_content "Changes saved"
expect(page).to have_content "Answer in English"
expect(page).to have_content "Description in English"
select('Español', from: 'locale-switcher')
expect(page).to have_content "Respuesta en Español"
expect(page).to have_content "Descripción en Español"
select('Français', from: 'locale-switcher')
expect(page).to have_content "Answer en Français"
expect(page).to have_content "Description en Français"
end
scenario "Update a translation with allowed blank translated field", :js do
visit @edit_answer_url
click_link "Español"
fill_in 'poll_question_answer_title_es', with: 'Pregunta correcta en Español'
fill_in_ckeditor 'poll_question_answer_description_es', with: ''
click_button 'Save'
expect(page).to have_content "Changes saved"
expect(page).to have_content("Answer in English")
expect(page).to have_content("Description in English")
select('Español', from: 'locale-switcher')
expect(page).to have_content("Pregunta correcta en Español")
expect(page).to_not have_content("Descripción en Español")
end
scenario "Remove a translation", :js do
visit @edit_answer_url
click_link "Español"
click_link "Remove language"
expect(page).not_to have_link "Español"
click_button "Save"
visit @edit_answer_url
expect(page).not_to have_link "Español"
end
scenario "Add a translation for a locale with non-underscored name", :js do
visit @edit_answer_url
select('Português brasileiro', from: 'translation_locale')
fill_in_ckeditor 'poll_question_answer_description_pt_br', with: 'resposta em Português'
click_button 'Save'
select('Português brasileiro', from: 'locale-switcher')
expect(page).to have_content("resposta em Português")
end
context "Globalize javascript interface" do
scenario "Highlight current locale", :js do
visit @edit_answer_url
expect(find("a.js-globalize-locale-link.is-active")).to have_content "English"
select('Español', from: 'locale-switcher')
expect(find("a.js-globalize-locale-link.is-active")).to have_content "Español"
end
scenario "Highlight selected locale", :js do
visit @edit_answer_url
expect(find("a.js-globalize-locale-link.is-active")).to have_content "English"
click_link "Español"
expect(find("a.js-globalize-locale-link.is-active")).to have_content "Español"
end
scenario "Show selected locale form", :js do
visit @edit_answer_url
expect(page).to have_field('poll_question_answer_title_en', with: 'Answer in English')
click_link "Español"
expect(page).to have_field('poll_question_answer_title_es', with: 'Respuesta en Español')
end
scenario "Select a locale and add it to the poll form", :js do
visit @edit_answer_url
select "Français", from: "translation_locale"
expect(page).to have_link "Français"
click_link "Français"
expect(page).to have_field('poll_question_answer_title_fr')
end
end
end
end
end
end

View File

@@ -240,11 +240,11 @@ def text_for(field, locale)
end
end
def field_for(field, locale)
def field_for(field, locale, visible: true)
if translatable_class.name == "I18nContent"
"contents_content_#{translatable.key}values_#{field}_#{locale}"
else
find("[data-locale='#{locale}'][id$='#{field}']")[:id]
find("[data-locale='#{locale}'][id$='#{field}']", visible: visible)[:id]
end
end
@@ -261,6 +261,8 @@ def fill_in_textarea(field, textarea_type, locale, with:)
click_link class: "fullscreen-toggle"
fill_in field_for(field, locale), with: with
click_link class: "fullscreen-toggle"
elsif textarea_type == :ckeditor
fill_in_ckeditor field_for(field, locale, visible: false), with: with
end
end
@@ -274,6 +276,10 @@ def expect_page_to_have_translatable_field(field, locale, with:)
click_link class: "fullscreen-toggle"
expect(page).to have_field field_for(field, locale), with: with
click_link class: "fullscreen-toggle"
elsif textarea_type == :ckeditor
within(".ckeditor div.js-globalize-attribute[data-locale='#{locale}']") do
within_frame(0) { expect(page).to have_content with }
end
end
end
end
@@ -288,7 +294,7 @@ def update_button_text
"Update notification"
when "Poll"
"Update poll"
when "Poll::Question"
when "Poll::Question", "Poll::Question::Answer"
"Save"
when "Widget::Card"
"Save card"