Module: IronAdmin::ApplicationHelper

Includes:
Heroicon::ApplicationHelper, FieldDisplayHelper, ThemeHelper
Included in:
Dashboards::RecentTableComponent
Defined in:
app/helpers/iron_admin/application_helper.rb

Overview

Main application helper for IronAdmin views.

Constant Summary collapse

DISPLAY_METHODS =

Methods checked when displaying a record's label.

Returns:

  • (Array<Symbol>)
%i[name title email label slug].freeze
INDEX_TRUNCATION_LENGTH =

Default max characters for text truncation on index pages.

50
TRUNCATABLE_TYPES =

Field types that should be truncated on index pages.

%i[text textarea].freeze
INDEX_COMPACT_TYPES =

Field types that show compact summaries on index pages.

%i[key_value boolean_group].freeze
FIELD_DISPLAY_METHODS =

Maps field types to their display method names.

Returns:

  • (Hash{Symbol => Symbol})
{
  belongs_to: :display_belongs_to,
  badge: :display_badge,
  password: :display_password,
  file: :display_file,
  files: :display_files,
  rich_text: :display_rich_text,
  tags: :display_tags,
  markdown: :display_markdown,
  url: :display_url,
  email: :display_email,
  color: :display_color,
  currency: :display_currency,
  boolean: :display_boolean,
  date: :display_date,
  datetime: :display_datetime,
  polymorphic_belongs_to: :display_polymorphic_belongs_to,
  hidden: :display_hidden,
  radio: :display_radio,
  code: :display_code,
  progress_bar: :display_progress_bar,
  external_image: :display_external_image,
  boolean_group: :display_boolean_group,
  key_value: :display_key_value,
}.freeze

Instance Method Summary collapse

Methods included from ThemeHelper

#cp_badge_count, #cp_body_text, #cp_btn_danger, #cp_btn_filter_apply, #cp_btn_ghost, #cp_btn_primary, #cp_btn_primary_lg, #cp_btn_secondary, #cp_card, #cp_checkbox_class, #cp_file_input_class, #cp_filter_input_class, #cp_focus, #cp_heading, #cp_input_class, #cp_label_text, #cp_link, #cp_link_muted, #cp_muted_text, #cp_navbar_search_class, #cp_scope_active, #cp_scope_inactive, #cp_search_class, #cp_select_class, #cp_sidebar_bg, #cp_sidebar_group_label, #cp_sidebar_link, #cp_sidebar_title, #cp_table_border, #cp_table_header_bg, #cp_table_row_hover, #cp_textarea_class, #t

Instance Method Details

#association_resource_for(field, association_class) ⇒ Object



133
134
135
136
137
138
139
# File 'app/helpers/iron_admin/application_helper.rb', line 133

def association_resource_for(field, association_class)
  resource = field.options[:resource]
  return resource.constantize if resource.is_a?(String)
  return resource if resource

  IronAdmin::ResourceRegistry.find(association_class.model_name.plural)
end

#display_field_value(record, field) ⇒ String?

Displays a field value with appropriate formatting.

Parameters:

  • record (ActiveRecord::Base)

    The record

  • field (IronAdmin::Field)

    Field configuration

Returns:

  • (String, nil)

    Formatted value



56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/iron_admin/application_helper.rb', line 56

def display_field_value(record, field)
  method_name = FIELD_DISPLAY_METHODS[field.type]
  if method_name
    method_name == :display_password ? display_password : send(method_name, record, field)
  elsif (custom = IronAdmin::FieldTypeRegistry.find(field.type))
    custom.render_display(record, field)
  else
    record.public_send(field.name)
  end
end

#display_index_field_value(record, field) ⇒ String?

Displays a field value for index pages with text truncation.

Parameters:

  • record (ActiveRecord::Base)

    The record

  • field (IronAdmin::Field)

    Field configuration

Returns:

  • (String, nil)

    Formatted value (truncated for text fields)



72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/iron_admin/application_helper.rb', line 72

def display_index_field_value(record, field)
  if TRUNCATABLE_TYPES.include?(field.type)
    display_truncated_index_value(record, field)
  elsif INDEX_COMPACT_TYPES.include?(field.type)
    display_compact_field_value(record, field)
  elsif (custom = IronAdmin::FieldTypeRegistry.find(field.type))
    custom.render_index_display(record, field)
  else
    display_field_value(record, field)
  end
end

#display_record_label(record, display_method = nil) ⇒ String

Returns a display label for a record.

Parameters:

  • record (ActiveRecord::Base)

    The record

  • display_method (Symbol, Proc, nil) (defaults to: nil)

    Custom display method

Returns:

  • (String)

    Display label



89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/iron_admin/application_helper.rb', line 89

def display_record_label(record, display_method = nil)
  return display_method.call(record) if display_method.is_a?(Proc)

  return record.public_send(display_method) if display_method.is_a?(Symbol) || display_method.is_a?(String)

  DISPLAY_METHODS.each do |method|
    return record.public_send(method) if record.respond_to?(method) && record.public_send(method).present?
  end

  "#{record.class.model_name.human} ##{record.id}"
end

#filter_options_for(resource_class, filter) ⇒ Array<Array(String, String)>

Returns options for a filter select dropdown.

Parameters:

  • resource_class (Class)

    The resource class

  • filter (Hash)

    Filter configuration

Returns:

  • (Array<Array(String, String)>)

    Options for select tag



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/iron_admin/application_helper.rb', line 106

def filter_options_for(resource_class, filter)
  resource_adapter = resource_class.adapter
  column_name = filter[:name].to_s

  case filter[:type]
  when :select
    enums = resource_adapter.enums
    if enums.key?(column_name)
      enums[column_name].keys.map { |k| [k.humanize, k] }
    elsif filter[:options]
      normalize_filter_options(filter[:options])
    else
      resource_adapter.distinct_values(column_name).map { |v| [v.to_s.humanize, v] }
    end
  when :boolean
    [[I18n.t("iron_admin.filters.true"), "true"], [I18n.t("iron_admin.filters.false"), "false"]]
  else
    []
  end
end

#normalize_filter_options(options) ⇒ Object



127
128
129
130
131
# File 'app/helpers/iron_admin/application_helper.rb', line 127

def normalize_filter_options(options)
  options.map do |option|
    option.is_a?(Array) ? option : [option.to_s.humanize, option]
  end
end