Class: Avo::AssociationsController

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

Instance Method Summary collapse

Methods inherited from BaseController

#edit, #update

Methods inherited from ApplicationController

#_current_user, #check_avo_license, #context, #exception_logger, #init_app, #render, #turbo_frame_request?

Methods included from UrlHelpers

#edit_resource_path, #new_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, #empty_state, #get_model_class, #input_classes, #render_license_warning, #render_license_warnings, #root_path_without_url, #svg

Instance Method Details

#createObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/avo/associations_controller.rb', line 61

def create
  association_name = BaseResource.valid_association_name(@model, params[:related_name])

  if reflection_class == "HasManyReflection"
    @model.send(association_name) << @attachment_model
  else
    @model.send("#{association_name}=", @attachment_model)
  end

  respond_to do |format|
    if @model.save
      format.html { redirect_back fallback_location: resource_view_response_path, notice: t("avo.attachment_class_attached", attachment_class: @related_resource.name) }
    else
      format.html { render :new }
    end
  end
end

#destroyObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/avo/associations_controller.rb', line 79

def destroy
  association_name = BaseResource.valid_association_name(@model, params[:related_name])

  if reflection_class == "HasManyReflection"
    @model.send(association_name).delete @attachment_model
  else
    @model.send("#{association_name}=", nil)
  end

  respond_to do |format|
    format.html { redirect_to params[:referrer] || resource_view_response_path, notice: t("avo.attachment_class_detached", attachment_class: @attachment_class) }
  end
end

#indexObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/avo/associations_controller.rb', line 20

def index
  @parent_resource = @resource.dup
  @resource = @related_resource
  @parent_model = @parent_resource.class.find_scope.find(params[:id])
  @parent_resource.hydrate(model: @parent_model)
  association_name = BaseResource.valid_association_name(@parent_model, params[:related_name])
  @query = @authorization.apply_policy @parent_model.send(association_name)
  @association_field = @parent_resource.get_field params[:related_name]

  if @association_field.present? && @association_field.scope.present?
    @query = Avo::Hosts::AssociationScopeHost.new(block: @association_field.scope, query: @query, parent: @parent_model).handle
  end

  super
end

#newObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/avo/associations_controller.rb', line 44

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

  if @field.present? && !@field.searchable
    query = @authorization.apply_policy @attachment_class

    # Add the association scope to the query scope
    if @field.attach_scope.present?
      query = Avo::Hosts::AssociationScopeHost.new(block: @field.attach_scope, query: query, parent: @model).handle
    end

    @options = query.all.map do |model|
      [model.send(@attachment_resource.class.title), model.id]
    end
  end
end

#orderObject



93
94
95
96
97
98
# File 'app/controllers/avo/associations_controller.rb', line 93

def order
  @parent_resource = @resource.dup
  @resource, @model = @related_resource, @related_model

  super
end

#showObject



36
37
38
39
40
41
42
# File 'app/controllers/avo/associations_controller.rb', line 36

def show
  @parent_resource, @parent_model = @resource, @model

  @resource, @model = @related_resource, @related_model

  super
end