Class: FlexiAdmin::Components::Resources::ListViewComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Helpers::LinkHelper, Helpers::ResourceHelper, Helpers::Selectable, Helpers::ValueFormatter
Defined in:
lib/flexi_admin/components/resources/list_view_component.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::LinkHelper

#navigate_to

Methods included from Helpers::Selectable

#selectable, #selectable?

Methods included from Helpers::ValueFormatter

#as_navigation, #format

Methods included from Helpers::ResourceHelper

#autocomplete_path, #bulk_action_path, #datalist_path, #edit_resource_path, #paginate, #resource__path, #resource_input_name, #resource_path, #resources_path, #scope, #scope_plural, #scope_singular

Constructor Details

#initialize(context) ⇒ ListViewComponent

Returns a new instance of ListViewComponent.



35
36
37
38
39
40
41
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 35

def initialize(context)
  @context = context
  @resources = context.resources
  @columns = []
  @headers = []
  @is_selectable = false
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



33
34
35
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 33

def columns
  @columns
end

#contextObject (readonly)

Returns the value of attribute context.



33
34
35
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 33

def context
  @context
end

#resourceObject (readonly)

Returns the value of attribute resource.



33
34
35
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 33

def resource
  @resource
end

#resourcesObject (readonly)

Returns the value of attribute resources.



33
34
35
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 33

def resources
  @resources
end

Instance Method Details

#as_date(value, format: nil) ⇒ Object



52
53
54
55
56
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 52

def as_date(value, format: nil)
  return if value.blank?

  I18n.l(value, format:)
end

#as_text(value) ⇒ Object



48
49
50
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 48

def as_text(value)
  value.to_s
end

#build_column(attribute, options, &block) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 72

def build_column(attribute, options, &block)
  options[:formatter] = format(options[:as] || :text)

  Column.new(attribute,
            block || proc { |resource| resource.send(attribute) },
            options.presence || {})
end

#column(attribute, options = {}, &block) ⇒ Object



68
69
70
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 68

def column(attribute, options = {}, &block)
  @columns << build_column(attribute, options, &block)
end

#list_viewObject



58
59
60
61
62
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 58

def list_view
  yield

  table
end

#render?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 43

def render?
  context.params.current_view == 'list' ||
    (context.views.first == 'list' && context.params.current_view.blank?)
end

#tableObject



64
65
66
# File 'lib/flexi_admin/components/resources/list_view_component.rb', line 64

def table
  render FlexiAdmin::Components::Resources::ListView::TableComponent.new(headers, columns, resources, context, selectable: true)
end