Class: Avo::Views::ResourceIndexComponent

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

Instance Method Summary collapse

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #empty_state, #get_model_class, #input_classes, #render_license_warning, #render_license_warnings, #svg, #turbo_frame_wrap

Methods included from ResourcesHelper

#edit_field_wrapper, #filter_wrapper, #index_field_wrapper, #item_select_all_input, #item_selector_init, #item_selector_input, #resource_grid, #resource_table, #show_field_wrapper

Constructor Details

#initialize(resource: nil, resources: nil, models: [], pagy: nil, index_params: {}, filters: [], actions: [], reflection: nil, turbo_frame: "", parent_model: 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
# File 'app/components/avo/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
)
  @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
end

Instance Method Details

#attach_pathObject



104
105
106
# File 'app/components/avo/views/resource_index_component.rb', line 104

def attach_path
  "#{Avo::App.root_path}#{request.env["PATH_INFO"]}/new"
end

#available_view_typesObject



43
44
45
# File 'app/components/avo/views/resource_index_component.rb', line 43

def available_view_types
  @index_params[:available_view_types]
end

#can_attach?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'app/components/avo/views/resource_index_component.rb', line 53

def can_attach?
  klass = @reflection
  klass = @reflection.through_reflection if klass.is_a? ::ActiveRecord::Reflection::ThroughReflection

  @reflection.present? && klass.is_a?(::ActiveRecord::Reflection::HasManyReflection) && !has_reflection_and_is_read_only && 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)


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

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

#create_pathObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/components/avo/views/resource_index_component.rb', line 75

def create_path
  args = {}

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

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

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

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

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

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

#has_reflection_and_is_read_onlyObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/avo/views/resource_index_component.rb', line 60

def has_reflection_and_is_read_only
  if @reflection.present? && @reflection.active_record.name && @reflection.name
    fields = ::Avo::App.get_resource_by_model_name(@reflection.active_record.name).get_field_definitions
    filtered_fields = fields.filter { |f| f.id == @reflection.name }
  else
    return false
  end

  if filtered_fields.present?
    filtered_fields.find { |f| f.id == @reflection.name }.readonly
  else
    false
  end
end

#singular_resource_nameObject



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

def singular_resource_name
  if @reflection.present?
    ::Avo::App.get_resource_by_model_name(@reflection.class_name).name
  else
    @resource.singular_name || @resource.model_class.model_name.name.downcase
  end
end

#titleObject



31
32
33
34
35
36
37
# File 'app/components/avo/views/resource_index_component.rb', line 31

def title
  if @reflection.present?
    ::Avo::App.get_resource_by_model_name(@reflection.class_name).plural_name
  else
    @resource.plural_name
  end
end

#view_typeObject



39
40
41
# File 'app/components/avo/views/resource_index_component.rb', line 39

def view_type
  @index_params[:view_type]
end