Class: QuickAdmin::ResourcesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/quick_admin/resources_controller.rb

Instance Method Summary collapse

Instance Method Details

#attachmentObject



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

def attachment
  field = params[:field]
  attachment_ref = @resource.public_send(field) if @resource.respond_to?(field)
  @blob = if params[:signed_id].present?
            begin
              ActiveStorage::Blob.find_signed(params[:signed_id])
            rescue ActiveSupport::MessageVerifier::InvalidSignature
              nil
            end
          else
            attachment_ref&.blob
          end

  respond_to do |format|
    format.html { render :attachment }
    format.turbo_stream { render :attachment }
  end
rescue => e
  Rails.logger.error "Attachment error: #{e.message}"
  @blob = nil
  respond_to do |format|
    format.html { render :attachment }
    format.turbo_stream { render :attachment }
  end
end

#bulk_destroyObject



95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/quick_admin/resources_controller.rb', line 95

def bulk_destroy
  ids = params[:ids] || []
  resource_class.where(id: ids).destroy_all
  
  respond_to do |format|
    format.html { redirect_to quick_admin.resources_path(resource_name), notice: "#{ids.count} records deleted successfully." }
    format.turbo_stream { refresh_resources_list }
    format.any { refresh_resources_list; render :bulk_destroy, formats: :turbo_stream }
  end
end

#createObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/quick_admin/resources_controller.rb', line 52

def create
  @resource = resource_class.new(resource_params)
  
  if @resource.save
    respond_to do |format|
      format.html { redirect_to quick_admin.resources_path(resource_name), notice: 'Record created successfully.' }
      format.turbo_stream { refresh_resources_list }
      format.any { refresh_resources_list; render :create, formats: :turbo_stream }
    end
  else
    respond_to do |format|
      format.html { render :new }
      format.turbo_stream { render :new }
    end
  end
end

#destroyObject



86
87
88
89
90
91
92
93
# File 'app/controllers/quick_admin/resources_controller.rb', line 86

def destroy
  @resource.destroy
  respond_to do |format|
    format.html { redirect_to quick_admin.resources_path(resource_name), notice: 'Record deleted successfully.' }
    format.turbo_stream { refresh_resources_list }
    format.any { refresh_resources_list; render :destroy, formats: :turbo_stream }
  end
end

#editObject



69
# File 'app/controllers/quick_admin/resources_controller.rb', line 69

def edit; end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/quick_admin/resources_controller.rb', line 8

def index
  @pagy, @resources = pagy(filtered_resources, items: QuickAdmin.config.per_page)
  
  respond_to do |format|
    format.html
    if params[:search].present? || params[:filter].present?
      format.turbo_stream
      format.any { render :index, formats: :turbo_stream }
    end
  end
end

#newObject



48
49
50
# File 'app/controllers/quick_admin/resources_controller.rb', line 48

def new
  @resource = resource_class.new
end

#showObject



46
# File 'app/controllers/quick_admin/resources_controller.rb', line 46

def show; end

#updateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/quick_admin/resources_controller.rb', line 71

def update
  if @resource.update(resource_params)
    respond_to do |format|
      format.html { redirect_to quick_admin.resources_path(resource_name), notice: 'Record updated successfully.' }
      format.turbo_stream { refresh_resources_list }
      format.any { refresh_resources_list; render :update, formats: :turbo_stream }
    end
  else
    respond_to do |format|
      format.html { render :edit }
      format.turbo_stream { render :edit }
    end
  end
end