Module: Olivander::ApplicationHelper

Defined in:
app/helpers/olivander/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#action_klass_for(resource) ⇒ Object

[View source]

67
68
69
70
# File 'app/helpers/olivander/application_helper.rb', line 67

def action_klass_for(resource)
  klazz = resource.is_a?(Class) ? resource : resource.class
  klazz.respond_to?(:action_klass) ? klazz.action_klass : klazz
end

#action_resource_for(resource) ⇒ Object

[View source]

72
73
74
# File 'app/helpers/olivander/application_helper.rb', line 72

def action_resource_for(resource)
  resource.respond_to?(:action_resource) ? resource.action_resource : resource
end

#authorized_resource_action?(action, resource, except = nil) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

76
77
78
79
80
# File 'app/helpers/olivander/application_helper.rb', line 76

def authorized_resource_action?(action, resource, except = nil)
  return false if action == except

  respond_to?('can?') ? can?(action, resource) : true
end

#authorized_resource_actions(resource, for_action: :show) ⇒ Object

[View source]

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/olivander/application_helper.rb', line 38

def authorized_resource_actions(resource, for_action: :show)
  action_klass = action_klass_for(resource)
  action_resource = action_resource_for(resource)
  raw_name = action_klass.name
  plural_name = raw_name.demodulize.underscore.pluralize
  routed_resource = Olivander::CurrentContext.application_context.route_builder.resources[plural_name.to_sym]
  return [] if routed_resource.nil?

  actions = if resource.is_a?(Class)
              routed_resource.unpersisted_crud_actions | routed_resource.collection_actions.reject(&:crud_action)
            elsif resource.persisted?
              routed_resource.persisted_crud_actions | routed_resource.member_actions.reject(&:crud_action)
            else
              []
            end
  actions = actions.select { |a| authorized_resource_action?(a.sym, action_resource, for_action) }
  preferred = %i[show edit destroy]
  [].tap do |arr|
    preferred.each do |p|
      actions.each do |a|
        arr << a if a.sym == p
      end
    end
    actions.reject { |x| preferred.include?(x.sym) }.each do |a|
      arr << a
    end
  end
end

#chart_column_class_num(label, count, min) ⇒ Object

[View source]

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/olivander/application_helper.rb', line 3

def chart_column_class_num(label, count, min)
  return "col-#{label}-#{min}" if count >= 12

  modded = count % 12
  divisor = modded.zero? ? 1 : modded
  "col-#{label}-#{[12 / divisor, min].max}"

  divisor = count
  result = 12 / divisor
  while result < min
    divisor /= 2
    result = 12 / divisor
  end
  "col-#{label}-#{result}"
end

#collection_component_builder_for(klass, collection, *args, &block) ⇒ Object

[View source]

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/helpers/olivander/application_helper.rb', line 213

def collection_component_builder_for(klass, collection, *args, &block)
  builder = klass::Builder.new(self, &block)
  options = args.extract_options!
  options[:builder] = builder
  render klass.new(collection, *(args + [options])) do |component|
    if builder.header_tools.present?
      component.with_header_tools do
        builder.header_tools.call
      end
    end
    if builder.footer_buttons.present?
      component.with_footer_buttons do
        builder.footer_buttons.call
      end
    end
  end
end

#current_abilityObject

[View source]

124
125
126
# File 'app/helpers/olivander/application_helper.rb', line 124

def current_ability
  Olivander::CurrentContext.ability
end

#current_userObject

[View source]

120
121
122
# File 'app/helpers/olivander/application_helper.rb', line 120

def current_user
  Olivander::CurrentContext.user
end
[View source]

180
181
182
183
# File 'app/helpers/olivander/application_helper.rb', line 180

def favicon_link
  favicon_path = is_dev_environment? ? '/images/favicon-dev.png' : '/images/favicon.ico'
  favicon_link_tag(image_path(favicon_path))
end

#field_label_for(resource_class, sym) ⇒ Object

[View source]

142
143
144
145
146
147
148
# File 'app/helpers/olivander/application_helper.rb', line 142

def field_label_for(resource_class, sym)
  sym_s = sym.to_s.gsub('.', '_')
  i18n_key = "activerecord.attributes.#{resource_class.name.underscore}.#{sym_s}"
  return O18n.t(i18n_key) if I18n.exists?(i18n_key)

  sym.to_s.titleize
end

#flash_class(key) ⇒ Object

[View source]

185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/helpers/olivander/application_helper.rb', line 185

def flash_class(key)
  case key
  when 'error'
    'danger'
  when 'notice'
    'info'
  when 'alert'
    'danger'
  else
    key
  end
end

#flash_icon(key) ⇒ Object

[View source]

198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/helpers/olivander/application_helper.rb', line 198

def flash_icon(key)
  case key
  when 'error'
    'fas fa-exclamation-circle'
  when 'notice'
    'fas fa-info-circle'
  when 'alert'
    'fas fa-info-circle'
  when 'success'
    'fas fa-check-circle'
  else
    'fas fa-question-circle'
  end
end

#header_page_titleObject

[View source]

176
177
178
# File 'app/helpers/olivander/application_helper.rb', line 176

def header_page_title
  [is_dev_environment? ? sidebar_context_suffix.upcase : nil, page_title].reject(&:blank?).join(' ')
end

#is_dev_environment?Boolean

