Updates translatable custom pages

This commit is contained in:
Papaya Labs
2018-09-27 13:46:17 +02:00
parent 2c22ab347d
commit 914bfa645e
10 changed files with 210 additions and 91 deletions

View File

@@ -1,4 +1,5 @@
class Admin::SiteCustomization::PagesController < Admin::SiteCustomization::BaseController class Admin::SiteCustomization::PagesController < Admin::SiteCustomization::BaseController
include Translatable
load_and_authorize_resource :page, class: "SiteCustomization::Page" load_and_authorize_resource :page, class: "SiteCustomization::Page"
def index def index
@@ -34,15 +35,21 @@ class Admin::SiteCustomization::PagesController < Admin::SiteCustomization::Base
private private
def page_params def page_params
params.require(:site_customization_page).permit( attributes = [:slug,
:slug,
:title, :title,
:subtitle, :subtitle,
:content, :content,
:more_info_flag, :more_info_flag,
:print_content_flag, :print_content_flag,
:status, :status,
:locale :locale]
params.require(:site_customization_page).permit(*attributes,
*translation_params(SiteCustomization::Page)
) )
end end
def resource
SiteCustomization::Page.find(params[:id])
end
end end

View File

@@ -6,11 +6,15 @@ class SiteCustomization::Page < ActiveRecord::Base
format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format } format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format }
validates :title, presence: true validates :title, presence: true
validates :status, presence: true, inclusion: { in: VALID_STATUSES } validates :status, presence: true, inclusion: { in: VALID_STATUSES }
validates :locale, presence: true
translates :title, touch: true
translates :subtitle, touch: true
translates :content, touch: true
globalize_accessors
scope :published, -> { where(status: 'published').order('id DESC') } scope :published, -> { where(status: 'published').order('id DESC') }
scope :with_more_info_flag, -> { where(status: 'published', more_info_flag: true).order('id ASC') } scope :with_more_info_flag, -> { where(status: 'published', more_info_flag: true).order('id ASC') }
scope :with_same_locale, -> { where(locale: I18n.locale).order('id ASC') } scope :with_same_locale, -> { joins(:translations).where("site_customization_page_translations.locale": I18n.locale) }
def url def url
"/#{slug}" "/#{slug}"

View File

@@ -1,5 +1,6 @@
<%= form_for [:admin, @page], html: {class: "edit_page", data: {watch_changes: true}} do |f| %> <%= render "admin/shared/globalize_locales", resource: @page %>
<%= translatable_form_for [:admin, @page], html: {class: "edit_page", data: {watch_changes: true}} do |f| %>
<% if @page.errors.any? %> <% if @page.errors.any? %>
<div id="error_explanation" data-alert class="callout alert" data-closable> <div id="error_explanation" data-alert class="callout alert" data-closable>
<button class="close-button" aria-label="<%= t("application.close") %>" type="button" data-close> <button class="close-button" aria-label="<%= t("application.close") %>" type="button" data-close>
@@ -30,33 +31,20 @@
<br> <br>
<% end %> <% end %>
</div> </div>
<div class="small-12 medium-4 column">
<%= f.label :locale %>
<%= f.select :locale, I18n.available_locales.map { |l| [name_for_locale(l), l.to_s] }, { include_blank: true, label: false } %>
</div>
<div class="small-12 medium-4 column"> <div class="small-12 medium-4 column">
<%= f.label :slug %> <%= f.label :slug %>
<%= f.text_field :slug, label: false, size: 80, maxlength: 80 %> <%= f.text_field :slug, label: false, size: 80, maxlength: 80 %>
</div> </div>
<div class="small-12 column">
<div class="small-12 column"> <hr>
<hr> <%= f.translatable_text_field :title %>
<%= f.translatable_text_field :subtitle %>
<%= f.label :title %> <div class="ckeditor">
<%= f.text_field :title, label: false %> <%= f.translatable_cktext_area :content %>
</div>
<%= f.label :subtitle %> <div class="small-12 medium-6 large-3 margin-top">
<%= f.text_field :subtitle, label: false, size: 80, maxlength: 80 %> <%= f.submit class: "button success expanded" %>
<div class="ckeditor margin-top">
<%= f.label :content %>
<%= f.cktext_area :content, label: false, cols: 80, rows: 10,
ckeditor: { language: I18n.locale, toolbar: "admin" } %>
</div>
<div class="small-12 medium-6 large-3">
<%= f.submit class: "button success expanded" %>
</div>
</div> </div>
</div> </div>
<% end %> <% end %>

