Skip to content

Instantly share code, notes, and snippets.

@mlovic
Created September 18, 2018 12:23
Show Gist options
  • Save mlovic/29f7272b4c0c4b2d1ef8aef69c1fadf2 to your computer and use it in GitHub Desktop.
Save mlovic/29f7272b4c0c4b2d1ef8aef69c1fadf2 to your computer and use it in GitHub Desktop.
All changes made to banner-related files during the translatable code refactor
Generated with:
git diff ayto/master..javier/refactor-translatable-specs -- `find . -not -path '*/\.*' -name '*banner*'`
diff --git a/app/controllers/admin/banners_controller.rb b/app/controllers/admin/banners_controller.rb
index 7d3773e..b17c17d 100644
--- a/app/controllers/admin/banners_controller.rb
+++ b/app/controllers/admin/banners_controller.rb
@@ -41,8 +41,9 @@ class Admin::BannersController < Admin::BaseController
attributes = [:title, :description, :target_url,
:post_started_at, :post_ended_at,
:background_color, :font_color,
+ *translation_params(Banner),
web_section_ids: []]
- params.require(:banner).permit(*attributes, *translation_params(params[:banner]))
+ params.require(:banner).permit(*attributes)
end
def banner_styles
@@ -65,8 +66,4 @@ class Admin::BannersController < Admin::BaseController
@banner = Banner.find(params[:id]) unless @banner
@banner
end
-
- def resource_model
- Banner
- end
end
diff --git a/app/models/banner.rb b/app/models/banner.rb
index 8ad7b44..37824d0 100644
--- a/app/models/banner.rb
+++ b/app/models/banner.rb
@@ -5,7 +5,7 @@ class Banner < ActiveRecord::Base
translates :title, touch: true
translates :description, touch: true
- globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br]
+ globalize_accessors
validates :title, presence: true,
length: { minimum: 2 }
diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb
index 495cc5e..28e8a71 100644
--- a/app/views/admin/banners/_form.html.erb
+++ b/app/views/admin/banners/_form.html.erb
@@ -1,13 +1,9 @@
<%= render "admin/shared/globalize_locales", resource: @banner %>
-<%= form_for [:admin, @banner] do |f| %>
+<%= translatable_form_for [:admin, @banner] do |f| %>
<%= render 'errors' %>
- <% @banner.globalize_locales.each do |locale| %>
- <%= hidden_field_tag "delete_translations[#{locale}]", 0 %>
- <% end %>
-
<div class="row">
<% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %>
<div class="small-12 medium-3 column">
@@ -33,19 +29,10 @@
<div class="row">
<div class="small-12 medium-6 column">
- <%= f.label :title, t("admin.banners.banner.title") %>
-
- <% @banner.globalize_locales.each do |locale| %>
- <% globalize(locale) do %>
- <%= f.text_field "title_#{locale}",
- placeholder: t("admin.banners.banner.title"),
- class: "js-globalize-attribute",
- data: {js_banner_title: "js_banner_title",
- locale: locale},
- style: display_translation?(locale),
- label: false %>
- <% end %>
- <% end %>
+ <%= f.translatable_text_field :title,
+ placeholder: t("admin.banners.banner.title"),
+ data: {js_banner_title: "js_banner_title"},
+ label: t("admin.banners.banner.title") %>
</div>
<div class="small-12 medium-6 column">
@@ -58,18 +45,10 @@
<div class="row">
<div class="small-12 column">
- <%= f.label :description, t("admin.banners.banner.description") %>
- <% @banner.globalize_locales.each do |locale| %>
- <% globalize(locale) do %>
- <%= f.text_field "description_#{locale}",
- placeholder: t("admin.banners.banner.description"),
- class: "js-globalize-attribute",
- data: {js_banner_description: "js_banner_description",
- locale: locale},
- style: display_translation?(locale),
- label: false %>
- <% end %>
- <% end %>
+ <%= f.translatable_text_field :description,
+ placeholder: t("admin.banners.banner.description"),
+ data: {js_banner_description: "js_banner_description"},
+ label: t("admin.banners.banner.description") %>
</div>
</div>
diff --git a/db/dev_seeds/banners.rb b/db/dev_seeds/banners.rb
index 68f28be..641f519 100644
--- a/db/dev_seeds/banners.rb
+++ b/db/dev_seeds/banners.rb
@@ -10,8 +10,7 @@ section "Creating banners" do
post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)),
created_at: rand((Time.current - 1.week)..Time.current))
I18n.available_locales.map do |locale|
- neutral_locale = locale.to_s.downcase.underscore.to_sym
- Globalize.with_locale(neutral_locale) do
+ Globalize.with_locale(locale) do
banner.description = "Description for locale #{locale}"
banner.title = "Title for locale #{locale}"
banner.save!
diff --git a/spec/features/admin/banners_spec.rb b/spec/features/admin/banners_spec.rb
index cf3dfb1..6eaaea4 100644
--- a/spec/features/admin/banners_spec.rb
+++ b/spec/features/admin/banners_spec.rb
@@ -6,6 +6,11 @@ feature 'Admin banners magement' do
login_as(create(:administrator).user)
end
+ it_behaves_like "translatable",
+ "banner",
+ "edit_admin_banner_path",
+ %w[title description]
+
context "Index" do
background do
@banner1 = create(:banner, title: "Banner number one",
@@ -183,123 +188,4 @@ feature 'Admin banners magement' do
visit admin_root_path
expect(page).not_to have_content 'Ugly banner'
end
-
- context "Translations" do
-
- let(:banner) { create(:banner, title_en: "Title in English",
- title_es: "Título en Español",
- target_url: 'http://url.com',
- description_en: "Description in English",
- description_es: "Descripción en Español") }
-
- before do
- @edit_banner_url = edit_admin_banner_path(banner)
- end
-
- scenario "Add a translation", :js do
- visit @edit_banner_url
-
- select "Français", from: "translation_locale"
- fill_in 'banner_title_fr', with: 'Titre en Français'
- fill_in 'banner_description_fr', with: 'Description en Français'
-
- click_button 'Save changes'
-
- visit @edit_banner_url
- expect(page).to have_field('banner_description_en', with: 'Description in English')
-
- click_link "Español"
- expect(page).to have_field('banner_description_es', with: 'Descripción en Español')
-
- click_link "Français"
- expect(page).to have_field('banner_description_fr', with: 'Description en Français')
- end
-
- scenario "Update a translation", :js do
- banner.update_attributes(target_url: 'http://www.url.com',
- post_started_at: (Time.current - 4.days),
- post_ended_at: (Time.current + 10.days))
-
- section = create(:web_section, name: 'debates')
- create(:banner_section, web_section: section, banner_id: banner.id)
-
- visit @edit_banner_url
-
- click_link "Español"
- fill_in 'banner_title_es', with: 'Título correcto en Español'
-
- click_button 'Save changes'
-
- visit debates_path
-
- within('.banner') do
- expect(page).to have_content("Description in English")
- end
-
- select('Español', from: 'locale-switcher')
-
- within('.banner') do
- expect(page).to have_content('Título correcto en Español')
- end
- end
-
- scenario "Remove a translation", :js do
-
- visit @edit_banner_url
-
- click_link "Español"
- click_link "Remove language"
-
- expect(page).not_to have_link "Español"
-
- click_button "Save changes"
- visit @edit_banner_url
- expect(page).not_to have_link "Español"
- end
-
- context "Globalize javascript interface" do
-
- scenario "Highlight current locale", :js do
- visit @edit_banner_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_banner_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_banner_url
-
- expect(page).to have_field('banner_description_en', with: 'Description in English')
-
- click_link "Español"
-
- expect(page).to have_field('banner_description_es', with: 'Descripción en Español')
- end
-
- scenario "Select a locale and add it to the banner form", :js do
- visit @edit_banner_url
-
- select "Français", from: "translation_locale"
-
- expect(page).to have_link "Français"
-
- click_link "Français"
-
- expect(page).to have_field('banner_description_fr')
- end
- end
- end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment