Merge pull request #2914 from microweb10/make_polls_translatable

Make polls translatable
This commit is contained in:
Raimond Garcia
2018-09-26 13:19:17 +02:00
committed by GitHub
25 changed files with 434 additions and 50 deletions

View File

@@ -23,7 +23,10 @@ App.Globalize =
element.addClass('is-active');
remove_language: (locale) ->
$(".js-globalize-attribute[data-locale=" + locale + "]").val('').hide()
$(".js-globalize-attribute[data-locale=" + locale + "]").each ->
$(this).val('').hide()
if CKEDITOR.instances[$(this).attr('id')]
CKEDITOR.instances[$(this).attr('id')].setData('')
$(".js-globalize-locale-link[data-locale=" + locale + "]").hide()
next = $(".js-globalize-locale-link:visible").first()
App.Globalize.highlight_locale(next)

View File

@@ -1,4 +1,5 @@
class Admin::Poll::PollsController < Admin::Poll::BaseController
include Translatable
load_and_authorize_resource
before_action :load_search, only: [:search_booths, :search_officers]
@@ -63,7 +64,7 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :summary, :description,
:results_enabled, :stats_enabled, geozone_ids: [],
image_attributes: image_attributes]
params.require(:poll).permit(*attributes)
params.require(:poll).permit(*attributes, *translation_params(Poll))
end
def search_params
@@ -74,4 +75,7 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
@search = search_params[:search]
end
def resource
@poll ||= Poll.find(params[:id])
end
end

View File

@@ -1,4 +1,6 @@
class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
include Translatable
before_action :load_answer, only: [:show, :edit, :update, :documents]
load_and_authorize_resource :question, class: "::Poll::Question"
@@ -49,11 +51,15 @@ 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)
params.require(:poll_question_answer).permit(*attributes, *translation_params(Poll::Question::Answer))
end
def load_answer
@answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id])
end
def resource
load_answer unless @answer
@answer
end
end

View File

@@ -1,5 +1,6 @@
class Admin::Poll::QuestionsController < Admin::Poll::BaseController
include CommentableActions
include Translatable
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: 'Poll::Question'
@@ -55,11 +56,15 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
private
def question_params
params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id)
attributes = [:poll_id, :title, :question, :proposal_id]
params.require(:poll_question).permit(*attributes, *translation_params(Poll::Question))
end
def search_params
params.permit(:poll_id, :search)
end
def resource
@poll_question ||= Poll::Question.find(params[:id])
end
end

View File

@@ -27,7 +27,7 @@ module Translatable
end
def enabled_translations
params.fetch(:enabled_translations)
params.fetch(:enabled_translations, {})
.select { |_, v| v == '1' }
.keys
end

View File

@@ -35,6 +35,10 @@ module TranslatableFormHelper
translatable_field(:text_area, method, options)
end
def translatable_cktext_area(method, options = {})
translatable_field(:cktext_area, method, options)
end
private
def translatable_field(field_type, method, options = {})
@@ -47,7 +51,14 @@ module TranslatableFormHelper
final_options = @template.merge_translatable_field_options(options, locale)
.reverse_merge(label: label_without_locale)
@template.concat send(field_type, localized_attr_name, final_options)
if field_type == :cktext_area
@template.concat content_tag :div, send(field_type, localized_attr_name, final_options),
class: "js-globalize-attribute",
style: @template.display_translation?(locale),
data: { locale: locale }
else
@template.concat send(field_type, localized_attr_name, final_options)
end
end
end
end

View File

@@ -4,6 +4,11 @@ class Poll < ActiveRecord::Base
include ActsAsParanoidAliases
include Notifiable
translates :name, touch: true
translates :summary, touch: true
translates :description, touch: true
globalize_accessors
RECOUNT_DURATION = 1.week
has_many :booth_assignments, class_name: "Poll::BoothAssignment"

View File

@@ -9,7 +9,9 @@ class Poll::Answer < ActiveRecord::Base
validates :author, presence: true
validates :answer, presence: true
validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }},
validates :answer, inclusion: { in: ->(a) { a.question.question_answers
.joins(:translations)
.pluck("poll_question_answer_translations.title") }},
unless: ->(a) { a.question.blank? }
scope :by_author, ->(author_id) { where(author_id: author_id) }

View File

@@ -10,7 +10,9 @@ class Poll::PartialResult < ActiveRecord::Base
validates :question, presence: true
validates :author, presence: true
validates :answer, presence: true
validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }},
validates :answer, inclusion: { in: ->(a) { a.question.question_answers
.joins(:translations)
.pluck("poll_question_answer_translations.title") }},
unless: ->(a) { a.question.blank? }
validates :origin, inclusion: { in: VALID_ORIGINS }

