Class: Avo::AssociationsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



82
83
84
85
86
87
88
# File 'app/controllers/avo/associations_controller.rb', line 82

def create
  if create_association
    create_success_action
  else
    create_fail_action
  end
end

#create_associationObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/avo/associations_controller.rb', line 90

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

  if attachment_records.empty?
    @record.errors.add(:base, t("avo.choose_an_option"))
    return false
  end

  perform_action_and_record_errors do
    @record.transaction do
      attachment_records.each do |attachment_record|
        attach_record(association_name, attachment_record)
      end
    end
  end
end

#destroyObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/avo/associations_controller.rb', line 107

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/avo/associations_controller.rb', line 20

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, record: @parent_record).handle || @parent_record.try(@field.id))
  else
    association_query_scope(
      parent_resource: @parent_resource,
      parent_record: @parent_record,
      association_name: association_from_params,
      authorization: @related_authorization,
      resource: @resource,
      field_association_name: params[:related_name]
    )
  end

  super
end

#newObject



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

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 = if attach_using_checkbox_list?
      checkbox_list_options(query)
    else
      select_options(query)
    end
  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



45
46
47
48
49
50
51
# File 'app/controllers/avo/associations_controller.rb', line 45

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

  @resource, @record = @related_resource, @related_record

  super
end