Class: IronAdmin::Resources::RelatedListComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/iron_admin/resources/related_list_component.rb

Overview

Renders a list of related records for has_many associations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association:, records:, limit: 20) ⇒ RelatedListComponent

Returns a new instance of RelatedListComponent.

Parameters:

  • association (Hash)

    Association config

  • records (ActiveRecord::Relation)

    Records

  • limit (Integer) (defaults to: 20)

    Max records (default: 20)



19
20
21
22
23
# File 'app/components/iron_admin/resources/related_list_component.rb', line 19

def initialize(association:, records:, limit: 20)
  @association = association
  @records = records.limit(limit)
  @limit = limit
end

Instance Attribute Details

#associationHash (readonly)

Returns Association configuration.

Returns:

  • (Hash)

    Association configuration



8
9
10
# File 'app/components/iron_admin/resources/related_list_component.rb', line 8

def association
  @association
end

#limitInteger (readonly)

Returns Max records to show.

Returns:

  • (Integer)

    Max records to show



14
15
16
# File 'app/components/iron_admin/resources/related_list_component.rb', line 14

def limit
  @limit
end

#recordsActiveRecord::Relation (readonly)

Returns Related records.

Returns:

  • (ActiveRecord::Relation)

    Related records



11
12
13
# File 'app/components/iron_admin/resources/related_list_component.rb', line 11

def records
  @records
end

Instance Method Details

#display_methodSymbol, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Display method for record labels.

Returns:

  • (Symbol, Proc, nil)

    Display method for record labels



45
46
47
# File 'app/components/iron_admin/resources/related_list_component.rb', line 45

def display_method
  association[:display]
end

#record_url(record) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns URL to view the record.

Parameters:

  • record (ActiveRecord::Base)

    The record

Returns:

  • (String)

    URL to view the record



58
59
60
# File 'app/components/iron_admin/resources/related_list_component.rb', line 58

def record_url(record)
  helpers.iron_admin.resource_path(resource_name, record)
end

#render?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Whether to render the component.

Returns:

  • (Boolean)

    Whether to render the component



64
65
66
# File 'app/components/iron_admin/resources/related_list_component.rb', line 64

def render?
  records.any?
end

#resource_nameString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Associated resource name.

Returns:

  • (String)

    Associated resource name



39
40
41
# File 'app/components/iron_admin/resources/related_list_component.rb', line 39

def resource_name
  association[:resource].resource_name
end

#themeIronAdmin::Configuration::Theme

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Theme configuration.

Returns:



27
28
29
# File 'app/components/iron_admin/resources/related_list_component.rb', line 27

def theme
  IronAdmin.configuration.theme
end

#titleString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Humanized association title.

Returns:

  • (String)

    Humanized association title



33
34
35
# File 'app/components/iron_admin/resources/related_list_component.rb', line 33

def title
  association[:name].to_s.humanize
end

#view_all_urlString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns URL to view all records.

Returns:

  • (String)

    URL to view all records



51
52
53
# File 'app/components/iron_admin/resources/related_list_component.rb', line 51

def view_all_url
  helpers.iron_admin.resources_path(resource_name)
end