Class: Avo::AssociationsController

Inherits:
BaseController show all
Defined in:
app/controllers/avo/associations_controller.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Method Summary collapse

Methods inherited from BaseController

#edit, #preview, #update

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, #button_classes, #card_classes, #chart_color, #container_classes, #container_is_full_width?, #d, #decode_filter_params, #e, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #pagy_major_version, #possibly_rails_authentication?, #render_license_warning, #root_path_without_url, #svg, #white_panel_classes

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



89
90
91
92
93
94
95
# File 'app/controllers/avo/associations_controller.rb', line 89

def create
  if create_association
    create_success_action
  else
    create_fail_action
  end
end

#create_associationObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/avo/associations_controller.rb', line 97

def create_association
  association_name = BaseResource.valid_association_name(@record, association_from_params)

  perform_action_and_record_errors do
    if through_reflection? && additional_params.present?
      new_join_record.save
    elsif has_many_reflection? || through_reflection?
      @record.send(association_name) << @attachment_record
    else
      @record.send(:"#{association_name}=", @attachment_record)
      @record.save!
    end
  end
end

#destroyObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/avo/associations_controller.rb', line 112

def destroy
  association_name = BaseResource.valid_association_name(@record, @field.for_attribute || params[:related_name])

  if through_reflection?
    join_record.destroy!
  elsif has_many_reflection?
    @record.send(association_name).delete @attachment_record
  else
    @record.send(:"#{association_name}=", nil)
  end

  destroy_success_action
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
# File 'app/controllers/avo/associations_controller.rb', line 22

def index
  @parent_resource = @resource.dup
  @resource = @related_resource
  @parent_record = @parent_resource.find_record(params[:id], params: params)
  @parent_resource.hydrate(record: @parent_record)

  # When array field the records are fetched from the field block, from the parent record or from the resource def records
  # When other field type, like has_many the @query is directly fetched from the parent record
  # Don't apply policy on array type since it can return an array of hashes where `.all` and other methods used on policy will fail.
  @query = if @field.type == "array"
    @resource.fetch_records(Avo::ExecutionContext.new(target: @field.block).handle || @parent_record.try(@field.id))
  else
    @related_authorization.apply_policy(
      @parent_record.send(
        BaseResource.valid_association_name(@parent_record, association_from_params)
      )
    )
  end

  @association_field = find_association_field(resource: @parent_resource, association: params[:related_name])

  if @association_field.present? && @association_field.scope.present?
    @query = Avo::ExecutionContext.new(
      target: @association_field.scope,
      query: @query,
      parent: @parent_record,
      resource: @resource,
      parent_resource: @parent_resource
    ).handle
  end

  super
end

#newObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/avo/associations_controller.rb', line 64

def new
  @resource.hydrate(record: @record)

  if @field.present? && !@field.is_searchable?
    query = @related_authorization.apply_policy @attachment_class

    # Add the association scope to the query scope
    if @field.attach_scope.present?
      query = Avo::ExecutionContext.new(target: @field.attach_scope, query: query, parent: @record).handle
    end

    @options = select_options(query)
  end

  @url = Avo::Services::URIService.parse(avo.root_url.to_s)
    .append_paths("resources", params[:resource_name], params[:id], params[:related_name])
    .append_query(
      {
        view: @resource&.view&.to_s,
        for_attribute: @field&.try(:for_attribute)
      }.compact
    )
    .to_s
end

#showObject



56
57
58
59
60
61
62
# File 'app/controllers/avo/associations_controller.rb', line 56

def show
  @parent_resource, @parent_record = @resource, @record

  @resource, @record = @related_resource, @related_record

  super
end