Class: ActiveAdmin::Views::StatusTag

Inherits:
Component
  • Object
show all
Defined in:
lib/active_admin/views/components/status_tag.rb

Overview

Build a StatusTag

Instance Method Summary collapse

Instance Method Details

#status_tag(status, type = nil, options = {}) ⇒ ActiveAdmin::Views::StatusTag

Examples:

status_tag('In Progress')
# => <span class='status_tag in_progress'>In Progress</span>
status_tag('active', :ok)
# => <span class='status_tag active ok'>Active</span>
status_tag('active', :ok, class: 'important', id: 'status_123', label: 'on')
# => <span class='status_tag active ok important' id='status_123'>on</span>

Parameters:

  • status (String)

    the status to display. One of the span classes will be an underscored version of the status.

  • type (Symbol) (defaults to: nil)

    type of status. Will become a class of the span. ActiveAdmin provide style for :ok, :warning and :error.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :class (String)

    to override the default class

  • :id (String)

    to override the default id

  • :label (String)

    to override the default label

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_admin/views/components/status_tag.rb', line 36

def build(*args)
  options = args.extract_options!
  status = args[0]
  type = args[1]
  label = options.delete(:label)
  classes = options.delete(:class)
  status = convert_to_boolean_status(status)

  if status
    content = label || if s = status.to_s and s.present?
      I18n.t "active_admin.status_tag.#{s.downcase}", default: s.titleize
    end
  end

  super(content, options)

  add_class(status_to_class(status)) if status
  add_class(type.to_s) if type
  add_class(classes) if classes
end

#convert_to_boolean_status(status) ⇒ Object (protected)



59
60
61
62
63
64
65
66
67
68
# File 'lib/active_admin/views/components/status_tag.rb', line 59

def convert_to_boolean_status(status)
  case status
  when true, 'true'
    'Yes'
  when false, 'false', nil
    'No'
  else
    status
  end
end

#default_class_nameObject



11
12
13
# File 'lib/active_admin/views/components/status_tag.rb', line 11

def default_class_name
  'status_tag'
end

#status_to_class(status) ⇒ Object (protected)



70
71
72
73
74
75
76
77
# File 'lib/active_admin/views/components/status_tag.rb', line 70

def status_to_class(status)
  case status
  when String, Symbol
    status.to_s.titleize.gsub(/\s/, '').underscore
  else
    ''
  end
end

#tag_nameObject



7
8
9
# File 'lib/active_admin/views/components/status_tag.rb', line 7

def tag_name
  'span'
end