Class: IronAdmin::Ui::ScopesComponent

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

Overview

Renders scope tabs for filtering resource lists.

Examples:

Scopes from resource

render IronAdmin::Ui::ScopesComponent.new(
  scopes: @resource_class.all_scopes,
  current_scope: @current_scope,
  base_path: resources_path(@resource_class.resource_name)
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scopes:, current_scope:, base_path:, params: {}) ⇒ ScopesComponent

Returns a new instance of ScopesComponent.

Parameters:

  • scopes (Array<Hash>)

    Scope definitions

  • current_scope (String, nil)

    Active scope name

  • base_path (String)

    Base URL

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

    Additional query params



30
31
32
33
34
35
# File 'app/components/iron_admin/ui/scopes_component.rb', line 30

def initialize(scopes:, current_scope:, base_path:, params: {})
  @scopes = scopes
  @current_scope = current_scope
  @base_path = base_path
  @params = params
end

Instance Attribute Details

#base_pathString (readonly)

Returns Base URL path.

Returns:

  • (String)

    Base URL path



21
22
23
# File 'app/components/iron_admin/ui/scopes_component.rb', line 21

def base_path
  @base_path
end

#current_scopeString? (readonly)

Returns Currently active scope name.

Returns:

  • (String, nil)

    Currently active scope name



18
19
20
# File 'app/components/iron_admin/ui/scopes_component.rb', line 18

def current_scope
  @current_scope
end

#paramsHash (readonly)

Returns Additional query params.

Returns:

  • (Hash)

    Additional query params



24
25
26
# File 'app/components/iron_admin/ui/scopes_component.rb', line 24

def params
  @params
end

#scopesArray<Hash> (readonly)

Returns Scope definitions.

Returns:

  • (Array<Hash>)

    Scope definitions



15
16
17
# File 'app/components/iron_admin/ui/scopes_component.rb', line 15

def scopes
  @scopes
end

Instance Method Details

#active?(scope) ⇒ 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 scope is currently active.

Parameters:

  • scope (Hash)

    Scope definition

Returns:

  • (Boolean)

    Whether scope is currently active



53
54
55
# File 'app/components/iron_admin/ui/scopes_component.rb', line 53

def active?(scope)
  current_scope == scope[:name].to_s
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



71
72
73
# File 'app/components/iron_admin/ui/scopes_component.rb', line 71

def render?
  scopes.any?
end

#scope_classes(scope) ⇒ 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 CSS classes for scope tab.

Parameters:

  • scope (Hash)

    Scope definition

Returns:

  • (String)

    CSS classes for scope tab



60
61
62
63
64
65
66
67
# File 'app/components/iron_admin/ui/scopes_component.rb', line 60

def scope_classes(scope)
  base = "px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors duration-150"
  if active?(scope)
    "#{base} #{theme.scope_active}"
  else
    "#{base} #{theme.scope_inactive}"
  end
end

#scope_url(scope_name) ⇒ 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 with scope parameter.

Parameters:

  • scope_name (Symbol, String)

    Scope name

Returns:

  • (String)

    URL with scope parameter



46
47
48
# File 'app/components/iron_admin/ui/scopes_component.rb', line 46

def scope_url(scope_name)
  "#{base_path}?#{params.merge(scope: scope_name).to_query}"
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:



39
40
41
# File 'app/components/iron_admin/ui/scopes_component.rb', line 39

def theme
  IronAdmin.configuration.theme
end