Module: Integral::Backend::BaseHelper

Includes:
SupportHelper, Webpacker::Helper
Defined in:
app/helpers/integral/backend/base_helper.rb

Overview

Base Backend Helper

Instance Method Summary collapse

Methods included from SupportHelper

#anchor_to, #display_media_query_indicator?, #icon, #method_missing, #respond_to?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Integral::SupportHelper

Instance Method Details

#body_data_attributesObject

Data which is embedded into the backend <body> tag



149
150
151
152
153
154
155
156
157
158
# File 'app/helpers/integral/backend/base_helper.rb', line 149

def body_data_attributes
  {
    locale: I18n.locale,
    'user-name' => current_user.name,
    'user-email' => current_user.email,
    'user-created-at' => current_user.created_at.to_i,
    'flash-type' => flash.first&.first,
    'flash-message' => flash.first&.second
  }
end

#create_menu_itemsObject



203
204
205
# File 'app/helpers/integral/backend/base_helper.rb', line 203

def create_menu_items
  extract_menu_items(Integral::ActsAsIntegral.backend_create_menu_items, :integral_backend_create_menu_item).sort_by { |item| item[:order] }
end

#current_user_authorized_for_menu_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
# File 'app/helpers/integral/backend/base_helper.rb', line 189

def current_user_authorized_for_menu_item?(item)
  if item[:authorize]
    instance_eval(&item[:authorize])
  elsif item[:authorize_class] && item[:authorize_action]
    Pundit.policy(current_user, item[:authorize_class]).public_send("#{item[:authorize_action]}?")
  else
    true
  end
end

#current_webpacker_instanceObject



42
43
44
# File 'app/helpers/integral/backend/base_helper.rb', line 42

def current_webpacker_instance
  Integral.webpacker
end

#dataset_dashboard_last_weekObject

Line graph - Last week



177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/integral/backend/base_helper.rb', line 177

def dataset_dashboard_last_week
  data = [
    { scope: Integral::Page, label: 'Pages' },
    { scope: Integral::List, label: 'Lists' },
    { scope: Integral::Storage::File, label: 'Files' },
    { scope: Integral::User, label: 'Users' }
  ]

  data.prepend(scope: Integral::Post, label: 'Posts') if Integral.blog_enabled?
  data
end

#decorated_current_userObject



46
47
48
# File 'app/helpers/integral/backend/base_helper.rb', line 46

def decorated_current_user
  @decorated_current_user ||= current_user.decorate
end

#google_tag_manager(type = :script) ⇒ String

Backend Google Tag Manager Snippet

Returns:

  • (String)

    GTM Container if ID has been supplied



162
163
164
# File 'app/helpers/integral/backend/base_helper.rb', line 162

def google_tag_manager(type = :script)
  GoogleTagManager.render(Integral.gtm_container_id, type)
end

#grouped_page_alternate_optionsObject



33
34
35
36
37
38
39
40
# File 'app/helpers/integral/backend/base_helper.rb', line 33

def grouped_page_alternate_options
  Integral::Page.published.where.not(id: @resource.id).order('updated_at DESC').group_by(&:locale).map do |result|
    [
      t("integral.language.#{result[0]}"),
      result[1].map { |page| ["#{page.title} - #{page.path} (##{page.id})", page.id, {disabled: @resource.alternate_ids.include?(page.id), data: { title: page.title, description: page.description, path: page.path, url: backend_page_url(page.id) } }] }
    ]
  end.to_h
end

#grouped_page_parent_optionsObject



15
16
17
18
19
20
21
22
# File 'app/helpers/integral/backend/base_helper.rb', line 15

def grouped_page_parent_options
  @resource.available_parents.order('updated_at DESC').group_by(&:locale).map do |result|
    [
      t("integral.language.#{result[0]}"),
      result[1].map { |page| [ "#{page.title} - #{page.path} (##{page.id})", page.id ] }
    ]
  end.to_h
end

#grouped_post_alternate_optionsObject



24
25
26
27
28
29
30
31
# File 'app/helpers/integral/backend/base_helper.rb', line 24

def grouped_post_alternate_options
  Integral::Post.published.where.not(id: @resource.id).order('updated_at DESC').group_by(&:locale).map do |result|
    [
      t("integral.language.#{result[0]}"),
      result[1].map { |post| ["#{post.title} - #{post.slug} (##{post.id})", post.id, {disabled: @resource.alternate_ids.include?(post.id), data: { title: post.title, description: post.description, path: post.slug, url: backend_post_url(post.id) } }] }
    ]
  end.to_h
end

