diff --git a/app/controllers/admin/widget/cards_controller.rb b/app/controllers/admin/widget/cards_controller.rb
index f51b269c5..f5dce76a1 100644
--- a/app/controllers/admin/widget/cards_controller.rb
+++ b/app/controllers/admin/widget/cards_controller.rb
@@ -1,4 +1,5 @@
class Admin::Widget::CardsController < Admin::BaseController
+ include Translatable
def new
@card = ::Widget::Card.new(header: header_card?)
@@ -44,6 +45,7 @@ class Admin::Widget::CardsController < Admin::BaseController
params.require(:widget_card).permit(
:label, :title, :description, :link_text, :link_url,
:button_text, :button_url, :alignment, :header,
+ *translation_params(Widget::Card),
image_attributes: image_attributes
)
end
@@ -52,4 +54,7 @@ class Admin::Widget::CardsController < Admin::BaseController
params[:header_card].present?
end
+ def resource
+ Widget::Card.find(params[:id])
+ end
end
diff --git a/app/models/widget/card.rb b/app/models/widget/card.rb
index b408cc0a9..73bc3eb00 100644
--- a/app/models/widget/card.rb
+++ b/app/models/widget/card.rb
@@ -1,8 +1,15 @@
class Widget::Card < ActiveRecord::Base
include Imageable
+ # table_name must be set before calls to 'translates'
self.table_name = "widget_cards"
+ translates :label, touch: true
+ translates :title, touch: true
+ translates :description, touch: true
+ translates :link_text, touch: true
+ globalize_accessors
+
def self.header
where(header: true)
end
@@ -10,4 +17,4 @@ class Widget::Card < ActiveRecord::Base
def self.body
where(header: false).order(:created_at)
end
-end
\ No newline at end of file
+end
diff --git a/app/views/admin/widget/cards/_form.html.erb b/app/views/admin/widget/cards/_form.html.erb
index 271f7d206..a823274de 100644
--- a/app/views/admin/widget/cards/_form.html.erb
+++ b/app/views/admin/widget/cards/_form.html.erb
@@ -1,14 +1,18 @@
-<%= form_for [:admin, @card] do |f| %>
+<%= render "admin/shared/globalize_locales", resource: @card %>
+
+<%= translatable_form_for [:admin, @card] do |f| %>
+
- <%= f.text_field :label %>
+ <%= f.translatable_text_field :label %>
- <%= f.text_field :title %>
+ <%= f.translatable_text_field :title %>
- <%= f.text_area :description, rows: 5 %>
+ <%= f.translatable_text_area :description, rows: 5 %>
- <%= f.text_field :link_text %>
+ <%= f.translatable_text_field :link_text %>
+ <%= f.label :link_text %>
diff --git a/db/migrate/20180730120800_add_homepage_content_translations.rb b/db/migrate/20180730120800_add_homepage_content_translations.rb
new file mode 100644
index 000000000..415964536
--- /dev/null
+++ b/db/migrate/20180730120800_add_homepage_content_translations.rb
@@ -0,0 +1,16 @@
+class AddHomepageContentTranslations < ActiveRecord::Migration
+
+ def self.up
+ Widget::Card.create_translation_table!(
+ label: :string,
+ title: :string,
+ description: :text,
+ link_text: :string
+ )
+ end
+
+ def self.down
+ Widget::Card.drop_translation_table!
+ end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index 37bfe9912..2d59c76ae 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -1404,6 +1404,20 @@ ActiveRecord::Schema.define(version: 20180813141443) do
add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope", using: :btree
add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree
+ create_table "widget_card_translations", force: :cascade do |t|
+ t.integer "widget_card_id", null: false
+ t.string "locale", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.string "label"
+ t.string "title"
+ t.text "description"
+ t.string "link_text"
+ end
+
+ add_index "widget_card_translations", ["locale"], name: "index_widget_card_translations_on_locale", using: :btree
+ add_index "widget_card_translations", ["widget_card_id"], name: "index_widget_card_translations_on_widget_card_id", using: :btree
+
create_table "widget_cards", force: :cascade do |t|
t.string "title"
t.text "description"
diff --git a/spec/features/admin/widgets/cards_spec.rb b/spec/features/admin/widgets/cards_spec.rb
index 58899f0a2..22b1e6bff 100644
--- a/spec/features/admin/widgets/cards_spec.rb
+++ b/spec/features/admin/widgets/cards_spec.rb
@@ -7,14 +7,19 @@ feature 'Cards' do
login_as(admin)
end
+ it_behaves_like "translatable",
+ "widget_card",
+ "edit_admin_widget_card_path",
+ %w[title description link_text label]
+
scenario "Create", :js do
visit admin_homepage_path
click_link "Create card"
- fill_in "widget_card_label", with: "Card label"
- fill_in "widget_card_title", with: "Card text"
- fill_in "widget_card_description", with: "Card description"
- fill_in "widget_card_link_text", with: "Link text"
+ fill_in "widget_card_label_en", with: "Card label"
+ fill_in "widget_card_title_en", with: "Card text"
+ fill_in "widget_card_description_en", with: "Card description"
+ fill_in "widget_card_link_text_en", with: "Link text"
fill_in "widget_card_link_url", with: "consul.dev"
attach_image_to_card
click_button "Create card"
@@ -59,10 +64,10 @@ feature 'Cards' do
click_link "Edit"
end
- fill_in "widget_card_label", with: "Card label updated"
- fill_in "widget_card_title", with: "Card text updated"
- fill_in "widget_card_description", with: "Card description updated"
- fill_in "widget_card_link_text", with: "Link text updated"
+ fill_in "widget_card_label_en", with: "Card label updated"
+ fill_in "widget_card_title_en", with: "Card text updated"
+ fill_in "widget_card_description_en", with: "Card description updated"
+ fill_in "widget_card_link_text_en", with: "Link text updated"
fill_in "widget_card_link_url", with: "consul.dev updated"
click_button "Save card"
@@ -99,10 +104,10 @@ feature 'Cards' do
visit admin_homepage_path
click_link "Create header"
- fill_in "widget_card_label", with: "Header label"
- fill_in "widget_card_title", with: "Header text"
- fill_in "widget_card_description", with: "Header description"
- fill_in "widget_card_link_text", with: "Link text"
+ fill_in "widget_card_label_en", with: "Header label"
+ fill_in "widget_card_title_en", with: "Header text"
+ fill_in "widget_card_description_en", with: "Header description"
+ fill_in "widget_card_link_text_en", with: "Link text"
fill_in "widget_card_link_url", with: "consul.dev"
click_button "Create header"
@@ -135,4 +140,4 @@ feature 'Cards' do
make_visible: true)
expect(page).to have_field('widget_card_image_attributes_title', with: "clippy.jpg")
end
-end
\ No newline at end of file
+end
diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb
index a8b86cc75..843df2c09 100644
--- a/spec/shared/features/translatable.rb
+++ b/spec/shared/features/translatable.rb
@@ -192,6 +192,8 @@ def update_button_text
"Update poll"
when "Poll::Question"
"Save"
+ when "Widget::Card"
+ "Save card"
else
"Save changes"
end