View File

@@ -12,10 +12,10 @@
<thead> <thead>
<tr> <tr>
<th><%= t("admin.site_customization.pages.page.title") %></th> <th><%= t("admin.site_customization.pages.page.title") %></th>
<th><%= t("admin.site_customization.pages.page.slug") %></th>
<th><%= t("admin.site_customization.pages.page.created_at") %></th> <th><%= t("admin.site_customization.pages.page.created_at") %></th>
<th><%= t("admin.site_customization.pages.page.updated_at") %></th> <th><%= t("admin.site_customization.pages.page.updated_at") %></th>
<th><%= t("admin.site_customization.pages.page.status") %></th> <th><%= t("admin.site_customization.pages.page.status") %></th>
<th><%= t("admin.site_customization.pages.page.locale") %></th>
<th class="small-4"><%= t("admin.actions.actions") %></th> <th class="small-4"><%= t("admin.actions.actions") %></th>
</tr> </tr>
</thead> </thead>
@@ -25,10 +25,10 @@
<td> <td>
<%= link_to page.title, edit_admin_site_customization_page_path(page) %> <%= link_to page.title, edit_admin_site_customization_page_path(page) %>
</td> </td>
<td><%= page.slug %></td>
<td><%= I18n.l page.created_at, format: :short %></td> <td><%= I18n.l page.created_at, format: :short %></td>
<td><%= I18n.l page.created_at, format: :short %></td> <td><%= I18n.l page.created_at, format: :short %></td>
<td><%= t("admin.site_customization.pages.page.status_#{page.status}") %></td> <td><%= t("admin.site_customization.pages.page.status_#{page.status}") %></td>
<td><%= name_for_locale(page.locale) %></td>
<td> <td>
<div class="small-6 column"> <div class="small-6 column">
<% if page.status == "published" %> <% if page.status == "published" %>
@@ -56,3 +56,4 @@
<%= page_entries_info @pages %> <%= page_entries_info @pages %>
</div> </div>
<% end %> <% end %>

View File

@@ -1349,11 +1349,11 @@ en:
page: page:
created_at: Created at created_at: Created at
status: Status status: Status
title: Title
updated_at: Updated at updated_at: Updated at
status_draft: Draft status_draft: Draft
status_published: Published status_published: Published
locale: Language title: Title
slug: Slug
homepage: homepage:
title: Homepage title: Homepage
description: The active modules appear in the homepage in the same order as here. description: The active modules appear in the homepage in the same order as here.

View File

@@ -0,0 +1,15 @@
class AddTranslatePages < ActiveRecord::Migration
def self.up
SiteCustomization::Page.create_translation_table!({
title: :string,
subtitle: :string,
content: :text
})
change_column :site_customization_pages, :title, :string, :null => true
end
def self.down
SiteCustomization::Page.drop_translation_table!
change_column :site_customization_pages, :title, :string, :null => false
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180813141443) do ActiveRecord::Schema.define(version: 20180924071722) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -1161,9 +1161,22 @@ ActiveRecord::Schema.define(version: 20180813141443) do
add_index "site_customization_images", ["name"], name: "index_site_customization_images_on_name", unique: true, using: :btree add_index "site_customization_images", ["name"], name: "index_site_customization_images_on_name", unique: true, using: :btree
create_table "site_customization_page_translations", force: :cascade do |t|
t.integer "site_customization_page_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.string "subtitle"
t.text "content"
end
add_index "site_customization_page_translations", ["locale"], name: "index_site_customization_page_translations_on_locale", using: :btree
add_index "site_customization_page_translations", ["site_customization_page_id"], name: "index_7fa0f9505738cb31a31f11fb2f4c4531fed7178b", using: :btree
create_table "site_customization_pages", force: :cascade do |t| create_table "site_customization_pages", force: :cascade do |t|
t.string "slug", null: false t.string "slug", null: false
t.string "title", null: false t.string "title"
t.string "subtitle" t.string "subtitle"
t.text "content" t.text "content"
t.boolean "more_info_flag" t.boolean "more_info_flag"

View File

@@ -41,7 +41,6 @@ FactoryBot.define do
more_info_flag false more_info_flag false
print_content_flag false print_content_flag false
status 'draft' status 'draft'
locale 'en'
trait :published do trait :published do
status "published" status "published"

View File

