Make Legislation Draft Versions translatable

This commit is contained in:
Marko Lovic
2018-08-12 21:43:47 +02:00
committed by Javi Martín
parent 06347062dd
commit 05bfa193cd
6 changed files with 201 additions and 22 deletions

View File

@@ -1,10 +1,15 @@
App.MarkdownEditor =
refresh_preview: (element, md) ->
textarea_content = element.find('textarea').val()
textarea_content = App.MarkdownEditor.find_textarea(element).val()
result = md.render(textarea_content)
element.find('#markdown-preview').html(result)
# Multi-locale (translatable) form fields work by hiding inputs of locales
# which are not "active".
find_textarea: (editor) ->
editor.find('textarea:visible')
initialize: ->
$('.markdown-editor').each ->
md = window.markdownit({
@@ -13,18 +18,18 @@ App.MarkdownEditor =
typographer: true,
})
App.MarkdownEditor.refresh_preview($(this), md)
editor = $(this)
$(this).on 'change input paste keyup', ->
editor.on 'input', ->
App.MarkdownEditor.refresh_preview($(this), md)
$('.legislation-draft-versions-edit .warning').show()
return
$(this).find('textarea').on 'scroll', ->
editor.find('textarea').on 'scroll', ->
$('#markdown-preview').scrollTop($(this).scrollTop())
$(this).find('.fullscreen-toggle').on 'click', ->
$('.markdown-editor').toggleClass('fullscreen')
editor.find('.fullscreen-toggle').on 'click', ->
editor.toggleClass('fullscreen')
$('.fullscreen-container').toggleClass('medium-8', 'medium-12')
span = $(this).find('span')
current_html = span.html()
@@ -33,7 +38,8 @@ App.MarkdownEditor =
else
span.html(span.data('open-text'))
if $('.markdown-editor').hasClass('fullscreen')
$('.markdown-editor textarea').height($(window).height() - 100)
if editor.hasClass('fullscreen')
App.MarkdownEditor.find_textarea(editor).height($(window).height() - 100)
App.MarkdownEditor.refresh_preview(editor, md)
else
$('.markdown-editor textarea').height("10em")
App.MarkdownEditor.find_textarea(editor).height("10em")

View File

@@ -1,6 +1,8 @@
class Admin::Legislation::DraftVersionsController < Admin::Legislation::BaseController
load_and_authorize_resource :process, class: "Legislation::Process"
load_and_authorize_resource :draft_version, class: "Legislation::DraftVersion", through: :process
include Translatable
load_and_authorize_resource :draft_version, class: "Legislation::DraftVersion", through: :process, prepend: true
load_and_authorize_resource :process, class: "Legislation::Process", prepend: true
def index
@draft_versions = @process.draft_versions
@@ -44,7 +46,12 @@ class Admin::Legislation::DraftVersionsController < Admin::Legislation::BaseCont
:status,
:final_version,
:body,
:body_html
:body_html,
*translation_params(Legislation::DraftVersion)
)
end
def resource
@draft_version
end
end

View File

@@ -4,6 +4,13 @@ class Legislation::DraftVersion < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
translates :title, touch: true
translates :changelog, touch: true
translates :body, touch: true
translates :body_html, touch: true
translates :toc_html, touch: true
globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br]
belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id'
has_many :annotations, class_name: 'Legislation::Annotation', foreign_key: 'legislation_draft_version_id', dependent: :destroy
@@ -21,6 +28,13 @@ class Legislation::DraftVersion < ActiveRecord::Base
self.body_html = Redcarpet::Markdown.new(renderer).render(body)
self.toc_html = Redcarpet::Markdown.new(toc_renderer).render(body)
translations.each do |translation|
if translation.body_changed?
translation.body_html = Redcarpet::Markdown.new(renderer).render(translation.body)
translation.toc_html = Redcarpet::Markdown.new(toc_renderer).render(translation.body)
end
end
end
def display_title

View File

@@ -1,4 +1,6 @@
<%= form_for [:admin, @process, @draft_version], url: url, html: {data: {watch_changes: true}} do |f| %>
<%= render "admin/shared/globalize_locales", resource: @draft_version %>
<%= translatable_form_for [:admin, @process, @draft_version], url: url, html: {data: {watch_changes: true}} do |f| %>
<% if @draft_version.errors.any? %>
<div id="error_explanation" data-alert class="callout alert" data-closable>
@@ -14,14 +16,17 @@
<% end %>
<div class="small-12 medium-9 column">
<%= f.label :title %>
<%= f.text_field :title, label: false, placeholder: t("admin.legislation.draft_versions.form.title_placeholder") %>
<%= f.translatable_text_field :title,
placeholder: t("admin.legislation.draft_versions.form.title_placeholder") %>
</div>
<div class="small-12 medium-9 column">
<%= f.label :changelog %>
<span class="help-text"><%= t("admin.legislation.draft_versions.form.use_markdown") %></span>
<%= f.text_area :changelog, label: false, rows: 5, placeholder: t("admin.legislation.draft_versions.form.changelog_placeholder") %>
<%= f.translatable_text_area :changelog,
label: false,
rows: 5,
placeholder: t("admin.legislation.draft_versions.form.changelog_placeholder") %>
</div>
<div class="small-12 medium-9 column">
@@ -65,7 +70,9 @@
<% end %>
</div>
<div class="small-12 medium-6 column markdown-area">
<%= f.text_area :body, label: false, placeholder: t("admin.legislation.draft_versions.form.body_placeholder") %>
<%= f.translatable_text_area :body,
label: false,
placeholder: t("admin.legislation.draft_versions.form.body_placeholder") %>
</div>
<div id="markdown-preview" class="small-12 medium-6 column markdown-preview">

