Class: AdminSuite::ResourcesController

Inherits:
ApplicationController show all
Includes:
Pagy::Backend, Pagy::Frontend
Defined in:
app/controllers/admin_suite/resources_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_actionObject

POST /:portal/:resource_name/bulk_action/:action_name



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/admin_suite/resources_controller.rb', line 79

def bulk_action
  action = params[:action_name].to_s.to_sym
  ids = params[:ids] || []
  if ids.empty?
    redirect_to collection_url, alert: "No items selected."
    return
  end

  model = resource_class
  records = model.where(id: ids)
  executor = Admin::Base::ActionExecutor.new(resource_config, action, admin_suite_actor)
  result = executor.execute_bulk(records, params.to_unsafe_h)

  if result.success?
    redirect_to collection_url, notice: result.message
  else
    redirect_to collection_url, alert: result.message
  end
end

#createObject

POST /:portal/:resource_name



34
35
36
37
38
39
40
41
# File 'app/controllers/admin_suite/resources_controller.rb', line 34

def create
  @resource = resource_class.new(resource_params)
  if @resource.save
    redirect_to resource_url(@resource), notice: "#{resource_config.human_name} was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject

DELETE /:portal/:resource_name/:id



53
54
55
56
# File 'app/controllers/admin_suite/resources_controller.rb', line 53

def destroy
  @resource.destroy!
  redirect_to collection_url, notice: "#{resource_config.human_name} was successfully deleted.", status: :see_other
end

#editObject

GET /:portal/:resource_name/:id/edit



30
31
# File 'app/controllers/admin_suite/resources_controller.rb', line 30

def edit
end

#execute_actionObject

POST /:portal/:resource_name/:id/execute_action/:action_name



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/admin_suite/resources_controller.rb', line 59

def execute_action
  action = params[:action_name].to_s.to_sym
  action_def = find_action(action)
  unless action_def
    redirect_to resource_url(@resource), alert: "Action not found."
    return
  end

  executor = Admin::Base::ActionExecutor.new(resource_config, action, admin_suite_actor)
  result = executor.execute_member(@resource, params.to_unsafe_h)
  redirect_target = result.redirect_url.presence || resource_url(@resource)

  if result.success?
    redirect_to redirect_target, notice: result.message
  else
    redirect_to resource_url(@resource), alert: result.message
  end
end

#indexObject

GET /:portal/:resource_name



14
15
16
17
18
# File 'app/controllers/admin_suite/resources_controller.rb', line 14

def index
  scope = filtered_collection
  @stats = calculate_stats(scope) if resource_config&.index_config&.stats_list&.any?
  @pagy, @collection = paginate_collection(scope)
end

#newObject

GET /:portal/:resource_name/new



25
26
27
# File 'app/controllers/admin_suite/resources_controller.rb', line 25

def new
  @resource = resource_class.new
end

#showObject

GET /:portal/:resource_name/:id



21
22
# File 'app/controllers/admin_suite/resources_controller.rb', line 21

def show
end

#toggleObject

POST /:portal/:resource_name/:id/toggle



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
# File 'app/controllers/admin_suite/resources_controller.rb', line 100

def toggle
  field = params[:field].presence&.to_sym
  field ||= toggleable_fields.first if toggleable_fields.one?
  unless field
    head :unprocessable_entity
    return
  end

  unless toggleable_fields.include?(field)
    head :unprocessable_entity
    return
  end

  current_value = !!@resource.public_send(field)
  @resource.update!(field => !current_value)

  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.replace(
        dom_id(@resource, "#{field}_toggle"),
        partial: "admin_suite/shared/toggle_cell",
        locals: { record: @resource, field: field, toggle_url: resource_toggle_path(portal: current_portal, resource_name: resource_name, id: @resource.to_param, field: field) }
      )
    end
    format.html { redirect_to resource_url(@resource), notice: "#{resource_config.human_name} updated." }
  end
end

#updateObject

PATCH/PUT /:portal/:resource_name/:id



44
45
46
47
48
49
50
# File 'app/controllers/admin_suite/resources_controller.rb', line 44

def update
  if @resource.update(resource_params)
    redirect_to resource_url(@resource), notice: "#{resource_config.human_name} was successfully updated."
  else
    render :edit, status: :unprocessable_entity
  end
end