diff --git a/app/assets/javascripts/globalize.js.coffee b/app/assets/javascripts/globalize.js.coffee index 027ac498e..068ca88c8 100644 --- a/app/assets/javascripts/globalize.js.coffee +++ b/app/assets/javascripts/globalize.js.coffee @@ -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()) diff --git a/app/assets/javascripts/legislation_admin.js.coffee b/app/assets/javascripts/legislation_admin.js.coffee index fc1d7cab4..f7f9ea17a 100644 --- a/app/assets/javascripts/legislation_admin.js.coffee +++ b/app/assets/javascripts/legislation_admin.js.coffee @@ -12,3 +12,5 @@ App.LegislationAdmin = else $(this).val("") + $("#nested-question-options").on "cocoon:after-insert", -> + App.Globalize.refresh_visible_translations() diff --git a/app/assets/javascripts/markdown_editor.js.coffee b/app/assets/javascripts/markdown_editor.js.coffee index 510bf83ec..29e74e51c 100644 --- a/app/assets/javascripts/markdown_editor.js.coffee +++ b/app/assets/javascripts/markdown_editor.js.coffee @@ -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") diff --git a/app/controllers/admin/legislation/draft_versions_controller.rb b/app/controllers/admin/legislation/draft_versions_controller.rb index 4e7ae18a8..703d8a543 100644 --- a/app/controllers/admin/legislation/draft_versions_controller.rb +++ b/app/controllers/admin/legislation/draft_versions_controller.rb @@ -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 diff --git a/app/controllers/admin/legislation/processes_controller.rb b/app/controllers/admin/legislation/processes_controller.rb index 976318958..2cb20d0ba 100644 --- a/app/controllers/admin/legislation/processes_controller.rb +++ b/app/controllers/admin/legislation/processes_controller.rb @@ -1,4 +1,6 @@ class Admin::Legislation::ProcessesController < Admin::Legislation::BaseController + include Translatable + has_filters %w{open next past all}, only: :index load_and_authorize_resource :process, class: "Legislation::Process" @@ -61,6 +63,7 @@ class Admin::Legislation::ProcessesController < Admin::Legislation::BaseControll :result_publication_enabled, :published, :custom_list, + *translation_params(Legislation::Process), documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] ) end @@ -69,4 +72,8 @@ class Admin::Legislation::ProcessesController < Admin::Legislation::BaseControll @process.set_tag_list_on(:customs, process_params[:custom_list]) @process.save end + + def resource + @process || Legislation::Process.find(params[:id]) + end end diff --git a/app/controllers/admin/legislation/questions_controller.rb b/app/controllers/admin/legislation/questions_controller.rb index bac16295e..da73e905c 100644 --- a/app/controllers/admin/legislation/questions_controller.rb +++ b/app/controllers/admin/legislation/questions_controller.rb @@ -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 diff --git a/app/controllers/concerns/translatable.rb b/app/controllers/concerns/translatable.rb index aecf42fb4..b75d242c0 100644 --- a/app/controllers/concerns/translatable.rb +++ b/app/controllers/concerns/translatable.rb @@ -8,6 +8,8 @@ module Translatable private def translation_params(resource_model) + return [] unless params[:enabled_translations] + resource_model.translated_attribute_names.product(enabled_translations).map do |attr_name, loc| resource_model.localized_attr_name_for(attr_name, loc) end diff --git a/app/models/admin_notification.rb b/app/models/admin_notification.rb index ef632b1f1..4291206bc 100644 --- a/app/models/admin_notification.rb +++ b/app/models/admin_notification.rb @@ -1,8 +1,8 @@ class AdminNotification < ActiveRecord::Base include Notifiable - translates :title, touch: :true - translates :body, touch: :true + translates :title, touch: true + translates :body, touch: true globalize_accessors validates :title, presence: true diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb index 7b9e6a0aa..18e4489b7 100644 --- a/app/models/legislation/draft_version.rb +++ b/app/models/legislation/draft_version.rb @@ -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 + 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 @@ -19,8 +26,17 @@ class Legislation::DraftVersion < ActiveRecord::Base renderer = Redcarpet::Render::HTML.new(with_toc_data: true) toc_renderer = Redcarpet::Render::HTML_TOC.new(with_toc_data: true) - self.body_html = Redcarpet::Markdown.new(renderer).render(body) - self.toc_html = Redcarpet::Markdown.new(toc_renderer).render(body) + if body_changed? + self.body_html = Redcarpet::Markdown.new(renderer).render(body) + self.toc_html = Redcarpet::Markdown.new(toc_renderer).render(body) + end + + 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 diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index ff683aa00..bc3df6be4 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -9,6 +9,12 @@ class Legislation::Process < ActiveRecord::Base acts_as_paranoid column: :hidden_at acts_as_taggable_on :customs + translates :title, touch: true + translates :summary, touch: true + translates :description, touch: true + translates :additional_info, touch: true + globalize_accessors + PHASES_AND_PUBLICATIONS = %i(debate_phase allegations_phase proposals_phase draft_publication result_publication).freeze has_many :draft_versions, -> { order(:id) }, class_name: 'Legislation::DraftVersion', diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index 2ca5fb39a..0c9a5703f 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -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 diff --git a/app/models/legislation/question_option.rb b/app/models/legislation/question_option.rb index f7927dd1a..1ee46ee90 100644 --- a/app/models/legislation/question_option.rb +++ b/app/models/legislation/question_option.rb @@ -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 diff --git a/app/views/admin/legislation/draft_versions/_form.html.erb b/app/views/admin/legislation/draft_versions/_form.html.erb index 45c04f3a6..672f1b119 100644 --- a/app/views/admin/legislation/draft_versions/_form.html.erb +++ b/app/views/admin/legislation/draft_versions/_form.html.erb @@ -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? %>