Make Widget::Card translatable

This commit is contained in:
Javi Martín
2018-09-26 15:55:36 +02:00
parent 2c22ab347d
commit 5bb5cfa7fb
7 changed files with 72 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
class Admin::Widget::CardsController < Admin::BaseController class Admin::Widget::CardsController < Admin::BaseController
include Translatable
def new def new
@card = ::Widget::Card.new(header: header_card?) @card = ::Widget::Card.new(header: header_card?)
@@ -44,6 +45,7 @@ class Admin::Widget::CardsController < Admin::BaseController
params.require(:widget_card).permit( params.require(:widget_card).permit(
:label, :title, :description, :link_text, :link_url, :label, :title, :description, :link_text, :link_url,
:button_text, :button_url, :alignment, :header, :button_text, :button_url, :alignment, :header,
*translation_params(Widget::Card),
image_attributes: image_attributes image_attributes: image_attributes
) )
end end
@@ -52,4 +54,7 @@ class Admin::Widget::CardsController < Admin::BaseController
params[:header_card].present? params[:header_card].present?
end end
def resource
Widget::Card.find(params[:id])
end
end end

View File

@@ -1,8 +1,15 @@
class Widget::Card < ActiveRecord::Base class Widget::Card < ActiveRecord::Base
include Imageable include Imageable
# table_name must be set before calls to 'translates'
self.table_name = "widget_cards" 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 def self.header
where(header: true) where(header: true)
end end

View File

@@ -1,14 +1,18 @@
<%= form_for [:admin, @card] do |f| %> <%= render "admin/shared/globalize_locales", resource: @card %>
<%= translatable_form_for [:admin, @card] do |f| %>
<div class="small-12 medium-6"> <div class="small-12 medium-6">
<%= f.text_field :label %> <%= f.translatable_text_field :label %>
</div> </div>
<%= f.text_field :title %> <%= f.translatable_text_field :title %>
<%= f.text_area :description, rows: 5 %> <%= f.translatable_text_area :description, rows: 5 %>
<div class="small-12 medium-6"> <div class="small-12 medium-6">
<%= f.text_field :link_text %> <%= f.translatable_text_field :link_text %>
<%= f.label :link_text %>
</div> </div>
<div class="small-12 medium-6"> <div class="small-12 medium-6">

View File

@@ -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

View File

@@ -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", ["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 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| create_table "widget_cards", force: :cascade do |t|
t.string "title" t.string "title"
t.text "description" t.text "description"

View File

@@ -7,14 +7,19 @@ feature 'Cards' do
login_as(admin) login_as(admin)
end end
it_behaves_like "translatable",
"widget_card",
"edit_admin_widget_card_path",
%w[title description link_text label]
scenario "Create", :js do scenario "Create", :js do
visit admin_homepage_path visit admin_homepage_path
click_link "Create card" click_link "Create card"
fill_in "widget_card_label", with: "Card label" fill_in "widget_card_label_en", with: "Card label"
fill_in "widget_card_title", with: "Card text" fill_in "widget_card_title_en", with: "Card text"
fill_in "widget_card_description", with: "Card description" fill_in "widget_card_description_en", with: "Card description"
fill_in "widget_card_link_text", with: "Link text" fill_in "widget_card_link_text_en", with: "Link text"
fill_in "widget_card_link_url", with: "consul.dev" fill_in "widget_card_link_url", with: "consul.dev"
attach_image_to_card attach_image_to_card
click_button "Create card" click_button "Create card"
@@ -59,10 +64,10 @@ feature 'Cards' do
click_link "Edit" click_link "Edit"
end end
fill_in "widget_card_label", with: "Card label updated" fill_in "widget_card_label_en", with: "Card label updated"
fill_in "widget_card_title", with: "Card text updated" fill_in "widget_card_title_en", with: "Card text updated"
fill_in "widget_card_description", with: "Card description updated" fill_in "widget_card_description_en", with: "Card description updated"
fill_in "widget_card_link_text", with: "Link text updated" fill_in "widget_card_link_text_en", with: "Link text updated"
fill_in "widget_card_link_url", with: "consul.dev updated" fill_in "widget_card_link_url", with: "consul.dev updated"
click_button "Save card" click_button "Save card"
@@ -99,10 +104,10 @@ feature 'Cards' do
visit admin_homepage_path visit admin_homepage_path
click_link "Create header" click_link "Create header"
fill_in "widget_card_label", with: "Header label" fill_in "widget_card_label_en", with: "Header label"
fill_in "widget_card_title", with: "Header text" fill_in "widget_card_title_en", with: "Header text"
fill_in "widget_card_description", with: "Header description" fill_in "widget_card_description_en", with: "Header description"
fill_in "widget_card_link_text", with: "Link text" fill_in "widget_card_link_text_en", with: "Link text"
fill_in "widget_card_link_url", with: "consul.dev" fill_in "widget_card_link_url", with: "consul.dev"
click_button "Create header" click_button "Create header"

View File

@@ -192,6 +192,8 @@ def update_button_text
"Update poll" "Update poll"
when "Poll::Question" when "Poll::Question"
"Save" "Save"
when "Widget::Card"
"Save card"
else else
"Save changes" "Save changes"
end end