View File

@@ -7,6 +7,11 @@ feature 'Admin legislation draft versions' do
login_as(admin.user)
end
it_behaves_like "translatable",
"legislation_draft_version",
"edit_admin_legislation_process_draft_version_path",
%w[title changelog]
context "Feature flag" do
scenario 'Disabled with a feature flag' do
@@ -53,9 +58,9 @@ feature 'Admin legislation draft versions' do
click_link 'Create version'
fill_in 'legislation_draft_version_title', with: 'Version 3'
fill_in 'legislation_draft_version_changelog', with: 'Version 3 changes'
fill_in 'legislation_draft_version_body', with: 'Version 3 body'
fill_in 'legislation_draft_version_title_en', with: 'Version 3'
fill_in 'legislation_draft_version_changelog_en', with: 'Version 3 changes'
fill_in 'legislation_draft_version_body_en', with: 'Version 3 body'
within('.end') do
click_button 'Create version'
@@ -86,11 +91,11 @@ feature 'Admin legislation draft versions' do
click_link 'Version 1'
fill_in 'legislation_draft_version_title', with: 'Version 1b'
fill_in 'legislation_draft_version_title_en', with: 'Version 1b'
click_link 'Launch text editor'
fill_in 'legislation_draft_version_body', with: '# Version 1 body\r\n\r\nParagraph\r\n\r\n>Quote'
fill_in 'legislation_draft_version_body_en', with: '# Version 1 body\r\n\r\nParagraph\r\n\r\n>Quote'
within('.fullscreen') do
click_link 'Close text editor'
@@ -101,4 +106,135 @@ feature 'Admin legislation draft versions' do
expect(page).to have_content 'Version 1b'
end
end
context "Translations" do
let!(:draft_version) { create(:legislation_draft_version,
title_en: "Title in English",
title_es: "Título en Español",
changelog_en: "Changes in English",
changelog_es: "Cambios en Español",
body_en: "Body in English",
body_es: "Texto en Español") }
before do
@edit_draft_version_url = edit_admin_legislation_process_draft_version_path(draft_version.process, draft_version)
end
scenario "Add a translation", :js do
visit @edit_draft_version_url
select "Français", from: "translation_locale"
fill_in 'legislation_draft_version_title_fr', with: 'Titre en Français'
click_button 'Save changes'
visit @edit_draft_version_url
expect(page).to have_field('legislation_draft_version_title_en', with: 'Title in English')
click_link "Español"
expect(page).to have_field('legislation_draft_version_title_es', with: 'Título en Español')
click_link "Français"
expect(page).to have_field('legislation_draft_version_title_fr', with: 'Titre en Français')
end
scenario "Update a translation", :js do
draft_version.update!(status: 'published')
draft_version.process.update!(title_es: 'Título de proceso')
visit @edit_draft_version_url
click_link "Español"
fill_in 'legislation_draft_version_title_es', with: 'Título correcto en Español'
click_button 'Save changes'
visit legislation_process_draft_version_path(draft_version.process, draft_version)
expect(page).to have_content("Title in English")
select('Español', from: 'locale-switcher')
expect(page).to have_content('Título correcto en Español')
end
scenario "Remove a translation", :js do
visit @edit_draft_version_url
click_link "Español"
click_link "Remove language"
expect(page).not_to have_link "Español"
click_button "Save changes"
visit @edit_draft_version_url
expect(page).not_to have_link "Español"
end
scenario 'Add translation through markup editor', :js do
visit @edit_draft_version_url
select "Français", from: "translation_locale"
click_link 'Launch text editor'
fill_in 'legislation_draft_version_body_fr', with: 'Texte en Français'
click_link 'Close text editor'
click_button "Save changes"
visit @edit_draft_version_url
click_link "Français"
click_link 'Launch text editor'
expect(page).to have_field('legislation_draft_version_body_fr', with: 'Texte en Français')
end
context "Globalize javascript interface" do
scenario "Highlight current locale", :js do
visit @edit_draft_version_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_draft_version_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_draft_version_url
expect(page).to have_field('legislation_draft_version_title_en', with: 'Title in English')
click_link "Español"
expect(page).to have_field('legislation_draft_version_title_es', with: 'Título en Español')
end
scenario "Select a locale and add it to the draft_version form", :js do
visit @edit_draft_version_url
select "Français", from: "translation_locale"
expect(page).to have_link "Français"
click_link "Français"
expect(page).to have_field('legislation_draft_version_title_fr')
end
end
end
end

View File

@@ -16,6 +16,15 @@ RSpec.describe Legislation::DraftVersion, type: :model do
expect(legislation_draft_version.toc_html).to eq(toc_html)
end
it "renders and saves the html from the markdown body field with alternative translation" do
legislation_draft_version.body_fr = body_markdown
legislation_draft_version.save!
expect(legislation_draft_version.body_html_fr).to eq(body_html)
expect(legislation_draft_version.toc_html_fr).to eq(toc_html)
end
def body_markdown
<<-BODY_MARKDOWN
# Title 1