Skip to content

Commit 3afd2fe

Browse files
authored
refactor: access view_type from resource (#3814)
* refactor: access `view_type` from resource * wip * wip
1 parent 4dfa3be commit 3afd2fe

File tree

4 files changed

+40
-48
lines changed

4 files changed

+40
-48
lines changed

app/components/avo/views/resource_index_component.html.erb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<%= content_tag :div,
2-
class: class_names("index-#{@index_params[:view_type]}-view", {"has-record-selector": @resource.record_selector}),
2+
class: class_names("index-#{@resource.view_type}-view", {"has-record-selector": @resource.record_selector}),
33
data: {
44
component_name: self.class.to_s.underscore,
55
model_name: @resource.model_name.to_s,
@@ -48,42 +48,42 @@
4848

4949
<%= render Avo::FiltersComponent.new filters: @filters, resource: @resource, applied_filters: @applied_filters, parent_record: @parent_record %>
5050

51-
<%= render partial: "avo/partials/view_toggle_button", locals: { available_view_types: available_view_types, view_type: view_type, turbo_frame: @turbo_frame } %>
51+
<%= render partial: "avo/partials/view_toggle_button", locals: { available_view_types: @resource.available_view_types, view_type: @resource.view_type, turbo_frame: @turbo_frame } %>
5252
</div>
5353
</div>
5454
<% if has_dynamic_filters? %>
5555
<%= render Avo::DynamicFilters::FiltersComponent.new resource: resource, turbo_frame: @turbo_frame, dynamic_filters_component_id: dynamic_filters_component_id, parent_record: @parent_record, parent_resource: @parent_resource %>
5656
<% end %>
5757
</div>
5858
</div>
59-
<% if view_type.to_sym == :table %>
59+
<% if @resource.view_type.to_sym == :table %>
6060
<% if @resources.present? %>
6161
<div class="w-full relative flex-1 flex mt-0">
6262
<%= render(@resource.resolve_component(Avo::Index::ResourceTableComponent).new(resources: @resources, resource: @resource, reflection: @reflection, parent_record: @parent_record, parent_resource: @parent_resource, pagy: @pagy, query: @query, actions: @actions)) %>
6363
</div>
6464
<% else %>
65-
<%= helpers.empty_state by_association: params[:related_name].present?, view_type: view_type, add_background: true %>
65+
<%= helpers.empty_state by_association: params[:related_name].present?, view_type: @resource.view_type, add_background: true %>
6666
<% end %>
6767
<% end %>
6868
<% end %>
6969
<% c.with_bare_content do %>
70-
<% if view_type.to_sym == :map %>
70+
<% if @resource.view_type.to_sym == :map %>
7171
<% if @resources.present? %>
7272
<div>
7373
<%= render(@resource.resolve_component(Avo::Index::ResourceMapComponent).new(resources: @resources, resource: @resource, reflection: @reflection, parent_record: @parent_record, parent_resource: @parent_resource, pagy: @pagy, query: @query)) %>
7474
</div>
7575
<% else %>
76-
<%= helpers.empty_state by_association: params[:related_name].present?, view_type: view_type, add_background: true %>
76+
<%= helpers.empty_state by_association: params[:related_name].present?, view_type: @resource.view_type, add_background: true %>
7777
<% end %>
7878
<% end %>
79-
<% if view_type.to_sym == :table || view_type.to_sym == :map %>
79+
<% if @resource.view_type.to_sym == :table || @resource.view_type.to_sym == :map %>
8080
<% if @records.present? %>
8181
<div class="mt-4 w-full">
8282
<%= render Avo::PaginatorComponent.new pagy: @pagy, turbo_frame: @turbo_frame, index_params: @index_params, resource: @resource, parent_record: @parent_record, parent_resource: @parent_resource, discreet_pagination: field&.discreet_pagination %>
8383
</div>
8484
<% end %>
8585
<% end %>
86-
<% if view_type.to_sym == :grid %>
86+
<% if @resource.view_type.to_sym == :grid %>
8787
<div id="records_panel">
8888
<%= render Avo::Index::ResourceGridComponent.new(resources: @resources, resource: @resource, reflection: @reflection, parent_record: @parent_record, parent_resource: @parent_resource, actions: @actions) %>
8989
</div>

app/components/avo/views/resource_index_component.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ def title
2929
end
3030
end
3131

32-
def view_type
33-
@index_params[:view_type]
34-
end
35-
36-
def available_view_types
37-
@index_params[:available_view_types]
38-
end
39-
4032
# The Create button is dependent on the new? policy method.
4133
# The create? should be called only when the user clicks the Save button so the developers gets access to the params from the form.
4234
def can_see_the_create_button?
@@ -219,7 +211,7 @@ def filters_present?
219211
end
220212

221213
def has_many_view_types?
222-
available_view_types.count > 1
214+
@resource.available_view_types.count > 1
223215
end
224216

225217
# Generate a unique component id for the current component.

app/controllers/avo/base_controller.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,24 +331,8 @@ def set_index_params
331331

332332
@index_params[:sort_direction] = params[:sort_direction] || @resource.default_sort_direction
333333

334-
# View types
335-
available_view_types = @resource.available_view_types
336-
@index_params[:available_view_types] = available_view_types
337-
338-
@index_params[:view_type] = if params[:view_type].present?
339-
params[:view_type]
340-
elsif available_view_types.size == 1
341-
available_view_types.first
342-
else
343-
Avo::ExecutionContext.new(
344-
target: @resource.default_view_type || Avo.configuration.default_view_type,
345-
resource: @resource,
346-
view: @view
347-
).handle
348-
end
349-
350-
if available_view_types.exclude? @index_params[:view_type].to_sym
351-
raise "View type '#{@index_params[:view_type]}' is unavailable for #{@resource.class}."
334+
if @resource.available_view_types.exclude? @resource.view_type.to_sym
335+
raise "View type '#{@resource.view_type}' is unavailable for #{@resource.class}."
352336
end
353337
end
354338

lib/avo/resources/base.rb

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -432,22 +432,24 @@ def fetch_record_title
432432
end
433433

434434
def available_view_types
435-
if self.class.view_types.present?
436-
return Array(
437-
Avo::ExecutionContext.new(
438-
target: self.class.view_types,
439-
resource: self,
440-
record: record
441-
).handle
442-
)
443-
end
435+
@available_view_types ||= begin
436+
if self.class.view_types.present?
437+
return Array(
438+
Avo::ExecutionContext.new(
439+
target: self.class.view_types,
440+
resource: self,
441+
record: record
442+
).handle
443+
)
444+
end
444445

445-
view_types = [:table]
446+
view_types = [:table]
446447

447-
view_types << :grid if self.class.grid_view.present?
448-
view_types << :map if map_view.present?
448+
view_types << :grid if self.class.grid_view.present?
449+
view_types << :map if map_view.present?
449450

450-
view_types
451+
view_types
452+
end
451453
end
452454

453455
def attachment_fields
@@ -675,6 +677,20 @@ def sort_by_param
675677

676678
def sorting_supported? = true
677679

680+
def view_type
681+
@view_type ||= if @params[:view_type].present?
682+
@params[:view_type]
683+
elsif available_view_types.size == 1
684+
available_view_types.first
685+
else
686+
Avo::ExecutionContext.new(
687+
target: default_view_type || Avo.configuration.default_view_type,
688+
resource: self,
689+
view: @view
690+
).handle
691+
end
692+
end
693+
678694
private
679695

680696
def flatten_keys(array)

0 commit comments

Comments
 (0)