Module: Admin::ViewHelper

Defined in:
app/helpers/admin/view_helper.rb

Constant Summary collapse

ADMIN_PILL_COLORS =
HashWithIndifferentAccess.new({
  green: "3BAB46",
  red: "DA3946",
  blue: "3497BE",
  black: "343242",
  grey: "777777",
})

Instance Method Summary collapse

Instance Method Details

#admin_template_exists?(template_path) ⇒ Boolean

Returns true if a partial is available at "admin/#{template_path}"

Returns:

  • (Boolean)


37
38
39
# File 'app/helpers/admin/view_helper.rb', line 37

def admin_template_exists?(template_path)
  lookup_context.template_exists?("admin/#{template_path}", [], true)
end

#contextual_form_urlObject

Returns the correct value to pass to the url: of form_for, based on the current controller.action_name



58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/admin/view_helper.rb', line 58

def contextual_form_url
  case controller.action_name
  when "edit"
    url_for(action:"update", id:@resource.id)
  when "new"
    url_for(action:"create")
  else
    nil
  end
end

#deletion_warning(resource) ⇒ Object

Returns a deletion warning message for the given ActiveRecord instance



52
53
54
# File 'app/helpers/admin/view_helper.rb', line 52

def deletion_warning(resource)
  return %{Are you sure you want to delete the #{@managed_class.navigation_label.singularize.downcase}#{Tolaria.display_name(resource)}”? This action is not reversible.}
end

#fontawesome_icon(icon = "", options = {}) ⇒ Object

Returns an <i> tag that displays the Font Awesome icon for the given icon. Names much match those from the Font Awesome site.



22
23
24
25
26
27
28
# File 'app/helpers/admin/view_helper.rb', line 22

def fontawesome_icon(icon = "", options = {})
  icon = icon.to_s.parameterize.tr("_", "-")
   :i, nil, options.reverse_merge({
    :class => "icon icon-#{icon}",
    :"aria-hidden" => true,
  })
end

#gravatar_for(email) ⇒ Object

Returns a URI to a Gravatar for the given email



31
32
33
34
# File 'app/helpers/admin/view_helper.rb', line 31

def gravatar_for(email)
  digest = Digest::MD5.hexdigest(email)
  return "https://secure.gravatar.com/avatar/#{digest}?d=retro&s=36"
end

#pill(label, color: :black) ⇒ Object

Returns a <span> tag that displays the given label as a pill status badge. You can change the color of the pill by providing a six-digit hexadecimal color string, or passing one of the predefined color names: :green, :red, :blue, :black, :grey.



73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/admin/view_helper.rb', line 73

def pill(label, color: :black)

  if ADMIN_PILL_COLORS[color].present?
    color = ADMIN_PILL_COLORS[color]
  else
    color = color.to_s.delete("#")
  end

   :span, label.to_s, class:"pill", style:"background-color:##{color}"

end

#ransack_text_search_chain(model) ⇒ Object

Attempt to automatically construct a default text search field name for Ransack from a given model's table settings



43
44
45
46
47
48
49
# File 'app/helpers/admin/view_helper.rb', line 43

def ransack_text_search_chain(model)
  textual_columns = model.columns_hash.select do |column, settings|
    settings.sql_type.include?("char") || settings.sql_type.include?("text")
  end
  textual_columns = {id:"id"} if textual_columns.none?
  return %{#{textual_columns.keys.join("_or_")}_cont}.to_sym
end

Returns a navigation menu link with the given label, Font Awesome icon name, and URI



13
14
15
16
17
18
# File 'app/helpers/admin/view_helper.rb', line 13

def tolaria_navigation_link(label, icon, index_path, options = {})
  options[:class] = "#{options[:class]} current" if index_path.in?(url_for)
  link_to index_path, options do
    fontawesome_icon(icon) << " #{label}"
  end
end