Class: Avo::RelationsController

Inherits:
BaseController show all
Defined in:
app/controllers/avo/relations_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

Methods included from UrlHelpers

#edit_resource_path, #new_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_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, #svg, #turbo_frame_wrap

Instance Method Details

#createObject



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

def create
  if reflection_class == "HasManyReflection"
    @model.send(params[:related_name].to_s) << @attachment_model
  else
    @model.send("#{params[:related_name]}=", @attachment_model)
  end

  respond_to do |format|
    if @model.save
      format.html { redirect_to resource_path(model: @model, resource: @resource), notice: t("avo.attachment_class_attached", attachment_class: @attachment_class) }
      format.json { render :show, status: :created, location: resource_path(model: @model, resource: @resource) }
    else
      format.html { render :new }
      format.json { render json: @model.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/avo/relations_controller.rb', line 60

def destroy
  if reflection_class == "HasManyReflection"
    @model.send(params[:related_name].to_s).delete @attachment_model
  else
    @model.send("#{params[:related_name]}=", nil)
  end

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

#indexObject



15
16
17
18
19
20
21
22
23
# File 'app/controllers/avo/relations_controller.rb', line 15

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)
  @query = @authorization.apply_policy @parent_model.public_send(params[:related_name])

  super
end

#newObject



31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/avo/relations_controller.rb', line 31

def new
  query = @authorization.apply_policy @attachment_class

  @options = query.all.map do |model|
    {
      value: model.id,
      label: model.send(@attachment_resource.class.title)
    }
  end
end

#showObject



25
26
27
28
29
# File 'app/controllers/avo/relations_controller.rb', line 25

def show
  @resource, @model = @related_resource, @related_model

  super
end