Module: LatoCms::PagesHelper

Defined in:
app/helpers/lato_cms/pages_helper.rb

Constant Summary collapse

BUILTIN_FIELD_TYPES =
%w[string textarea text number date datetime boolean select multiselect color json file image gallery].freeze

Instance Method Summary collapse

Instance Method Details

#lato_cms_attachment_path(attachment) ⇒ Object

Returns the path for an Active Storage attachment. Using main_app avoids the missing attachment_path error inside the engine.



7
8
9
10
11
# File 'app/helpers/lato_cms/pages_helper.rb', line 7

def lato_cms_attachment_path(attachment)
  main_app.rails_blob_path(attachment.blob, only_path: true)
rescue StandardError
  '#'
end

#lato_cms_field_dom_id(field_id, suffix = 'value', dom_id_prefix: nil) ⇒ Object



123
124
125
126
# File 'app/helpers/lato_cms/pages_helper.rb', line 123

def lato_cms_field_dom_id(field_id, suffix = 'value', dom_id_prefix: nil)
  base = dom_id_prefix.presence || "fields_#{field_id}"
  "#{base}_#{suffix}".parameterize(separator: '_')
end

#lato_cms_field_input_name(field_id, suffix = 'value', input_name_prefix: nil, multiple: false) ⇒ Object



117
118
119
120
121
# File 'app/helpers/lato_cms/pages_helper.rb', line 117

def lato_cms_field_input_name(field_id, suffix = 'value', input_name_prefix: nil, multiple: false)
  base = input_name_prefix.presence || "fields[#{field_id}]"
  name = "#{base}[#{suffix}]"
  multiple ? "#{name}[]" : name
end

#lato_cms_page_actions(page, show_edit: false, show_delete: false, hide_show: false) ⇒ Object

Index actions cell (also the actions column viewer). Inline buttons.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/lato_cms/pages_helper.rb', line 22

def lato_cms_page_actions(page, show_edit: false, show_delete: false, hide_show: false)
  btn_group = capture do
    (:div, class: 'btn-group btn-group-sm') do
      concat(link_to(t('lato_cms.cta_show'), lato_cms.pages_show_path(page), class: 'btn btn-primary')) unless hide_show
      if show_edit
        concat link_to(t('lato_cms.cta_edit'), lato_cms.pages_update_path(page), class: 'btn btn-secondary',
          data: { lato_action_target: 'trigger', turbo_frame: dom_id(page, 'form'), action_title: t('lato_cms.page_update_title') })
      end
      if show_delete
        concat link_to(t('lato_cms.cta_delete'), lato_cms.pages_destroy_action_path(page), class: 'btn btn-danger',
          data: { turbo_method: 'DELETE', turbo_confirm: t('lato_cms.cta_delete_confirm') })
      end
    end
  end

  (:div, class: 'd-flex align-items-center gap-2') do
    if page.frontend_url.blank?
      concat (:span, t('lato_cms.action_view_frontend'), class: 'btn btn-sm btn-link px-0 disabled text-muted')
    else
      concat link_to(t('lato_cms.action_view_frontend'), page.frontend_url, class: 'btn btn-sm btn-link px-0', target: '_blank')
    end
    concat btn_group
  end
end

#lato_cms_page_actions_dropdown(page, show_edit: false, show_translations: false, show_delete: false, hide_show: false) ⇒ Object

Show-page actions: single dropdown (view frontend, show, edit settings, translations, delete).



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/lato_cms/pages_helper.rb', line 74

def lato_cms_page_actions_dropdown(page, show_edit: false, show_translations: false, show_delete: false, hide_show: false)
  (:div, class: 'dropdown', title: t('lato_cms.cta_actions'), data: { controller: 'lato-tooltip' }) do
    concat button_tag(tag.i(class: 'bi bi-three-dots-vertical'), type: 'button',
      class: 'btn btn-sm btn-outline-primary', data: { bs_toggle: 'dropdown' })
    concat((:ul, class: 'dropdown-menu dropdown-menu-end') do
      if page.frontend_url.present?
        concat (:li, link_to(t('lato_cms.action_view_frontend'), page.frontend_url, class: 'dropdown-item', target: '_blank'))
      else
        concat (:li, (:span, t('lato_cms.action_view_frontend'), class: 'dropdown-item disabled text-muted'))
      end
      unless hide_show
        concat (:li, link_to(t('lato_cms.cta_show'), lato_cms.pages_show_path(page), class: 'dropdown-item'))
      end
      if show_edit
        concat (:li, link_to(t('lato_cms.cta_edit'), lato_cms.pages_update_path(page), class: 'dropdown-item',
          data: { lato_action_target: 'trigger', turbo_frame: dom_id(page, 'form'), action_title: t('lato_cms.page_update_title') }))
      end
      if show_translations
        concat (:li, link_to(t('lato_cms.translations_title'), lato_cms.pages_translations_path(page), class: 'dropdown-item',
          data: { lato_action_target: 'trigger', turbo_frame: dom_id(page, 'translations'), action_title: t('lato_cms.translations_title') }))
      end
      if show_delete
        concat (:li, tag.hr(class: 'dropdown-divider'))
        concat (:li, link_to(t('lato_cms.cta_delete'), lato_cms.pages_destroy_action_path(page), class: 'dropdown-item text-danger',
          data: { turbo_method: 'DELETE', turbo_confirm: t('lato_cms.cta_delete_confirm') }))
      end
    end)
  end
end

#lato_cms_page_locale(page) ⇒ Object

Index locale cell (also the locale column viewer). Static badge.



14
15
16
17
18
19
# File 'app/helpers/lato_cms/pages_helper.rb', line 14

def lato_cms_page_locale(page)
  (:span, class: 'badge bg-secondary') do
    concat locale_to_flag(page.locale)
    concat " #{page.locale.upcase}"
  end
end

#lato_cms_page_locale_dropdown(page) ⇒ Object

Show-page locale: dropdown to jump to the same page in another language.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/lato_cms/pages_helper.rb', line 48

def lato_cms_page_locale_dropdown(page)
  siblings = page.translations.order(:locale).to_a
  badge = capture do
    concat locale_to_flag(page.locale)
    concat " #{page.locale.upcase}"
  end

  return (:span, badge, class: 'badge bg-secondary') if siblings.empty?

  (:div, class: 'dropdown') do
    concat button_tag(badge, type: 'button', class: 'btn btn-sm btn-secondary dropdown-toggle', data: { bs_toggle: 'dropdown' })
    concat((:ul, class: 'dropdown-menu') do
      siblings.each do |sibling|
        concat((:li) do
          link_to lato_cms.pages_show_path(sibling), class: 'dropdown-item' do
            concat locale_to_flag(sibling.locale)
            concat " #{sibling.locale.upcase}#{sibling.title}"
          end
        end)
      end
    end)
  end
end

#lato_cms_render_field(field_id:, field_config:, page_field:, input_name_prefix: nil, dom_id_prefix: nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/lato_cms/pages_helper.rb', line 104

def lato_cms_render_field(field_id:, field_config:, page_field:, input_name_prefix: nil, dom_id_prefix: nil)
  render resolve_lato_cms_field_partial(field_config),
    field_id: field_id,
    field_config: field_config,
    page_field: page_field,
    input_name_prefix: input_name_prefix,
    dom_id_prefix: dom_id_prefix
rescue ActionView::MissingTemplate => e
  (:div, class: 'alert alert-danger mb-0') do
    "Field '#{field_id}' render error: #{e.message}"
  end
end