Class: Jav::TabSwitcherComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
ApplicationHelper, UrlHelpers
Defined in:
app/components/jav/tab_switcher_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #render_license_warning, #render_license_warnings, #root_path_without_url, #svg, #white_panel_classes

Methods included from UrlHelpers

#edit_resource_path, #new_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_path, #resource_view_path, #resources_path

Methods inherited from BaseComponent

#has_with_trial

Constructor Details

#initialize(resource:, group:, current_tab:, active_tab_name:, view:, style:) ⇒ TabSwitcherComponent

Returns a new instance of TabSwitcherComponent.



11
12
13
14
15
16
17
18
19
20
# File 'app/components/jav/tab_switcher_component.rb', line 11

def initialize(resource:, group:, current_tab:, active_tab_name:, view:, style:)
  super
  @active_tab_name = active_tab_name
  @resource = resource
  @group = group
  @current_tab = current_tab
  @tabs = group.items
  @view = view
  @style = style
end

Instance Attribute Details

#active_tab_nameObject (readonly)

Returns the value of attribute active_tab_name.



7
8
9
# File 'app/components/jav/tab_switcher_component.rb', line 7

def active_tab_name
  @active_tab_name
end

#current_tabObject (readonly)

Returns the value of attribute current_tab.



7
8
9
# File 'app/components/jav/tab_switcher_component.rb', line 7

def current_tab
  @current_tab
end

#groupObject (readonly)

Returns the value of attribute group.



7
8
9
# File 'app/components/jav/tab_switcher_component.rb', line 7

def group
  @group
end

#styleObject (readonly)

Returns the value of attribute style.



7
8
9
# File 'app/components/jav/tab_switcher_component.rb', line 7

def style
  @style
end

#tabsObject (readonly)

Returns the value of attribute tabs.



7
8
9
# File 'app/components/jav/tab_switcher_component.rb', line 7

def tabs
  @tabs
end

#viewObject (readonly)

Returns the value of attribute view.



7
8
9
# File 'app/components/jav/tab_switcher_component.rb', line 7

def view
  @view
end

Instance Method Details

#is_edit?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/components/jav/tab_switcher_component.rb', line 32

def is_edit?
  @view.in?(i[edit update])
end

#is_initial_load?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/components/jav/tab_switcher_component.rb', line 40

def is_initial_load?
  params[:active_tab_name].blank?
end

#is_new?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/components/jav/tab_switcher_component.rb', line 36

def is_new?
  @view.in?(i[new create])
end

#selected?(tab) ⇒ Boolean

On initial load we want that each tab button to be the selected one. We do that so we don't get the wrongly selected item for a quick brief when first switching from one panel to another.

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'app/components/jav/tab_switcher_component.rb', line 46

def selected?(tab)
  if is_initial_load?
    current_tab.name.to_s == tab.name.to_s
  else
    tab.name.to_s == active_tab_name.to_s
  end
end

#tab_path(tab) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/components/jav/tab_switcher_component.rb', line 22

def tab_path(tab)
  if is_edit?
    helpers.edit_resource_path(resource: @resource, model: @resource.model, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id)
  elsif is_new?
    helpers.new_resource_path(resource: @resource, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id)
  else
    helpers.resource_path(resource: @resource, model: @resource.model, keep_query_params: true, active_tab_name: tab.name, tab_turbo_frame: group.turbo_frame_id)
  end
end

#visible_itemsObject

Goes through all items and removes the ones that are not supposed to be visible. Example below: tabs do field :comments, as: :has_many end Because the developer hasn't specified that it should be visible on edit views (with the show_on: :edit option), the field should not be visible in the item switcher either.



61
62
63
64
65
66
67
68
69
70
71
# File 'app/components/jav/tab_switcher_component.rb', line 61

def visible_items
  tabs.select do |tab|
    next false if tab.items.blank?
    next false if tab.is_field? && !tab.authorized?
    next false if tab.has_a_single_item? && !single_item_visible?(tab.items.first)
    next false unless tab.visible?
    next false unless tab.visible_on?(view)

    true
  end
end