Move form field partial to a component

This way it's easier to test; changing it will also be easier.

During my experiments I made a mistake which wasn't covered by the test
suite. We're adding a test for this case.

Note we're using `i18n_content` in the component instead of `content`
because there's already a `content` method provided by ViewComponent.
This commit is contained in:
Javi Martín
2021-10-05 19:05:05 +02:00
parent ced55a58f8
commit b35c8bda4b
6 changed files with 43 additions and 18 deletions

View File

@@ -0,0 +1,9 @@
<% globalize(locale) do %>
<%= hidden_field_tag "contents[content_#{i18n_content.key}][id]", i18n_content.key %>
<%= text_area_tag "contents[content_#{i18n_content.key}]values[value_#{locale}]",
text || t(i18n_content.key, locale: locale),
rows: 5,
class: "js-globalize-attribute",
style: site_customization_display_translation_style(locale),
data: { locale: locale } %>
<% end %>

View File

@@ -0,0 +1,19 @@
class Admin::SiteCustomization::InformationTexts::FormFieldComponent < ApplicationComponent
attr_reader :i18n_content, :locale
delegate :globalize, :site_customization_display_translation_style, to: :helpers
def initialize(i18n_content, locale:)
@i18n_content = i18n_content
@locale = locale
end
private
def text
if i18n_content.present?
I18nContentTranslation.find_by(i18n_content_id: i18n_content.id, locale: locale)&.value
else
false
end
end
end

View File

@@ -7,14 +7,6 @@ module SiteCustomizationHelper
site_customization_enable_translation?(locale) ? "" : "display: none;" site_customization_enable_translation?(locale) ? "" : "display: none;"
end end
def translation_for_locale(content, locale)
if content.present?
I18nContentTranslation.find_by(i18n_content_id: content.id, locale: locale)&.value
else
false
end
end
def information_texts_tabs def information_texts_tabs
[:basic, :debates, :community, :proposals, :polls, :layouts, :mailers, :management, :welcome, :machine_learning] [:basic, :debates, :community, :proposals, :polls, :layouts, :mailers, :management, :welcome, :machine_learning]
end end

View File

@@ -8,7 +8,7 @@
<% group.each do |content| %> <% group.each do |content| %>
<b><%= content.key %></b> <b><%= content.key %></b>
<% content.globalize_locales.each do |locale| %> <% content.globalize_locales.each do |locale| %>
<%= render "form_field", content: content, locale: locale %> <%= render Admin::SiteCustomization::InformationTexts::FormFieldComponent.new(content, locale: locale) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

View File

@@ -1,9 +0,0 @@
<% globalize(locale) do %>
<%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %>
<%= text_area_tag "contents[content_#{content.key}]values[value_#{locale}]",
translation_for_locale(content, locale) || t(content.key, locale: locale),
rows: 5,
class: "js-globalize-attribute",
style: site_customization_display_translation_style(locale),
data: { locale: locale } %>
<% end %>

View File

@@ -0,0 +1,14 @@
require "rails_helper"
describe Admin::SiteCustomization::InformationTexts::FormFieldComponent do
after { I18n.backend.reload! }
it "uses the I18n translation when the record exists without a database translation" do
I18n.backend.store_translations(:en, { testing: "It works!" })
content = I18nContent.create!(key: "testing")
render_inline Admin::SiteCustomization::InformationTexts::FormFieldComponent.new(content, locale: :en)
expect(page).to have_field with: "It works!"
end
end