Module: Releaf::Builders::FormBuilder::I18nFields

Included in:
Fields
Defined in:
app/builders/releaf/builders/form_builder/i18n_fields.rb

Instance Method Summary collapse

Instance Method Details

#default_localeObject


71
72
73
74
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 71

def default_locale
  selected_locale = (layout_settings("releaf.i18n.locale") || I18n.locale).to_sym
  locales.include?(selected_locale) ? selected_locale : locales.first
end

#localesObject


67
68
69
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 67

def locales
  object.class.globalize_locales
end

#localization_switchObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 50

def localization_switch
  tag(:div, class: "localization-switch") do
    button_tag(type: 'button', title: t('Switch locale'), class: "trigger") do
      tag(:span, default_locale, class: "label") + tag(:i, nil, class: ["fa", "fa-chevron-down"])
    end <<
    tag(:menu, class: ["localization-menu-items"], type: 'toolbar') do
      tag(:ul) do
        object.class.globalize_locales.collect do |locale|
          tag(:li) do
            tag(:button, translate_locale(locale), type: "button", data: {locale: locale})
          end
        end
      end
    end
  end
end

#localized_field(name, field_type, input: {}, label: {}, field: {}, options: {}) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 28

def localized_field(name, field_type, input: {}, label: {}, field: {}, options: {})
  options = {i18n: true, label: {translation_key: name}}.deep_merge(options)

  wrapper(field_attributes(name, field, options)) do
    content = object.class.globalize_locales.collect do |locale|
      localized_name = "#{name}_#{locale}"
      html_class = ["localization"]
      html_class << "active" if locale == default_locale

      tag(:div, class: html_class, data: {locale: locale}) do
        releaf_label(localized_name, label, options) <<
        tag(:div, class: "value") do
          attributes = input_attributes(name, {value: object.send(localized_name)}.merge(input), options)
          send(field_type, localized_name, attributes)
        end
      end
    end

    content << localization_switch
  end
end

8
9
10
11
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 8

def releaf_link_i18n_field(name, input: {}, label: {}, field: {}, options: {})
  options = {field: {type: "link"}}.deep_merge(options)
  releaf_text_i18n_field(name, input: input, label: label, field: field, options: options)
end

#releaf_richtext_i18n_field(name, input: {}, label: {}, field: {}, options: {}) ⇒ Object


22
23
24
25
26
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 22

def releaf_richtext_i18n_field(name, input: {}, label: {}, field: {}, options: {})
  input = richtext_input_attributes(name).merge(input)
  options = richtext_options(name, options)
  releaf_textarea_i18n_field(name, input: input, label: label, field: field, options: options)
end

#releaf_text_i18n_field(name, input: {}, label: {}, field: {}, options: {}) ⇒ Object


2
3
4
5
6
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 2

def releaf_text_i18n_field(name, input: {}, label: {}, field: {}, options: {})
  options = {field: {type: "text"}}.deep_merge(options)
  input = {class: "text"}.deep_merge(input)
  localized_field(name, :text_field, input: input, label: label, field: field, options: options)
end

#releaf_textarea_i18n_field(name, input: {}, label: {}, field: {}, options: {}) ⇒ Object


13
14
15
16
17
18
19
20
# File 'app/builders/releaf/builders/form_builder/i18n_fields.rb', line 13

def releaf_textarea_i18n_field(name, input: {}, label: {}, field: {}, options: {})
  input = {
    rows: 5,
    cols: 75,
  }.merge(input)
  options = {field: {type: "textarea"}}.deep_merge(options)
  localized_field(name, :text_area, input: input, label: label, field: field, options: options)
end