View File

@@ -5,6 +5,9 @@ class Poll::Question < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
translates :title, touch: true
globalize_accessors
belongs_to :poll
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'

View File

@@ -1,6 +1,11 @@
class Poll::Question::Answer < ActiveRecord::Base
include Galleryable
include Documentable
translates :title, touch: true
translates :description, touch: true
globalize_accessors
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
@@ -15,7 +20,7 @@ class Poll::Question::Answer < ActiveRecord::Base
before_validation :set_order, on: :create
def description
super.try :html_safe
self[:description].try :html_safe
end
def self.order_answers(ordered_array)

View File

@@ -1,6 +1,8 @@
<%= form_for [:admin, @poll] do |f| %>
<%= render "admin/shared/globalize_locales", resource: @poll %>
<%= translatable_form_for [:admin, @poll] do |f| %>
<div class="small-12 medium-6 column">
<%= f.text_field :name %>
<%= f.translatable_text_field :name %>
</div>
<div class="clear">
@@ -18,11 +20,11 @@
</div>
<div class="small-12 column">
<%=f.text_area :summary, rows: 4%>
<%=f.translatable_text_area :summary, rows: 4%>
</div>
<div class="small-12 column">
<%=f.text_area :description, rows: 8%>
<%=f.translatable_text_area :description, rows: 8%>
</div>
<div class="small-12 column">

View File

@@ -1,4 +1,6 @@
<%= form_for(@question, url: form_url) do |f| %>
<%= render "admin/shared/globalize_locales", resource: @question %>
<%= translatable_form_for(@question, url: form_url) do |f| %>
<%= render 'shared/errors', resource: @question %>
@@ -6,13 +8,14 @@
<div class="small-12">
<div class="small-12 medium-6 large-4">
<% select_options = Poll.all.map { |p| [p.name, p.id] } %>
<%= f.select :poll_id,
options_for_select(Poll.pluck(:name, :id)),
options_for_select(select_options),
prompt: t("admin.questions.index.select_poll"),
label: t("admin.questions.new.poll_label") %>
</div>
<%= f.text_field :title %>
<%= f.translatable_text_field :title %>
<div class="small-12 medium-4 large-2 margin-top">
<%= f.submit(class: "button success expanded", value: t("shared.save")) %>

View File

@@ -1,15 +1,15 @@
<%= form_for(@answer, url: form_url) do |f| %>
<%= render "admin/shared/globalize_locales", resource: @answer %>
<%= translatable_form_for(@answer, url: form_url) do |f| %>
<%= render 'shared/errors', resource: @answer %>
<%= f.hidden_field :question_id, value: @answer.question_id || @question.id %>
<%= f.text_field :title %>
<%= f.translatable_text_field :title %>
<div class="ckeditor">
<%= f.cktext_area :description,
maxlength: Poll::Question.description_max_length,
ckeditor: { language: I18n.locale } %>
<%= f.translatable_cktext_area :description, maxlength: Poll::Question.description_max_length %>
</div>
<div class="small-12 medium-4 large-2 margin-top">

View File

@@ -28,19 +28,46 @@ section "Creating polls" do
ends_at: 1.month.ago,
results_enabled: true,
stats_enabled: true)
Poll.find_each do |poll|
name = poll.name
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
poll.name = "#{name} (#{locale})"
poll.summary = "Summary for locale #{locale}"
poll.description = "Description for locale #{locale}"
end
end
poll.save!
end
end
section "Creating Poll Questions & Answers" do
Poll.find_each do |poll|
(1..4).to_a.sample.times do
question = Poll::Question.create!(author: User.all.sample,
title: Faker::Lorem.sentence(3).truncate(60) + '?',
poll: poll)
Faker::Lorem.words((2..4).to_a.sample).each do |answer|
title = Faker::Lorem.sentence(3).truncate(60) + '?'
question = Poll::Question.new(author: User.all.sample,
title: title,
poll: poll)
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
question.title = "#{title} (#{locale})"
end
end
question.save!
Faker::Lorem.words((2..4).to_a.sample).each do |title|
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
Poll::Question::Answer.create!(question: question,
title: answer.capitalize,
description: description)
answer = Poll::Question::Answer.new(question: question,
title: title.capitalize,
description: description)
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
answer.title = "#{title} (#{locale})"
answer.description = "#{description} (#{locale})"
end
end
answer.save!
end
end
end
@@ -174,12 +201,26 @@ section "Creating Poll Questions from Proposals" do
proposal = Proposal.all.sample
poll = Poll.current.first
question = Poll::Question.create(poll: poll)
Faker::Lorem.words((2..4).to_a.sample).each do |answer|
Poll::Question::Answer.create!(question: question,
title: answer.capitalize,
description: Faker::ChuckNorris.fact)
Faker::Lorem.words((2..4).to_a.sample).each do |title|
description = "<p>#{Faker::ChuckNorris.fact}</p>"
answer = Poll::Question::Answer.new(question: question,
title: title.capitalize,
description: description)
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
answer.title = "#{title} (#{locale})"
answer.description = "#{description} (#{locale})"
end
end
answer.save!
end
question.copy_attributes_from_proposal(proposal)
title = question.title
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
question.title = "#{title} (#{locale})"
end
end
question.save!
end
end
@@ -189,12 +230,26 @@ section "Creating Successful Proposals" do
proposal = Proposal.all.sample
poll = Poll.current.first
question = Poll::Question.create(poll: poll)
Faker::Lorem.words((2..4).to_a.sample).each do |answer|
Poll::Question::Answer.create!(question: question,
title: answer.capitalize,
description: Faker::ChuckNorris.fact)
Faker::Lorem.words((2..4).to_a.sample).each do |title|
description = "<p>#{Faker::ChuckNorris.fact}</p>"
answer = Poll::Question::Answer.new(question: question,
title: title.capitalize,
description: description)
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
answer.title = "#{title} (#{locale})"
answer.description = "#{description} (#{locale})"
end
end
answer.save!
end
question.copy_attributes_from_proposal(proposal)
title = question.title
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
question.title = "#{title} (#{locale})"
end
end
question.save!
end
end

