Make answers translatable

This commit is contained in:
Julian Herrero
2018-09-20 17:13:40 +02:00
parent 5e6248d2ac
commit 673ec075eb
13 changed files with 258 additions and 28 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,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

@@ -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

@@ -1,6 +1,11 @@
class Poll::Question::Answer < ActiveRecord::Base
include Galleryable
include Documentable
translates :title, touch: true
translates :description, touch: true
globalize_accessors locales: [:en, :es, :fr, :nl, :pt_br]
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,15 +1,30 @@
<%= 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.label :description, t("admin.shared.description") %>
<% @answer.globalize_locales.each do |locale| %>
<% globalize(locale) do %>
<%= content_tag :span, class: "js-globalize-attribute",
data: { locale: locale },
style: display_translation?(locale) do %>
<%= f.cktext_area "description_#{locale}",
maxlength: Poll::Question.description_max_length,
ckeditor: { language: locale },
class: "js-globalize-attribute",
data: { locale: locale },
label: false %>
<% end %>
<% end %>
<% end %>
</div>
<div class="small-12 medium-4 large-2 margin-top">