@@ -11,7 +11,7 @@ feature "Admin custom pages" do
custom_page = create(:site_customization_page) custom_page = create(:site_customization_page)
visit admin_site_customization_pages_path visit admin_site_customization_pages_path
expect(page).to have_content(custom_page.title) expect(page).to have_content(custom_page.slug)
end end
context "Create" do context "Create" do
@@ -22,19 +22,18 @@ feature "Admin custom pages" do
click_link "Custom pages" click_link "Custom pages"
end end
expect(page).not_to have_content "An example custom page" expect(page).not_to have_content "example-page"
click_link "Create new page" click_link "Create new page"
fill_in "site_customization_page_title", with: "An example custom page" fill_in "site_customization_page_title_en", with: "An example custom page"
fill_in "site_customization_page_subtitle", with: "Page subtitle" fill_in "site_customization_page_subtitle_en", with: "Page subtitle"
fill_in "site_customization_page_slug", with: "example-page" fill_in "site_customization_page_slug", with: "example-page"
fill_in "site_customization_page_content", with: "This page is about..." fill_in "site_customization_page_content_en", with: "This page is about..."
select 'English', from:"site_customization_page_locale"
click_button "Create Custom page" click_button "Create Custom page"
expect(page).to have_content "An example custom page" expect(page).to have_content "example-page"
end end
end end
@@ -51,7 +50,7 @@ feature "Admin custom pages" do
expect(page).to have_selector("h2", text: "An example custom page") expect(page).to have_selector("h2", text: "An example custom page")
fill_in "site_customization_page_title", with: "Another example custom page" fill_in "site_customization_page_title_en", with: "Another example custom page"
click_button "Update Custom page" click_button "Update Custom page"
expect(page).to have_content "Page updated successfully" expect(page).to have_content "Page updated successfully"

View File