View File

@@ -0,0 +1,15 @@
class AddPollTranslations < ActiveRecord::Migration
def self.up
Poll.create_translation_table!(
name: :string,
summary: :text,
description: :text
)
end
def self.down
Poll.drop_translation_table!
end
end

View File

@@ -0,0 +1,13 @@
class AddPollQuestionTranslations < ActiveRecord::Migration
def self.up
Poll::Question.create_translation_table!(
title: :string
)
end
def self.down
Poll::Question.drop_translation_table!
end
end

View File

@@ -0,0 +1,14 @@
class AddPollQuestionAnswerTranslations < ActiveRecord::Migration
def self.up
Poll::Question::Answer.create_translation_table!(
title: :string,
description: :text
)
end
def self.down
Poll::Question::Answer.drop_translation_table!
end
end

View File

@@ -880,6 +880,18 @@ ActiveRecord::Schema.define(version: 20180813141443) do
add_index "poll_partial_results", ["origin"], name: "index_poll_partial_results_on_origin", using: :btree
add_index "poll_partial_results", ["question_id"], name: "index_poll_partial_results_on_question_id", using: :btree
create_table "poll_question_answer_translations", force: :cascade do |t|
t.integer "poll_question_answer_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "description"
end
add_index "poll_question_answer_translations", ["locale"], name: "index_poll_question_answer_translations_on_locale", using: :btree
add_index "poll_question_answer_translations", ["poll_question_answer_id"], name: "index_85270fa85f62081a3a227186b4c95fe4f7fa94b9", using: :btree
create_table "poll_question_answer_videos", force: :cascade do |t|
t.string "title"
t.string "url"
@@ -898,6 +910,17 @@ ActiveRecord::Schema.define(version: 20180813141443) do
add_index "poll_question_answers", ["question_id"], name: "index_poll_question_answers_on_question_id", using: :btree
create_table "poll_question_translations", force: :cascade do |t|
t.integer "poll_question_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
end
add_index "poll_question_translations", ["locale"], name: "index_poll_question_translations_on_locale", using: :btree
add_index "poll_question_translations", ["poll_question_id"], name: "index_poll_question_translations_on_poll_question_id", using: :btree
create_table "poll_questions", force: :cascade do |t|
t.integer "proposal_id"
t.integer "poll_id"
@@ -951,6 +974,19 @@ ActiveRecord::Schema.define(version: 20180813141443) do
add_index "poll_shifts", ["booth_id"], name: "index_poll_shifts_on_booth_id", using: :btree
add_index "poll_shifts", ["officer_id"], name: "index_poll_shifts_on_officer_id", using: :btree
create_table "poll_translations", force: :cascade do |t|
t.integer "poll_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.text "summary"
t.text "description"
end
add_index "poll_translations", ["locale"], name: "index_poll_translations_on_locale", using: :btree
add_index "poll_translations", ["poll_id"], name: "index_poll_translations_on_poll_id", using: :btree
create_table "poll_voters", force: :cascade do |t|
t.string "document_number"
t.string "document_type"

View File

