Module: EasyAdmin::Concerns::BelongsToEditing

Extended by:
ActiveSupport::Concern
Included in:
ResourcesController
Defined in:
app/controllers/easy_admin/concerns/belongs_to_editing.rb

Overview

BelongsToEditing concern handles belongs_to field specific editing functionality Provides methods for reattaching and editing attached belongs_to records

Instance Method Summary collapse

Instance Method Details

#belongs_to_edit_attachedObject

Edit the attached record in a belongs_to association



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
# File 'app/controllers/easy_admin/concerns/belongs_to_editing.rb', line 27

def belongs_to_edit_attached
  @field_name = params[:field]
  @field_config = find_field_config_by_name_or_association(@field_name)
  
  unless @field_config
    render json: { error: "Field '#{@field_name}' not found" }, status: :not_found
    return
  end

  unless @field_config[:type] == :belongs_to
    render json: { error: "Field '#{@field_name}' is not a belongs_to field" }, status: :bad_request
    return
  end

  # Get the association info - use explicit association name if provided
  association_name = @field_config[:association] || @field_config[:name]
  model_class = @resource_class.model_class
  association_reflection = model_class.reflect_on_association(association_name)
  
  unless association_reflection
    render json: { error: "Association '#{association_name}' not found on #{model_class.name}" }, status: :bad_request
    return
  end

  # Get the associated record using the association name
  @associated_record = @record.public_send(association_name)
  
  unless @associated_record
    render json: { error: "No associated record found" }, status: :not_found
    return
  end

  # Get the resource class for the associated record
  associated_model_class = association_reflection.klass
  @associated_resource_class = find_resource_class_for_model(associated_model_class)

  unless @associated_resource_class
    render json: { error: "No resource class found for #{associated_model_class.name}" }, status: :not_found
    return
  end

  respond_to do |format|
    format.html { render template: 'easy_admin/resources/belongs_to_edit_attached', layout: false }
  end
end

#belongs_to_reattachObject

Reattach a belongs_to association (select different associated record)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/easy_admin/concerns/belongs_to_editing.rb', line 9

def belongs_to_reattach
  @field_name = params[:field]
  @field_config = find_field_config_by_name_or_association(@field_name)
  
  unless @field_config
    render json: { error: "Field '#{@field_name}' not found" }, status: :not_found
    return
  end

  # Convert belongs_to field to select field format for reattaching
  @select_field_config = prepare_belongs_to_as_select(@field_config)

  respond_to do |format|
    format.html { render template: 'easy_admin/resources/belongs_to_reattach', layout: false }
  end
end

#update_belongs_to_attachedObject

Update the attached record in a belongs_to association



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
106
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
# File 'app/controllers/easy_admin/concerns/belongs_to_editing.rb', line 74

def update_belongs_to_attached
  @field_name = params[:field]
  @field_config = find_field_config_by_name_or_association(@field_name)
  @attached_id = params[:attached_id]
  
  unless @field_config
    render json: { error: "Field '#{@field_name}' not found" }, status: :not_found
    return
  end

  # Get the association info - use explicit association name if provided
  association_name = @field_config[:association] || @field_config[:name]
  model_class = @resource_class.model_class
  association_reflection = model_class.reflect_on_association(association_name)
  
  unless association_reflection
    render json: { error: "Association '#{association_name}' not found on #{model_class.name}" }, status: :bad_request
    return
  end

  associated_model_class = association_reflection.klass
  associated_resource_class = find_resource_class_for_model(associated_model_class)
  
  # Find the attached record
  attached_record = associated_model_class.find(@attached_id)
  
  unless attached_record
    render json: { error: "Attached record not found" }, status: :not_found
    return
  end

  # Get the update parameters for the attached record
  raw_params = params.dig(associated_resource_class.param_key) || {}
  
  # Get permitted attributes from the resource class
  permitted_attributes = get_permitted_attributes(associated_resource_class)
  update_params = raw_params.permit(*permitted_attributes)
  
  if attached_record.update(update_params)
    @success = true
    @attached_record = attached_record
    @association_name = association_name
  else
    @success = false
    @attached_record = attached_record
  end

  respond_to do |format|
    format.turbo_stream { render template: 'easy_admin/resources/update_belongs_to_attached' }
    format.json do
      if @success
        render json: { status: 'success' }
      else
        render json: { errors: @attached_record.errors }
      end
    end
  end
end