Extract model to handle locales settings
This way we can simplify the view, particularly the form. However, we're still adding some complexity to the form so inputs are inside labels and so the collection is easier to style with CSS.
This commit is contained in:
@@ -1,37 +1,36 @@
|
||||
<%= form_tag admin_locales_path, method: :patch, class: "locales-form" do %>
|
||||
<%= form_for locales_settings, url: admin_locales_path, html: { class: "locales-form" } do |f| %>
|
||||
<% if many_available_locales? %>
|
||||
<%= label_tag "default_locale", t("admin.locales.default") %>
|
||||
<p class="help-text"><%= sanitize(t("admin.locales.default_help_text")) %></p>
|
||||
<%= select_tag "default_locale", locales_options %>
|
||||
<%= f.select :default, locales_options, hint: sanitize(t("admin.locales.default_help_text")) %>
|
||||
<% else %>
|
||||
<fieldset>
|
||||
<legend><%= t("admin.locales.default") %></legend>
|
||||
<legend><%= attribute_name(:default) %></legend>
|
||||
<p class="help-text"><%= sanitize(t("admin.locales.default_help_text")) %></p>
|
||||
|
||||
<div class="collection-radio-buttons">
|
||||
<% available_locales.each do |locale| %>
|
||||
<%= label_tag "default_locale_#{locale}" do %>
|
||||
<%= radio_button_tag "default_locale", locale, locale == default %>
|
||||
<%= name_for_locale(locale) %>
|
||||
<% end %>
|
||||
<%= f.collection_radio_buttons(
|
||||
:default,
|
||||
available_locales,
|
||||
:to_sym,
|
||||
->(locale) { name_for_locale(locale) }
|
||||
) do |b| %>
|
||||
<%= b.label { b.radio_button + b.text } %>
|
||||
<% end %>
|
||||
</div>
|
||||
</fieldset>
|
||||
<% end %>
|
||||
|
||||
<fieldset>
|
||||
<legend><%= t("admin.locales.enabled") %></legend>
|
||||
<legend><%= attribute_name(:enabled) %></legend>
|
||||
<p class="help-text"><%= t("admin.locales.enabled_help_text") %></p>
|
||||
|
||||
<div class="collection-check-boxes">
|
||||
<% available_locales.each do |locale| %>
|
||||
<%= label_tag "enabled_locales_#{locale}" do %>
|
||||
<%= check_box_tag "enabled_locales[]",
|
||||
locale,
|
||||
enabled_locales.include?(locale),
|
||||
id: "enabled_locales_#{locale}" %>
|
||||
<%= name_for_locale(locale) %>
|
||||
<% end %>
|
||||
<%= f.collection_check_boxes(
|
||||
:enabled,
|
||||
available_locales,
|
||||
:to_sym,
|
||||
->(locale) { name_for_locale(locale) }
|
||||
) do |b| %>
|
||||
<%= b.label { b.check_box + b.text } %>
|
||||
<% end %>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
class Admin::Locales::FormComponent < ApplicationComponent
|
||||
attr_reader :enabled_locales, :default
|
||||
attr_reader :locales_settings
|
||||
use_helpers :name_for_locale
|
||||
|
||||
def initialize(enabled_locales, default:)
|
||||
@enabled_locales = enabled_locales
|
||||
@default = default
|
||||
def initialize(locales_settings)
|
||||
@locales_settings = locales_settings
|
||||
end
|
||||
|
||||
private
|
||||
@@ -18,13 +17,14 @@ class Admin::Locales::FormComponent < ApplicationComponent
|
||||
end
|
||||
|
||||
def locales_options
|
||||
options_for_select(
|
||||
available_locales.map { |locale| [name_for_locale(locale), locale] },
|
||||
default
|
||||
)
|
||||
available_locales.map { |locale| [name_for_locale(locale), locale] }
|
||||
end
|
||||
|
||||
def select_field_threshold
|
||||
10
|
||||
end
|
||||
|
||||
def attribute_name(...)
|
||||
locales_settings.class.human_attribute_name(...)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<%= header %>
|
||||
<% provide :main_class, "admin-locales-show" %>
|
||||
|
||||
<%= render Admin::Locales::FormComponent.new(locales, default: default) %>
|
||||
<%= render Admin::Locales::FormComponent.new(locales_settings) %>
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
class Admin::Locales::ShowComponent < ApplicationComponent
|
||||
include Header
|
||||
attr_reader :locales, :default
|
||||
attr_reader :locales_settings
|
||||
|
||||
def initialize(locales, default:)
|
||||
@locales = locales
|
||||
@default = default
|
||||
def initialize(locales_settings)
|
||||
@locales_settings = locales_settings
|
||||
end
|
||||
|
||||
def title
|
||||
|
||||
Reference in New Issue
Block a user