@@ -7,6 +7,11 @@ feature 'Admin polls' do
login_as(admin.user)
end
it_behaves_like "translatable",
"poll",
"edit_admin_poll_path",
%w[name summary description]
scenario 'Index empty', :js do
visit admin_root_path
@@ -55,11 +60,11 @@ feature 'Admin polls' do
start_date = 1.week.from_now
end_date = 2.weeks.from_now
fill_in "poll_name", with: "Upcoming poll"
fill_in "poll_name_en", with: "Upcoming poll"
fill_in 'poll_starts_at', with: start_date.strftime("%d/%m/%Y")
fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y")
fill_in 'poll_summary', with: "Upcoming poll's summary. This poll..."
fill_in 'poll_description', with: "Upcomming poll's description. This poll..."
fill_in 'poll_summary_en', with: "Upcoming poll's summary. This poll..."
fill_in 'poll_description_en', with: "Upcomming poll's description. This poll..."
expect(page).not_to have_css("#poll_results_enabled")
expect(page).not_to have_css("#poll_stats_enabled")
@@ -83,7 +88,7 @@ feature 'Admin polls' do
expect(page).to have_css("img[alt='#{poll.image.title}']")
fill_in "poll_name", with: "Next Poll"
fill_in "poll_name_en", with: "Next Poll"
fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y")
click_button "Update poll"

View File

@@ -15,8 +15,8 @@ feature 'Answers' do
visit admin_question_path(question)
click_link 'Add answer'
fill_in 'poll_question_answer_title', with: title
fill_in 'poll_question_answer_description', with: description
fill_in 'poll_question_answer_title_en', with: title
fill_in 'poll_question_answer_description_en', with: description
click_button 'Save'
@@ -33,8 +33,8 @@ feature 'Answers' do
visit admin_question_path(question)
click_link 'Add answer'
fill_in 'poll_question_answer_title', with: title
fill_in 'poll_question_answer_description', with: description
fill_in 'poll_question_answer_title_en', with: title
fill_in 'poll_question_answer_description_en', with: description
click_button 'Save'
@@ -53,7 +53,7 @@ feature 'Answers' do
old_title = answer.title
new_title = 'Ex Machina'
fill_in 'poll_question_answer_title', with: new_title
fill_in 'poll_question_answer_title_en', with: new_title
click_button 'Save'

View File

@@ -6,6 +6,11 @@ feature 'Admin poll questions' do
login_as(create(:administrator).user)
end
it_behaves_like "translatable",
"poll_question",
"edit_admin_question_path",
%w[title]
scenario 'Index' do
question1 = create(:poll_question)
question2 = create(:poll_question)
@@ -41,7 +46,7 @@ feature 'Admin poll questions' do
click_link "Create question"
select 'Movies', from: 'poll_question_poll_id'
fill_in 'poll_question_title', with: title
fill_in 'poll_question_title_en', with: title
click_button 'Save'
@@ -56,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', with: proposal.title)
expect(page).to have_field('poll_question_title_en', with: proposal.title)
select 'Proposals', from: 'poll_question_poll_id'
@@ -79,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', with: new_title
fill_in 'poll_question_title_en', with: new_title
click_button 'Save'
@@ -108,4 +113,41 @@ feature 'Admin poll questions' do
pending "Mark all city by default when creating a poll question from a successful proposal"
context "Poll select box" 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") }
let(:question) { create(:poll_question, poll: poll,
title_en: "Question in English",
title_es: "Pregunta en Español") }
before do
@edit_question_url = edit_admin_question_path(question)
end
scenario "translates the poll name in options", :js do
visit @edit_question_url
expect(page).to have_select('poll_question_poll_id', options: [poll.name_en])
select('Español', from: 'locale-switcher')
expect(page).to have_select('poll_question_poll_id', options: [poll.name_es])
end
scenario "uses fallback if name is not translated to current locale", :js do
visit @edit_question_url
expect(page).to have_select('poll_question_poll_id', options: [poll.name_en])
select('Français', from: 'locale-switcher')
expect(page).to have_select('poll_question_poll_id', options: [poll.name_es])
end
end
end

View File

@@ -28,8 +28,8 @@ feature 'Answers' do
visit admin_question_path(question)
click_link "Add answer"
fill_in "poll_question_answer_title", with: "¿Would you like to reform Central Park?"
fill_in "poll_question_answer_description", with: "Adding more trees, creating a play area..."
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..."
click_button "Save"
expect(page).to have_content "Answer created successfully"

View File

@@ -0,0 +1,149 @@
# 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', from: 'translation_locale')
fill_in_ckeditor 'poll_question_answer_description_pt_br', with: 'resposta em Português'
click_button 'Save'
select('Português', 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

@@ -188,6 +188,10 @@ def update_button_text
"Update milestone"
when "AdminNotification"
"Update notification"
when "Poll"
"Update poll"
when "Poll::Question"
"Save"
else
"Save changes"
end