Class: Avo::BaseController

Inherits:
ApplicationController show all
Includes:
Concerns::FiltersSessionHandler
Defined in:
app/controllers/avo/base_controller.rb

Instance Method Summary collapse

Methods included from Concerns::FiltersSessionHandler

#cache_resource_filters?, #fetch_filters, #filters_from_params, #filters_from_session, #filters_session_key, #reset_filters, #save_filters_to_session

Methods inherited from ApplicationController

#exception_logger, #hello, #turbo_frame_request?

Methods included from Concerns::Breadcrumbs

#add_breadcrumb, #avo_breadcrumbs

Methods included from UrlHelpers

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

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #card_classes, #chart_color, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #render_license_warning, #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

Methods included from InitializesAvo

#_current_user, #context, #init_app

Instance Method Details

#createObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/avo/base_controller.rb', line 114

def create
  # This means that the record has been created through another parent record and we need to attach it somehow.
  if params[:via_record_id].present? && params[:via_belongs_to_resource_class].nil?
    @reflection = @record._reflections[params[:via_relation]]
    # Figure out what kind of association does the record have with the parent record

    # Fills in the required info for belongs_to and has_many
    # Get the foreign key and set it to the id we received in the params
    if @reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection) || @reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
      related_resource = Avo.resource_manager.get_resource_by_model_class params[:via_relation_class]
      related_record = related_resource.find_record params[:via_record_id], params: params

      @record.send(:"#{@reflection.foreign_key}=", related_record.id)
    end

    # For when working with has_one, has_one_through, has_many_through, has_and_belongs_to_many, polymorphic
    if @reflection.is_a? ActiveRecord::Reflection::ThroughReflection
      # find the record
      via_resource = Avo.resource_manager.get_resource_by_model_class(params[:via_relation_class])
      @related_record = via_resource.find_record params[:via_record_id], params: params
      association_name = BaseResource.valid_association_name(@record, params[:via_relation])

      if params[:via_association_type] == "has_one"
        # On has_one scenarios we should switch the @record and @related_record
        @related_record.send(:"#{@reflection.parent_reflection.inverse_of.name}=", @record)
      else
        @record.send(association_name) << @related_record
      end
    end
  end

  # record gets instantiated and filled in the fill_record method
  saved = save_record
  @resource.hydrate(record: @record, view: :new, user: _current_user)

  add_breadcrumb @resource.plural_name.humanize, resources_path(resource: @resource)
  add_breadcrumb t("avo.new").humanize
  set_actions

  set_component_for :edit

  if saved
    create_success_action
  else
    create_fail_action
  end
end

#destroyObject



183
184
185
186
187
188
189
# File 'app/controllers/avo/base_controller.rb', line 183

def destroy
  if destroy_model
    destroy_success_action
  else
    destroy_fail_action
  end
end

#editObject



162
163
164
165
166
# File 'app/controllers/avo/base_controller.rb', line 162

def edit
  set_actions

  set_component_for __method__
end

#indexObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/avo/base_controller.rb', line 19

def index
  @page_title = @resource.plural_name.humanize
  add_breadcrumb @resource.plural_name.humanize

  set_index_params
  set_filters
  set_actions

  # If we don't get a query object predefined from a child controller like associations, just spin one up
  unless defined? @query
    @query = @resource.class.query_scope
  end

  # Eager load the associations
  if @resource.includes.present?
    @query = @query.includes(*@resource.includes)
  end

  apply_sorting

  # Apply filters to the current query
  filters_to_be_applied.each do |filter_class, filter_value|
    @query = filter_class.safe_constantize.new(
      arguments: @resource.get_filter_arguments(filter_class)
    ).apply_query request, @query, filter_value
  end

  safe_call :set_and_apply_scopes
  safe_call :apply_dynamic_filters
  apply_pagination

  # Create resources for each record
  # Duplicate the @resource before hydration to avoid @resource keeping last record.
  @resource.hydrate(params: params)
  @resources = @records.map do |record|
    @resource.dup.hydrate(record: record)
  end

  set_component_for __method__
end

#newObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/avo/base_controller.rb', line 85

def new
  # Record is already hydrated on set_record_to_fill method
  @record = @resource.record
  @resource.hydrate(view: :new, user: _current_user)

  # Handle special cases when creating a new record via a belongs_to relationship
  if params[:via_belongs_to_resource_class].present?
    return render turbo_stream: turbo_stream.append("attach_modal", partial: "avo/base/new_via_belongs_to")
  end

  set_actions

  @page_title = @resource.default_panel_name.to_s

  if is_associated_record?
    via_resource = Avo.resource_manager.get_resource_by_model_class(params[:via_relation_class])
    via_record = via_resource.find_record params[:via_record_id], params: params
    via_resource = via_resource.new record: via_record

    add_breadcrumb via_resource.plural_name, resources_path(resource: via_resource)
    add_breadcrumb via_resource.record_title, resource_path(record: via_record, resource: via_resource)
  end

  add_breadcrumb @resource.plural_name.humanize, resources_path(resource: @resource)
  add_breadcrumb t("avo.new").humanize

  set_component_for __method__, fallback_view: :edit
end

#previewObject



191
192
193
194
195
# File 'app/controllers/avo/base_controller.rb', line 191

def preview
  @resource.hydrate(record: @record, view: :show, user: _current_user, params: params)

  render layout: params[:turbo_frame].blank?
end

#showObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/avo/base_controller.rb', line 60

def show
  @resource.hydrate(record: @record, view: :show, user: _current_user, params: params).detect_fields

  set_actions

  @page_title = @resource.default_panel_name.to_s

  # If we're accessing this resource via another resource add the parent to the breadcrumbs.
  if params[:via_resource_class].present? && params[:via_record_id].present?
    via_resource = Avo.resource_manager.get_resource(params[:via_resource_class])
    via_record = via_resource.find_record params[:via_record_id], params: params
    via_resource = via_resource.new record: via_record

    add_breadcrumb via_resource.plural_name, resources_path(resource: via_resource)
    add_breadcrumb via_resource.record_title, resource_path(record: via_record, resource: via_resource)
  end

  add_breadcrumb @resource.plural_name.humanize, resources_path(resource: @resource)

  add_breadcrumb @resource.record_title
  add_breadcrumb I18n.t("avo.details").upcase_first

  set_component_for __method__
end

#updateObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/avo/base_controller.rb', line 168

def update
  # record gets instantiated and filled in the fill_record method
  saved = save_record
  @resource = @resource.hydrate(record: @record, view: :edit, user: _current_user)
  set_actions

  set_component_for :edit

  if saved
    update_success_action
  else
    update_fail_action
  end
end