diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 225665b94..099fc818b 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -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 diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb index 868cb7028..8d14a1d74 100644 --- a/app/helpers/translatable_form_helper.rb +++ b/app/helpers/translatable_form_helper.rb @@ -53,9 +53,20 @@ module TranslatableFormHelper define_method field do |attribute, options = {}| final_options = translations_options(options) - custom_label(attribute, final_options[:label], final_options[:label_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 diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 56655e457..5fd0e19c7 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -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' - validates :title, presence: true + 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 diff --git a/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb index 1ca05d9ab..9759f6935 100644 --- a/app/views/admin/poll/questions/answers/_form.html.erb +++ b/app/views/admin/poll/questions/answers/_form.html.erb @@ -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 %> -
- <%= f.translatable_cktext_area :description, maxlength: Poll::Question.description_max_length %> -
+
+ <%= translations_form.cktext_area :description, maxlength: Poll::Question.description_max_length %> +
+ <% 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 28452f71f..5bb1e9b55 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -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 diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index a82ea1d97..f37680c51 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -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 diff --git a/spec/features/admin/poll/questions/answers/answers_spec.rb b/spec/features/admin/poll/questions/answers/answers_spec.rb index dbcf8cf5c..4e3965f01 100644 --- a/spec/features/admin/poll/questions/answers/answers_spec.rb +++ b/spec/features/admin/poll/questions/answers/answers_spec.rb @@ -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' diff --git a/spec/features/polls/answers_spec.rb b/spec/features/polls/answers_spec.rb index 6daffc0eb..89bdfeb5d 100644 --- a/spec/features/polls/answers_spec.rb +++ b/spec/features/polls/answers_spec.rb @@ -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" diff --git a/spec/features/translations/poll_question_answers_spec.rb b/spec/features/translations/poll_question_answers_spec.rb deleted file mode 100644 index fded75cfd..000000000 --- a/spec/features/translations/poll_question_answers_spec.rb +++ /dev/null @@ -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 diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 862417ff3..c34c8ec9c 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -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"