Handles extra optional options to ‘link_to` - Font awesome icons & wrapper



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/integral/backend/base_helper.rb', line 55

def link_to(name = nil, options = nil, html_options = nil, &block)
  return super if block_given?
  return super if html_options.nil?

  if html_options[:icon]
    name = (:span, name)
    name.prepend(icon(html_options.delete(:icon)))
  end

  if html_options[:wrapper]
    wrapper = html_options.delete(:wrapper)
    if wrapper == :cell
      (:div, super(name, options, html_options, &block), class: 'cell')
    else
      (wrapper, super(name, options, html_options, &block))
    end
  else
    super(name, options, html_options, &block)
  end
end


199
200
201
# File 'app/helpers/integral/backend/base_helper.rb', line 199

def main_menu_items
  extract_menu_items(Integral::ActsAsIntegral.backend_main_menu_items, :integral_backend_main_menu_item).sort_by { |item| item[:order] }
end

#page_titleString

Returns title provided through yield or i18n scoped to controller namespace & action.

Returns:

  • (String)

    title provided through yield or i18n scoped to controller namespace & action



126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/integral/backend/base_helper.rb', line 126

def page_title
  return content_for(:title) if content_for?(:title)
  return t("devise.#{controller_name}.#{action_name}.title") if devise_controller?

  # Scope is set to current controller namespace & action
  t('title', scope: "#{controller_path.tr('/', '.')}.#{action_name}",
             default: I18n.t("integral.backend.titles.#{action_name}",
                             type_singular: resource_klass.model_name.human.capitalize,
                             type_plural: resource_klass.model_name.human(count: 2).capitalize))
end

#recent_activity_grid(options) ⇒ Object



119
120
121
122
123
# File 'app/helpers/integral/backend/base_helper.rb', line 119

def recent_activity_grid(options)
  Integral::Grids::ActivitiesGrid.new options do |scope|
    scope.where.not(whodunnit: nil)
  end
end

#recent_site_activity_gridArray

Returns array of VersionDecorators subclassed depending on the Version subclass

Returns:

  • (Array)

    returns array of VersionDecorators subclassed depending on the Version subclass



109
110
111
112
113
114
115
116
117
# File 'app/helpers/integral/backend/base_helper.rb', line 109

def recent_site_activity_grid
  @recent_site_activity_grid ||= begin
                                options = {}
                                options[:object] = resource_klass.to_s if resource_klass.present?
                                options[:item_id] = @resource.id if @resource.present?

                                recent_activity_grid(options)
                              end
end

#recent_user_activity_gridArray

Returns array of VersionDecorators subclassed depending on the Version subclass

Returns:

  • (Array)

    returns array of VersionDecorators subclassed depending on the Version subclass



98
99
100
101
102
103
104
105
106
# File 'app/helpers/integral/backend/base_helper.rb', line 98

def recent_user_activity_grid
  @recent_user_activity_grid ||= begin
                                options = { user: current_user.id }
                                options[:object] = resource_klass.to_s if resource_klass.present?
                                options[:item_id] = @resource.id if @resource.present?

                                recent_activity_grid(options)
                              end
end

#recent_user_notificationsObject



50
51
52
# File 'app/helpers/integral/backend/base_helper.rb', line 50

def recent_user_notifications
  @recent_user_notifications ||= current_user.notifications.recent
end

#render_card(partial, locals = {}) ⇒ String

Returns Integral card.

Returns:

  • (String)

    Integral card



93
94
95
# File 'app/helpers/integral/backend/base_helper.rb', line 93

def render_card(partial, locals = {})
  render(partial: "integral/backend/shared/cards/#{partial}", locals: locals)
end

#render_data_gridObject

Renders a grid from a local partial within a datagrid container



138
139
140
141
142
143
144
145
146
# File 'app/helpers/integral/backend/base_helper.rb', line 138

def render_data_grid
  unless block_given?
    return (:div, render(partial: 'grid', locals: { grid: @grid }), data: { 'controller' => 'grid', 'form' => 'grid_form' }, class: 'grid-container')
  end

  (:div, data: { 'controller' => 'grid', 'form' => 'grid_form' }, class: 'grid-container') do
    yield
  end
end

#render_donut_chart(dataset) ⇒ Object

Renders a donut chart



172
173
174
# File 'app/helpers/integral/backend/base_helper.rb', line 172

def render_donut_chart(dataset)
  ChartRenderer::Donut.render(dataset)
end

#render_line_chart(dataset) ⇒ Object

Renders a line chart



167
168
169
# File 'app/helpers/integral/backend/base_helper.rb', line 167

def render_line_chart(dataset)
  ChartRenderer::Line.render(dataset)
end

#render_resource_grid(locals = {}) ⇒ String

Returns Resource Grid.

Returns:

  • (String)

    Resource Grid



86
87
88
89
90
# File 'app/helpers/integral/backend/base_helper.rb', line 86

def render_resource_grid(locals = {})
  render_data_grid do
    render(partial: "integral/backend/shared/grid/grid", locals: locals)
  end
end

#render_resource_grid_form(&block) ⇒ String

Returns Resource Grid Form.

Returns:

  • (String)

    Resource Grid Form



77
78
79
80
81
82
83
# File 'app/helpers/integral/backend/base_helper.rb', line 77

def render_resource_grid_form(&block)
  if block_given?
    render(layout: "integral/backend/shared/grid/form", &block)
  else
    render(partial: "integral/backend/shared/grid/form")
  end
end

#storage_file_content_type_optionsObject



11
12
13
# File 'app/helpers/integral/backend/base_helper.rb', line 11

def storage_file_content_type_options
  ActiveStorage::Blob.distinct.pluck(:content_type).sort
end