Module: Olivander::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

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

Returns:

  • (Boolean)


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

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



37
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
# File 'app/helpers/olivander/application_helper.rb', line 37

def authorized_resource_actions(resource, for_action: :show)
  raw_name = resource.is_a?(Class) ? resource.name : resource.class.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)
            else
              if resource.persisted?
                routed_resource.persisted_crud_actions | routed_resource.member_actions.reject(&:crud_action)
              else
                []
              end
            end
  actions = actions.select{ |a| authorized_resource_action?(a.sym, 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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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 = divisor/2
    result = 12/divisor
  end
  "col-#{label}-#{result}"
end

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



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

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



109
110
111
# File 'app/helpers/olivander/application_helper.rb', line 109

def current_ability
  Olivander::CurrentContext.ability
end

#current_userObject



105
106
107
# File 'app/helpers/olivander/application_helper.rb', line 105

def current_user
  Olivander::CurrentContext.user
end


164
165
166
167
# File 'app/helpers/olivander/application_helper.rb', line 164

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



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

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

  sym.to_s.titleize
end

#flash_class(key) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/helpers/olivander/application_helper.rb', line 169

def flash_class key
  case key
  when "error"
    "danger"
  when "notice"
    "info"
  when "alert"
    "danger"
  else
    key
  end
end

#flash_icon(key) ⇒ Object



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

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



160
161
162
# File 'app/helpers/olivander/application_helper.rb', line 160

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

#is_dev_environment?Boolean

Returns:

  • (Boolean)


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

def is_dev_environment?
  sidebar_context_suffix != 'production'
end

#page_titleObject



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

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 I18n.t(key) if I18n.exists?(key)

  return @page_title if @page_title

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

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



120
121
122
123
124
125
126
# File 'app/helpers/olivander/application_helper.rb', line 120

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



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

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



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

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

#resource_form_action_icon(resource, action) ⇒ Object



92
93
94
95
96
97
98
# File 'app/helpers/olivander/application_helper.rb', line 92

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

  action.to_s.titleize
end

#resource_form_action_label(resource, action) ⇒ Object



84
85
86
87
88
89
90
# File 'app/helpers/olivander/application_helper.rb', line 84

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

  action.to_s.titleize
end

#resource_form_action_tooltip(resource, action) ⇒ Object



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

def resource_form_action_tooltip(resource, action)
  key = resource.class.name.underscore
  return I18n.t("activerecord.actions.#{key}.#{action}-tooltip") if I18n.exists?("activerecord.actions.#{key}.#{action}-tooltip")
  return I18n.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



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

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


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

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


140
141
142
# File 'app/helpers/olivander/application_helper.rb', line 140

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


144
145
146
147
148
149
150
151
152
# File 'app/helpers/olivander/application_helper.rb', line 144

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



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

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

  user.avatar_path
end