Module: ActiveAdmin::ViewHelpers::DisplayHelper

Included in:
ActiveAdmin::ViewHelpers
Defined in:
lib/active_admin/view_helpers/display_helper.rb

Constant Summary collapse

DISPLAY_NAME_FALLBACK =
->{
  name, klass = "", self.class
  name << klass.model_name.human         if klass.respond_to? :model_name
  name << " ##{send(klass.primary_key)}" if klass.respond_to? :primary_key
  name.present? ? name : to_s
}

Instance Method Summary collapse

Instance Method Details

#association_methods_for(resource) ⇒ Object


38
39
40
41
# File 'lib/active_admin/view_helpers/display_helper.rb', line 38

def association_methods_for(resource)
  return [] unless resource.class.respond_to? :reflect_on_all_associations
  resource.class.reflect_on_all_associations.map(&:name)
end

#boolean_attr?(resource, attr) ⇒ Boolean

Returns:

  • (Boolean)

84
85
86
87
88
# File 'lib/active_admin/view_helpers/display_helper.rb', line 84

def boolean_attr?(resource, attr)
  if resource.class.respond_to? :columns_hash
    column = resource.class.columns_hash[attr.to_s] and column.type == :boolean
  end
end

#display_name(resource) ⇒ Object

Attempts to call any known display name methods on the resource. See the setting in `application.rb` for the list of methods and their priority.


17
18
19
# File 'lib/active_admin/view_helpers/display_helper.rb', line 17

def display_name(resource)
  render_in_context resource, display_name_method_for(resource) unless resource.nil?
end

#display_name_method_for(resource) ⇒ Object

Looks up and caches the first available display name method. To prevent conflicts, we exclude any methods that happen to be associations. If no methods are available and we're about to use the Kernel's `to_s`, provide our own.


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_admin/view_helpers/display_helper.rb', line 24

def display_name_method_for(resource)
  @@display_name_methods_cache ||= {}
  @@display_name_methods_cache[resource.class] ||= begin
    methods = active_admin_application.display_name_methods - association_methods_for(resource)
    method  = methods.detect{ |method| resource.respond_to? method }

    if method != :to_s || resource.method(method).source_location
      method
    else
      DISPLAY_NAME_FALLBACK
    end
  end
end

#find_value(resource, attr) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_admin/view_helpers/display_helper.rb', line 50

def find_value(resource, attr)
  if attr.is_a? Proc
    attr.call resource
  elsif attr =~ /\A(.+)_id\z/ && reflection_for(resource, $1.to_sym)
    resource.public_send $1
  elsif resource.respond_to? attr
    resource.public_send attr
  elsif resource.respond_to? :[]
    resource[attr]
  end
end

#format_attribute(resource, attr) ⇒ Object


43
44
45
46
47
48
# File 'lib/active_admin/view_helpers/display_helper.rb', line 43

def format_attribute(resource, attr)
  value = find_value resource, attr
  value = pretty_format value                    if attr.is_a? Symbol
  value = Arbre::Context.new{ status_tag value } if boolean_attr? resource, attr
  value
end

#pretty_format(object) ⇒ Object

Attempts to create a human-readable string for any object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_admin/view_helpers/display_helper.rb', line 63

def pretty_format(object)
  case object
  when String, Numeric, Symbol, Arbre::Element
    object.to_s
  when Date, Time
    localize object, format: active_admin_application.localize_format
  else
    if defined?(::ActiveRecord) && object.is_a?(ActiveRecord::Base) ||
       defined?(::Mongoid)      && object.class.include?(Mongoid::Document)
      auto_link object
    else
      display_name object
    end
  end
end

#reflection_for(resource, method) ⇒ Object


79
80
81
82
# File 'lib/active_admin/view_helpers/display_helper.rb', line 79

def reflection_for(resource, method)
  klass = resource.class
  klass.reflect_on_association method if klass.respond_to? :reflect_on_association
end