@@ -5,10 +5,9 @@ feature "Custom Pages" do
scenario "See default content when custom page is not published" do scenario "See default content when custom page is not published" do
custom_page = create(:site_customization_page, custom_page = create(:site_customization_page,
slug: "conditions", slug: "conditions",
title: "Custom conditions", title_en: "Custom conditions",
content: "New text for conditions page", content_en: "New text for conditions page",
print_content_flag: true, print_content_flag: true
locale: "en"
) )
visit custom_page.url visit custom_page.url
@@ -22,10 +21,9 @@ feature "Custom Pages" do
scenario "See custom content when custom page is published" do scenario "See custom content when custom page is published" do
custom_page = create(:site_customization_page, :published, custom_page = create(:site_customization_page, :published,
slug: "conditions", slug: "conditions",
title: "Custom conditions", title_en: "Custom conditions",
content: "New text for conditions page", content_en: "New text for conditions page",
print_content_flag: true, print_content_flag: true
locale: "en"
) )
visit custom_page.url visit custom_page.url
@@ -42,10 +40,9 @@ feature "Custom Pages" do
scenario "See page" do scenario "See page" do
custom_page = create(:site_customization_page, custom_page = create(:site_customization_page,
slug: "other-slug", slug: "other-slug",
title: "Custom page", title_en: "Custom page",
content: "Text for new custom page", content_en: "Text for new custom page",
print_content_flag: false, print_content_flag: false
locale: "en"
) )
visit custom_page.url visit custom_page.url
@@ -58,10 +55,9 @@ feature "Custom Pages" do
scenario "See page" do scenario "See page" do
custom_page = create(:site_customization_page, :published, custom_page = create(:site_customization_page, :published,
slug: "other-slug", slug: "other-slug",
title: "Custom page", title_en: "Custom page",
content: "Text for new custom page", content_en: "Text for new custom page",
print_content_flag: false, print_content_flag: false
locale: "en"
) )
visit custom_page.url visit custom_page.url
@@ -75,11 +71,10 @@ feature "Custom Pages" do
scenario "Show all fields and text with links" do scenario "Show all fields and text with links" do
custom_page = create(:site_customization_page, :published, custom_page = create(:site_customization_page, :published,
slug: "slug-with-all-fields-filled", slug: "slug-with-all-fields-filled",
title: "Custom page", title_en: "Custom page",
subtitle: "This is my new custom page", subtitle_en: "This is my new custom page",
content: "Text for new custom page with a link to https://consul.dev", content_en: "Text for new custom page with a link to https://consul.dev",
print_content_flag: true, print_content_flag: true
locale: "en"
) )
visit custom_page.url visit custom_page.url
@@ -95,11 +90,10 @@ feature "Custom Pages" do
scenario "Don't show subtitle if its blank" do scenario "Don't show subtitle if its blank" do
custom_page = create(:site_customization_page, :published, custom_page = create(:site_customization_page, :published,
slug: "slug-without-subtitle", slug: "slug-without-subtitle",
title: "Custom page", title_en: "Custom page",
subtitle: "", subtitle_en: "",
content: "Text for new custom page", content_en: "Text for new custom page",
print_content_flag: false, print_content_flag: false
locale: "en"
) )
visit custom_page.url visit custom_page.url
@@ -113,10 +107,10 @@ feature "Custom Pages" do
scenario "Listed in more information page" do scenario "Listed in more information page" do
custom_page = create(:site_customization_page, :published, custom_page = create(:site_customization_page, :published,
slug: "another-slug", title: "Another custom page", slug: "another-slug",
subtitle: "Subtitle for custom page", title_en: "Another custom page",
more_info_flag: true, subtitle_en: "Subtitle for custom page",
locale: "en" more_info_flag: true
) )
visit help_path visit help_path
@@ -126,10 +120,9 @@ feature "Custom Pages" do
scenario "Not listed in more information page" do scenario "Not listed in more information page" do
custom_page = create(:site_customization_page, :published, custom_page = create(:site_customization_page, :published,
slug: "another-slug", title: "Another custom page", slug: "another-slug", title_en: "Another custom page",
subtitle: "Subtitle for custom page", subtitle_en: "Subtitle for custom page",
more_info_flag: false, more_info_flag: false
locale: "en"
) )
visit help_path visit help_path
@@ -142,24 +135,124 @@ feature "Custom Pages" do
expect(page).to have_selector("h1", text: "Another custom page") expect(page).to have_selector("h1", text: "Another custom page")
expect(page).to have_content("Subtitle for custom page") expect(page).to have_content("Subtitle for custom page")
end end
end
end
scenario "Not listed in more information page due to different locale" do context "Translation" do
custom_page = create(:site_customization_page, :published,
slug: "another-slug", title: "Ce texte est en français",
subtitle: "Subtitle for custom page",
more_info_flag: false,
locale: "fr"
)
visit help_path let(:custom_page) { create(:site_customization_page, :published,
slug: "example-page",
title_en: "Title in English",
title_es: "Titulo en Español",
subtitle_en: "Subtitle in English",
subtitle_es: "Subtitulo en Español",
content_en: "Content in English",
content_es: "Contenido en Español"
) }
expect(page).not_to have_content("Ce texte est en français") background do
admin = create(:administrator)
login_as(admin.user)
end
visit custom_page.url before do
@edit_page_url = edit_admin_site_customization_page_path(custom_page)
end
expect(page).to have_title("Ce texte est en français") scenario "Add a translation in Português", :js do
expect(page).to have_selector("h1", text: "Ce texte est en français") visit @edit_page_url
expect(page).to have_content("Subtitle for custom page")
select "Português", from: "translation_locale"
fill_in 'site_customization_page_title_pt_br', with: 'Titulo em Português'
click_button 'Update Custom page'
expect(page).to have_content "Page updated successfully"
visit @edit_page_url
expect(page).to have_field('site_customization_page_title_en', with: 'Title in English')
click_link "Español"
expect(page).to have_field('site_customization_page_title_es', with: 'Titulo en Español')
click_link "Português"
expect(page).to have_field('site_customization_page_title_pt_br', with: 'Titulo em Português')
end
scenario "Update a translation", :js do
visit @edit_page_url
click_link "Español"
fill_in 'site_customization_page_title_es', with: 'Titulo correcta en Español'
click_button 'Update Custom page'
expect(page).to have_content "Page updated successfully"
visit custom_page.url
select('English', from: 'locale-switcher')
expect(page).to have_content("Title in English")
select('Español', from: 'locale-switcher')
expect(page).to have_content("Titulo correcta en Español")
end
scenario "Remove a translation", :js do
visit @edit_page_url
click_link "Español"
click_link "Remove language"
expect(page).not_to have_link "Español"
click_button "Update Custom page"
visit @edit_page_url
expect(page).not_to have_link "Español"
end
context "Globalize javascript interface" do
scenario "Highlight current locale", :js do
visit @edit_page_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_page_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_page_url
expect(page).to have_field('site_customization_page_title_en', with: 'Title in English')
click_link "Español"
expect(page).to have_field('site_customization_page_title_es', with: 'Titulo en Español')
end
scenario "Select a locale and add it to the milestone form", :js do
visit @edit_page_url
select "Français", from: "translation_locale"
expect(page).to have_link "Français"
click_link "Français"
expect(page).to have_field('site_customization_page_title_fr')
end end
end end
end end