Module: Lcms::Engine::ViewHelper

Included in:
Admin::ResourcesController, ApplicationHelper
Defined in:
app/helpers/lcms/engine/view_helper.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

ENABLE_BASE64_CACHING =
ActiveRecord::Type::Boolean.new.cast ENV.fetch('ENABLE_BASE64_CACHING', true)

Instance Method Summary collapse

Instance Method Details

#add_class_for_action(controller_name, action_name, klass, klass_prefix = nil) ⇒ Object



15
16
17
18
19
20
# File 'app/helpers/lcms/engine/view_helper.rb', line 15

def add_class_for_action(controller_name, action_name, klass, klass_prefix = nil)
  if controller.controller_name == controller_name.to_s && controller.action_name == action_name.to_s
    sufix = klass
  end
  "#{klass_prefix} #{sufix}"
end

#add_class_for_path(link_path, klass, klass_prefix = nil) ⇒ Object



8
9
10
11
12
13
# File 'app/helpers/lcms/engine/view_helper.rb', line 8

def add_class_for_path(link_path, klass, klass_prefix = nil)
  [
    klass_prefix,
    current_page?(link_path) ? klass : nil
  ].compact.join(' ')
end

#base64_encoded_asset(path) ⇒ Object



86
87
88
# File 'app/helpers/lcms/engine/view_helper.rb', line 86

def base64_encoded_asset(path)
  AssetHelper.base64_encoded path, cache: ENABLE_BASE64_CACHING
end

#color_code(model, base: false) ⇒ Object



100
101
102
103
104
# File 'app/helpers/lcms/engine/view_helper.rb', line 100

def color_code(model, base: false)
  subject_color_code = model.try(:subject) || 'default'
  grade_avg = base ? 'base' : model.grades.average
  "#{subject_color_code}-#{grade_avg}"
end

#flash_to_hashObject



27
28
29
30
31
32
33
34
# File 'app/helpers/lcms/engine/view_helper.rb', line 27

def flash_to_hash
  result = { message: nil, message_type: 'alert', status: false }
  flash.to_hash.slice('notice', 'alert').each do |name, message|
    result = { message: result[:message].present? ? "#{result.message}\n#{message}" : message,
               message_type: name, status: true }
  end
  result
end

#header_modObject



36
37
38
# File 'app/helpers/lcms/engine/view_helper.rb', line 36

def header_mod
  controller.controller_name
end

#inlined_asset(path) ⇒ Object



90
91
92
# File 'app/helpers/lcms/engine/view_helper.rb', line 90

def inlined_asset(path)
  AssetHelper.inlined path
end

#lcms_engine_url_helpersObject



118
119
120
# File 'app/helpers/lcms/engine/view_helper.rb', line 118

def lcms_engine_url_helpers
  @lcms_engine_url_helpers ||= Lcms::Engine::Engine.routes.url_helpers
end


22
23
24
25
# File 'app/helpers/lcms/engine/view_helper.rb', line 22

def nav_link(link_text, link_path, attrs = {}, link_attrs = {})
  cls = add_class_for_path(link_path, 'active', attrs[:class])
  (:li, attrs.merge(class: cls)) { link_to link_text, link_path, link_attrs }
end

#page_descriptionObject



44
45
46
# File 'app/helpers/lcms/engine/view_helper.rb', line 44

def page_description
  page_content_for :description
end

#page_og_imageObject



48
49
50
51
52
53
54
55
56
# File 'app/helpers/lcms/engine/view_helper.rb', line 48

def page_og_image
  if content_for?(:og_image)
    page_og_image = content_for(:og_image)
  else
    controller = controller_path.tr('/', '.')
    page_og_image = t("#{controller}.#{action_name}.og_image", default: t('default_og_image'))
  end
  page_og_image
end

#page_titleObject



40
41
42
# File 'app/helpers/lcms/engine/view_helper.rb', line 40

def page_title
  page_content_for :page_title
end

#redirect_meta_tagObject

Render meta tag



59
60
61
# File 'app/helpers/lcms/engine/view_helper.rb', line 59

def redirect_meta_tag
  content_for(:redirect_meta_tag) if content_for?(:redirect_meta_tag)
end

#redirect_via_meta_tag(to_url:, delay: 5) ⇒ Object

Use in views as redirection directive



64
65
66
67
68
# File 'app/helpers/lcms/engine/view_helper.rb', line 64

def redirect_via_meta_tag(to_url:, delay: 5)
  content_for(:redirect_meta_tag) do
    (:meta, nil, { content: "#{delay};url=#{to_url}", 'http-equiv' => 'refresh' }, true)
  end
end

#selected_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/lcms/engine/view_helper.rb', line 106

def selected_id?(id)
  selected_ids = params[:selected_ids]
  return unless selected_ids.present?

  case selected_ids
  when Array
    selected_ids.include?(id.to_s)
  else
    selected_ids.split(',').include?(id.to_s)
  end
end

#set_canonical_url(value) ⇒ Object

rubocop:disable Naming/AccessorMethodName



82
83
84
# File 'app/helpers/lcms/engine/view_helper.rb', line 82

def set_canonical_url(value) # rubocop:disable Naming/AccessorMethodName
  content_for(:canonical_url, value)
end

#set_page_description(dsc) ⇒ Object

rubocop:disable Naming/AccessorMethodName



76
77
78
79
80
# File 'app/helpers/lcms/engine/view_helper.rb', line 76

def set_page_description(dsc) # rubocop:disable Naming/AccessorMethodName
  content_for :description do
    dsc
  end
end

#set_page_title(title) ⇒ Object

rubocop:disable Naming/AccessorMethodName



70
71
72
73
74
# File 'app/helpers/lcms/engine/view_helper.rb', line 70

def set_page_title(title) # rubocop:disable Naming/AccessorMethodName
  content_for :page_title do
    title
  end
end

#strip_tags_and_squish(str) ⇒ Object



94
95
96
97
98
# File 'app/helpers/lcms/engine/view_helper.rb', line 94

def strip_tags_and_squish(str)
  return unless str.respond_to? :squish

  strip_tags(str).squish
end