Make Legislation Questions translatable
This commit is contained in:
@@ -36,6 +36,10 @@ App.Globalize =
|
||||
disable_locale: (locale) ->
|
||||
$("#enabled_translations_" + locale).val(0)
|
||||
|
||||
refresh_visible_translations: ->
|
||||
locale = $('.js-globalize-locale-link.is-active').data("locale")
|
||||
App.Globalize.display_translations(locale)
|
||||
|
||||
initialize: ->
|
||||
$('.js-globalize-locale').on 'change', ->
|
||||
App.Globalize.display_translations($(this).val())
|
||||
|
||||
@@ -12,3 +12,5 @@ App.LegislationAdmin =
|
||||
else
|
||||
$(this).val("")
|
||||
|
||||
$("#nested-question-options").on "cocoon:after-insert", ->
|
||||
App.Globalize.refresh_visible_translations()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class Admin::Legislation::QuestionsController < Admin::Legislation::BaseController
|
||||
include Translatable
|
||||
|
||||
load_and_authorize_resource :process, class: "Legislation::Process"
|
||||
load_and_authorize_resource :question, class: "Legislation::Question", through: :process
|
||||
|
||||
@@ -46,7 +48,13 @@ class Admin::Legislation::QuestionsController < Admin::Legislation::BaseControll
|
||||
def question_params
|
||||
params.require(:legislation_question).permit(
|
||||
:title,
|
||||
question_options_attributes: [:id, :value, :_destroy]
|
||||
*translation_params(::Legislation::Question),
|
||||
question_options_attributes: [:id, :value,
|
||||
*translation_params(::Legislation::QuestionOption)]
|
||||
)
|
||||
end
|
||||
|
||||
def resource
|
||||
@question || ::Legislation::Question.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,9 @@ class Legislation::Question < ActiveRecord::Base
|
||||
include ActsAsParanoidAliases
|
||||
include Notifiable
|
||||
|
||||
translates :title, touch: :true
|
||||
globalize_accessors
|
||||
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id'
|
||||
|
||||
@@ -11,7 +14,7 @@ class Legislation::Question < ActiveRecord::Base
|
||||
has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
|
||||
has_many :comments, as: :commentable, dependent: :destroy
|
||||
|
||||
accepts_nested_attributes_for :question_options, reject_if: proc { |attributes| attributes[:value].blank? }, allow_destroy: true
|
||||
accepts_nested_attributes_for :question_options, reject_if: proc { |attributes| attributes.all? { |k, v| v.blank? } }, allow_destroy: true
|
||||
|
||||
validates :process, presence: true
|
||||
validates :title, presence: true
|
||||
|
||||
@@ -2,6 +2,9 @@ class Legislation::QuestionOption < ActiveRecord::Base
|
||||
acts_as_paranoid column: :hidden_at
|
||||
include ActsAsParanoidAliases
|
||||
|
||||
translates :value, touch: :true
|
||||
globalize_accessors
|
||||
|
||||
belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :question_options
|
||||
has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<%= form_for [:admin, @process, @question], url: url, html: {data: {watch_changes: true}} do |f| %>
|
||||
<%= render "admin/shared/globalize_locales", resource: @question %>
|
||||
|
||||
<%= translatable_form_for [:admin, @process, @question], url: url, html: {data: {watch_changes: true}} do |f| %>
|
||||
|
||||
<% if @question.errors.any? %>
|
||||
<div class="small-12 medium-9 column">
|
||||
@@ -16,23 +18,25 @@
|
||||
<% end %>
|
||||
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= f.label :title, t("admin.legislation.questions.form.title") %>
|
||||
<%= f.text_area :title, rows: 5, label: false, placeholder: t("admin.legislation.questions.form.title_placeholder") %>
|
||||
<%= f.translatable_text_area :title,
|
||||
rows: 5,
|
||||
placeholder: t("admin.legislation.questions.form.title_placeholder"),
|
||||
label: t("admin.legislation.questions.form.title") %>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= f.label :question_options, t("admin.legislation.questions.form.question_options") %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div id="nested-question-options">
|
||||
<%= f.fields_for :question_options do |ff| %>
|
||||
<%= render 'question_option_fields', f: ff %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= link_to_add_association t("admin.legislation.questions.form.add_option"),
|
||||
f, :question_options, class: "button hollow" %>
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= link_to_add_association t("admin.legislation.questions.form.add_option"),
|
||||
f, :question_options, class: "button hollow" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-6 large-3 clear column end margin-top">
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<div class="nested-fields">
|
||||
<div class="field">
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= f.text_field :value, label: false, placeholder: t("admin.legislation.questions.form.value_placeholder") %>
|
||||
<%= f.translatable_text_field :value,
|
||||
placeholder: t("admin.legislation.questions.form.value_placeholder"),
|
||||
label: false %>
|
||||
</div>
|
||||
<div class="small-12 medium-3 column">
|
||||
<%= link_to_remove_association t("admin.legislation.questions.question_option_fields.remove_option"), f, class: "delete"%>
|
||||
|
||||
@@ -7,6 +7,11 @@ feature 'Admin legislation questions' do
|
||||
login_as(admin.user)
|
||||
end
|
||||
|
||||
it_behaves_like "translatable",
|
||||
"legislation_question",
|
||||
"edit_admin_legislation_process_question_path",
|
||||
%w[title]
|
||||
|
||||
context "Feature flag" do
|
||||
|
||||
background do
|
||||
@@ -60,7 +65,7 @@ feature 'Admin legislation questions' do
|
||||
|
||||
click_link 'Create question'
|
||||
|
||||
fill_in 'legislation_question_title', with: 'Question 3'
|
||||
fill_in 'legislation_question_title_en', with: 'Question 3'
|
||||
click_button 'Create question'
|
||||
|
||||
expect(page).to have_content 'Question 3'
|
||||
@@ -87,7 +92,7 @@ feature 'Admin legislation questions' do
|
||||
|
||||
click_link 'Question 2'
|
||||
|
||||
fill_in 'legislation_question_title', with: 'Question 2b'
|
||||
fill_in 'legislation_question_title_en', with: 'Question 2b'
|
||||
click_button 'Save changes'
|
||||
|
||||
expect(page).to have_content 'Question 2b'
|
||||
@@ -111,4 +116,60 @@ feature 'Admin legislation questions' do
|
||||
expect(page).not_to have_content 'Question 2'
|
||||
end
|
||||
end
|
||||
|
||||
context "Special translation behaviour" do
|
||||
|
||||
let!(:question) { create(:legislation_question,
|
||||
title_en: "Title in English",
|
||||
title_es: "Título en Español") }
|
||||
|
||||
before do
|
||||
@edit_question_url = edit_admin_legislation_process_question_path(question.process, question)
|
||||
end
|
||||
|
||||
scenario 'Add translation for question option', :js do
|
||||
visit @edit_question_url
|
||||
|
||||
click_on 'Add option'
|
||||
|
||||
find('#nested-question-options input').set('Option 1')
|
||||
|
||||
click_link "Español"
|
||||
|
||||
find('#nested-question-options input').set('Opción 1')
|
||||
|
||||
click_button "Save changes"
|
||||
visit @edit_question_url
|
||||
|
||||
expect(page).to have_field('legislation_question_question_options_attributes_0_value_en', with: 'Option 1')
|
||||
|
||||
click_link "Español"
|
||||
|
||||
expect(page).to have_field('legislation_question_question_options_attributes_0_value_es', with: 'Opción 1')
|
||||
end
|
||||
|
||||
scenario 'Add new question option after changing active locale', :js do
|
||||
visit @edit_question_url
|
||||
|
||||
click_link "Español"
|
||||
|
||||
click_on 'Add option'
|
||||
|
||||
find('#nested-question-options input').set('Opción 1')
|
||||
|
||||
click_link "English"
|
||||
|
||||
find('#nested-question-options input').set('Option 1')
|
||||
|
||||
click_button "Save changes"
|
||||
|
||||
visit @edit_question_url
|
||||
|
||||
expect(page).to have_field('legislation_question_question_options_attributes_0_value_en', with: 'Option 1')
|
||||
|
||||
click_link "Español"
|
||||
|
||||
expect(page).to have_field('legislation_question_question_options_attributes_0_value_es', with: 'Opción 1')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user