Class: Avo::BaseController

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

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Method Summary collapse

Methods included from Concerns::SafeCall

#safe_call

Methods included from Concerns::FiltersSessionHandler

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

Methods inherited from BaseApplicationController

#exception_logger, #turbo_frame_request?

Methods included from Concerns::FindAssociationField

#find_association_field

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, #body_classes, #button_classes, #chart_color, #container_classes, #d, #decode_filter_params, #e, #editor_file_path, #editor_url, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #possibly_rails_authentication?, #render_license_warning, #root_path_without_url, #rtl?, #text_direction, #ui, #wrap_in_modal

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_selector_data_attributes, #record_path, #record_title, #resource_for_record, #resource_grid, #resource_show_path, #resource_table

Methods included from CommonController

#default_url_options, #extra_default_url_options

Methods included from InitializesAvo

#_current_user, #context, #init_app

Instance Method Details

#createObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/avo/base_controller.rb', line 170

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.class.reflect_on_association(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) || @reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
      # 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: Avo::ViewInquirer.new(:new), user: _current_user)

  add_breadcrumb title: @resource.plural_name.humanize, path: resources_path(resource: @resource), initials: @resource.class.initials
  add_breadcrumb title: t("avo.new").humanize, path: nil, avatar: @resource.avatar, initials: @resource.initials
  set_actions

  set_component_for :edit

  if saved
    create_success_action
  else
    create_fail_action
  end
end

#destroyObject



242
243
244
245
246
247
248
# File 'app/controllers/avo/base_controller.rb', line 242

def destroy
  if destroy_model
    destroy_success_action
  else
    destroy_fail_action
  end
end

#editObject



218
219
220
221
222
223
224
225
# File 'app/controllers/avo/base_controller.rb', line 218

def edit
  set_actions

  set_component_for __method__

  # Called from avo-advanced
  safe_call :handle_reactive_fields
end

#indexObject



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
103
104
105
# File 'app/controllers/avo/base_controller.rb', line 22

def index
  @page_title = @resource.plural_name.humanize

  if @reflection.present? && !turbo_frame_request?
    # A good rule of thumb is that the resource name entry (Users) gets just the initials
    # The record name gets initials and avatar
    add_breadcrumb title: @record.class.to_s.pluralize, path: resources_path(resource: @parent_resource), initials: @parent_resource.class.initials
    add_breadcrumb title: @parent_resource.record_title, path: resource_path(record: @record, resource: @parent_resource), initials: @parent_resource.initials, avatar: @parent_resource.avatar
  end
  add_breadcrumb title: @resource.plural_name.humanize, initials: @resource.class.initials

  set_applied_filters
  set_index_params
  set_filters
  set_actions
  set_query

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

  # Apply the search query if configured on the resource
  apply_search

  # Eager load attachments
  if @resource.attachments.present?
    @resource.attachments.each do |attachment|
      @query = @query.send(:"with_attached_#{attachment}")
    end
  end

  apply_sorting if @index_params[:sort_by]

  # 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__

  if request.headers["X-Search-Request"] == "resource-search-controller"
    respond_to do |format|
      format.turbo_stream do
        has_resources = @resources.present?
        common_args = {
          resources: @resources,
          resource: @resource,
          reflection: @reflection,
          parent_record: @parent_record,
          parent_resource: @parent_resource,
          pagy: @pagy,
          query: @query,
          actions: @actions
        }

        render turbo_stream: [
          turbo_stream.replace("#{@resource.model_key}_body_content") do
            Avo::Current.view_context.render Avo::ResourceListingComponent.new(
              **common_args,
              turbo_frame: @turbo_frame,
              index_params: @index_params
            )
          end,
          # used to show/hide the filters button and bar
          has_resources ? turbo_stream.remove_css_class("body", "index-missing-resources") : turbo_stream.add_css_class("body", "index-missing-resources")
        ]
      end
    end
  end
end

#newObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/avo/base_controller.rb', line 140

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

  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 title: via_resource.plural_name, path: resources_path(resource: via_resource), initials: via_resource.class.initials
    add_breadcrumb title: via_resource.record_title, path: resource_path(record: via_record, resource: via_resource), avatar: via_resource.avatar, initials: via_resource.initials

    add_breadcrumb title: @resource.plural_name.humanize, initials: @resource.class.initials
  else
    add_breadcrumb title: @resource.plural_name.humanize, path: resources_path(resource: @resource), initials: @resource.class.initials
  end

  add_breadcrumb title: t("avo.new").humanize

  set_component_for __method__, fallback_view: :edit

  # Called from avo-advanced
  safe_call :handle_reactive_fields
end

#previewObject



250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/avo/base_controller.rb', line 250

def preview
  @authorized = @authorization.set_record(@record || @resource.model_class).authorize_action :preview, raise_exception: false

  if @authorized
    @resource.hydrate(record: @record, view: Avo::ViewInquirer.new(:show), user: _current_user, params: params)

    @preview_fields = @resource.get_preview_fields
  end

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

#searchObject



262
263
264
265
266
267
268
269
270
271
# File 'app/controllers/avo/base_controller.rb', line 262

def search
  # Do not remove this method, it is implemented on avo-pro
  # Just a placeholder to avoid:
  # The search action could not be found for the :authorize_base_action
  # callback on Avo::UsersController, but it is listed in the controller's
  # :except option.
  # Raising for missing callback actions is a new default in Rails 7.1, if you'd
  # like to turn this off you can delete the option from the environment configurations
  # or set `config.action_controller.raise_on_missing_callback_actions` to `false`.
end

#showObject



107
108
109
110
111
112
113
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
# File 'app/controllers/avo/base_controller.rb', line 107

def show
  @resource.hydrate(
    record: @record,
    view: Avo::ViewInquirer.new(: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 title: via_resource.plural_name, path: resources_path(resource: via_resource), initials: via_resource.class.initials
    add_breadcrumb title: via_resource.record_title, path: resource_path(record: via_record, resource: via_resource), avatar: via_resource.avatar, initials: via_resource.initials

    # The path is nil because it's not easy to compute the association link (course->course_links = /links)
    add_breadcrumb title: @resource.plural_name.humanize, path: nil, initials: @resource.class.initials
  else
    add_breadcrumb title: @resource.plural_name.humanize, path: resources_path(resource: @resource), initials: @resource.class.initials
  end

  add_breadcrumb title: @resource.record_title, path: nil, avatar: @resource.avatar, initials: @resource.initials
  add_breadcrumb title: I18n.t("avo.details").upcase_first, path: nil

  set_component_for __method__
end

#updateObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/controllers/avo/base_controller.rb', line 227

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

  set_component_for :edit

  if saved
    update_success_action
  else
    update_fail_action
  end
end