Returns:

  • (Boolean)
[View source]

150
151
152
# File 'app/helpers/olivander/application_helper.rb', line 150

def is_dev_environment?
  sidebar_context_suffix != 'production'
end

#page_titleObject

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/olivander/application_helper.rb', line 19

def page_title
  cf = content_for(:title)
  return cf unless cf.blank?

  controller_key = controller.class.name.underscore
  key = "page_titles.#{controller_key}.#{action_name}"
  return O18n.t(key) if I18n.exists?(key)

  return @page_title if @page_title

  "#{controller_name}: #{action_name}".titleize
end

#render_optional_partial(partial, locals: {}) ⇒ Object

[View source]

135
136
137
138
139
140
# File 'app/helpers/olivander/application_helper.rb', line 135

def render_optional_partial(partial, locals: {})
  render partial: partial, locals: locals
rescue ActionView::MissingTemplate
  Rails.logger.debug "did not find partial: #{partial}"
  nil
end

#resource_attributes(resource, effective_resource) ⇒ Object

[View source]

128
129
130
131
132
133
# File 'app/helpers/olivander/application_helper.rb', line 128

def resource_attributes(resource, effective_resource)
  er_attributes = effective_resource&.model_attributes&.collect { |x| x[0] }
  return er_attributes if er_attributes.present? && er_attributes.size.positive?

  resource.auto_form_attributes
end

#resource_field_group_label(resource_class, key) ⇒ Object

[View source]

115
116
117
118
# File 'app/helpers/olivander/application_helper.rb', line 115

def resource_field_group_label(resource_class, key)
  i18n_key = "activerecord.attributes.#{resource_class.name.underscore}.resource_field_groups.#{key}"
  I18n.exists?(i18n_key) ? O18n.t(i18n_key) : key.to_s.titleize
end

#resource_form_action_icon(resource, action) ⇒ Object

[View source]

105
106
107
108
109
110
111
112
113
# File 'app/helpers/olivander/application_helper.rb', line 105

def resource_form_action_icon(resource, action)
  key = resource.class.name.underscore
  if I18n.exists?("activerecord.actions.#{key}.#{action}-icon")
    return O18n.t("activerecord.actions.#{key}.#{action}-icon")
  end
  return O18n.t("activerecord.actions.#{action}-icon") if I18n.exists?("activerecord.actions.#{action}-icon")

  action.to_s.titleize
end

#resource_form_action_label(resource, action) ⇒ Object

[View source]

97
98
99
100
101
102
103
# File 'app/helpers/olivander/application_helper.rb', line 97

def resource_form_action_label(resource, action)
  key = resource.class.name.underscore
  return O18n.t("activerecord.actions.#{key}.#{action}") if I18n.exists?("activerecord.actions.#{key}.#{action}")
  return O18n.t("activerecord.actions.#{action}") if I18n.exists?("activerecord.actions.#{action}")

  action.to_s.titleize
end

#resource_form_action_tooltip(resource, action) ⇒ Object

[View source]

87
88
89
90
91
92
93
94
95
# File 'app/helpers/olivander/application_helper.rb', line 87

def resource_form_action_tooltip(resource, action)
  key = resource.class.name.underscore
  if I18n.exists?("activerecord.actions.#{key}.#{action}-tooltip")
    return O18n.t("activerecord.actions.#{key}.#{action}-tooltip")
  end
  return O18n.t("activerecord.actions.#{action}-tooltip") if I18n.exists?("activerecord.actions.#{action}-tooltip")

  action.to_s.titleize
end

#resource_form_actions(resource, for_action: :show) ⇒ Object

[View source]

82
83
84
85
# File 'app/helpers/olivander/application_helper.rb', line 82

def resource_form_actions(resource, for_action: :show)
  render partial: 'resource_form_actions',
         locals: { actions: authorized_resource_actions(resource, for_action: for_action).select(&:show_in_form) }
end
[View source]

170
171
172
173
174
# File 'app/helpers/olivander/application_helper.rb', line 170

def sidebar_background_class
  Olivander::CurrentContext.application_context.sidebar_background_class ||
    ENV['SIDEBAR_BACKGROUND_CLASS'] ||
    (is_dev_environment? ? 'bg-danger' : 'bg-info')
end
[View source]

154
155
156
157
158
# File 'app/helpers/olivander/application_helper.rb', line 154

def sidebar_context_name
  [Olivander::CurrentContext.application_context.name, sidebar_context_suffix&.upcase].reject do |x|
    x.blank? or x == 'PRODUCTION'
  end.join(' ')
end
[View source]

160
161
162
163
164
165
166
167
168
# File 'app/helpers/olivander/application_helper.rb', line 160

def sidebar_context_suffix
  suffix = nil
  suffix ||= ENV['CLIENT_ENVIRONMENT']
  parts = request.host.split('.')
  suffix ||= parts.first.split('-').last.downcase if request.host.include?('-')
  suffix ||= 'local' if %w[localhost test].include?(parts.last.downcase)
  suffix ||= 'production'
  suffix
end

#user_image_path(user) ⇒ Object

[View source]

32
33
34
35
36
# File 'app/helpers/olivander/application_helper.rb', line 32

def user_image_path(user)
  return "avatar#{SecureRandom.random_number(4)}.png" unless user.present? && user.respond_to?(:avatar_path)

  user.avatar_path
end