Class: Jav::Views::ResourceIndexComponent

Inherits:
ResourceComponent
  • Object
show all
Includes:
ApplicationHelper, ResourcesHelper
Defined in:
app/components/jav/views/resource_index_component.rb

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 ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_select_all_input, #item_selector_init, #item_selector_input, #resource_grid, #resource_table

Constructor Details

#initialize(resource: nil, resources: nil, models: [], pagy: nil, index_params: {}, filters: [], actions: [], reflection: nil, turbo_frame: "", parent_model: nil, parent_resource: nil, applied_filters: [], query: nil) ⇒ ResourceIndexComponent

Returns a new instance of ResourceIndexComponent.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/jav/views/resource_index_component.rb', line 7

def initialize(
  resource: nil,
  resources: nil,
  models: [],
  pagy: nil,
  index_params: {},
  filters: [],
  actions: [],
  reflection: nil,
  turbo_frame: "",
  parent_model: nil,
  parent_resource: nil,
  applied_filters: [],
  query: nil
)
  super
  @resource = resource
  @resources = resources
  @models = models
  @pagy = pagy
  @index_params = index_params
  @filters = filters
  @actions = actions
  @reflection = reflection
  @turbo_frame = turbo_frame
  @parent_model = parent_model
  @parent_resource = parent_resource
  @applied_filters = applied_filters
  @view = :index
  @query = query
end

Instance Method Details

#attach_pathObject



102
103
104
105
106
# File 'app/components/jav/views/resource_index_component.rb', line 102

def attach_path
  current_path = CGI.unescape(request.env["PATH_INFO"]).split("/").select(&:present?)

  Jav::App.root_path(paths: [*current_path, "new"])
end

#authorized_to_search?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
# File 'app/components/jav/views/resource_index_component.rb', line 137

def authorized_to_search?
  # Hide the search if the authorization prevents it
  return true unless @resource.authorization.has_action_method?("search")

  @resource.authorization.authorize_action("search", raise_exception: false)
end

#available_view_typesObject



53
54
55
# File 'app/components/jav/views/resource_index_component.rb', line 53

def available_view_types
  @index_params[:available_view_types]
end

#can_attach?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/components/jav/views/resource_index_component.rb', line 65

def can_attach?
  return false if has_reflection_and_is_read_only

  reflection_class = if @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
                       @reflection.through_reflection.class
                     else
                       @reflection.class
                     end

  return false unless reflection_class.in? [
    ActiveRecord::Reflection::HasManyReflection,
    ActiveRecord::Reflection::HasAndBelongsToManyReflection
  ]

  authorize_association_for(:attach)
end

#can_see_the_create_button?Boolean

The Create button is dependent on the new? policy method. The create? should be called only when the user clicks the Save button so the developers gets access to the params from the form.

Returns:

  • (Boolean)


59
60
61
62
63
# File 'app/components/jav/views/resource_index_component.rb', line 59

def can_see_the_create_button?
  return authorize_association_for(:create) if @reflection.present?

  @resource.authorization.authorize_action(:new, raise_exception: false) && !has_reflection_and_is_read_only
end

#create_pathObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/components/jav/views/resource_index_component.rb', line 82

def create_path
  args = {}

  if @reflection.present?
    args = {
      via_relation_class: reflection_model_class,
      via_resource_id: @parent_model.to_param
    }

    args[:active_tab_name] = params[:active_tab_name] if params[:active_tab_name].present?
    args[:via_relation] = params[:resource_name] if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection

    args[:via_relation] = @reflection.name if @reflection.is_a? ActiveRecord::Reflection::HasManyReflection

    args[:via_relation] = @reflection.inverse_of.name if @reflection.inverse_of.present?
  end

  helpers.new_resource_path(resource: @resource, **args)
end

#descriptionObject



118
119
120
121
122
123
124
125
126
127
# File 'app/components/jav/views/resource_index_component.rb', line 118

def description
  # If this is a has many association, the user can pass a description to be shown just for this association.
  if @reflection.present?
    return field.description if field.present? && field.description

    return
  end

  @resource.resource_description
end

#show_search_inputObject



129
130
131
132
133
134
135
# File 'app/components/jav/views/resource_index_component.rb', line 129

def show_search_input
  return false unless authorized_to_search?
  return false if @resource.search_query.blank?
  return false if field&.hide_search_input

  true
end

#singular_resource_nameObject



108
109
110
111
112
113
114
115
116
# File 'app/components/jav/views/resource_index_component.rb', line 108

def singular_resource_name
  if @reflection.present?
    return name.singularize if field.present?

    reflection_resource.name
  else
    @resource.singular_name || @resource.model_class.model_name.name.downcase
  end
end

#titleObject



39
40
41
42
43
44
45
46
47
# File 'app/components/jav/views/resource_index_component.rb', line 39

def title
  if @reflection.present?
    return name if field.present?

    reflection_resource.plural_name
  else
    @resource.plural_name
  end
end

#view_typeObject



49
50
51
# File 'app/components/jav/views/resource_index_component.rb', line 49

def view_type
  @index_params[:view_type]
end