Class: AdminResources::ResourcesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_custom_pages, #admin_models, #admin_path_for, #after_sign_in_path_for, #after_sign_out_path_for

Instance Method Details

#createObject



22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/admin_resources/resources_controller.rb', line 22

def create
  puts "[AdminResources::ResourcesController] create #{model_name}"
  @resource = model_class.new(resource_params)
  if @resource.save
    sync_join_associations
    flash[:new_url] = admin_path_for(model_name, :new)
    redirect_to admin_path_for(model_name, :show, @resource), notice: "#{model_name} was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#custom_actionObject

Dispatches to a host-app controller action via a configured custom action. The host app must define a route that this proxies to, specified as ‘handler`. If no handler is configured, falls back to calling a same-named method on the resource.



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

def custom_action
  action_name_param = params[:custom_action]
  action_config = custom_actions.find { |a| a[:name].to_s == action_name_param }
  unless action_config
    redirect_to admin_path_for(model_name, :show, @resource), alert: "Unknown action."
    return
  end

  handler = action_config[:handler]
  if handler
    # handler is a callable (proc/lambda) that receives (resource, controller)
    handler.call(@resource, self)
  else
    # Default: call a method by the action name on the resource
    if @resource.respond_to?(action_name_param)
      @resource.public_send(action_name_param)
      redirect_to admin_path_for(model_name, :show, @resource),
                  notice: "#{action_config[:label] || action_name_param} completed."
    else
      redirect_to admin_path_for(model_name, :show, @resource), alert: "Action not implemented."
    end
  end
end

#destroyObject



48
49
50
51
52
# File 'app/controllers/admin_resources/resources_controller.rb', line 48

def destroy
  puts "[AdminResources::ResourcesController] destroy #{model_name}##{@resource.id}"
  @resource.destroy
  redirect_to admin_path_for(model_name, :index), notice: "#{model_name} was successfully deleted."
end

#editObject



34
35
36
# File 'app/controllers/admin_resources/resources_controller.rb', line 34

def edit
  puts "[AdminResources::ResourcesController] edit #{model_name}##{@resource.id}"
end

#indexObject



8
9
10
11
# File 'app/controllers/admin_resources/resources_controller.rb', line 8

def index
  puts "[AdminResources::ResourcesController] index for #{model_name}"
  @resources = filter_resources(model_class.all).order(id: :desc)
end

#newObject



17
18
19
20
# File 'app/controllers/admin_resources/resources_controller.rb', line 17

def new
  puts "[AdminResources::ResourcesController] new #{model_name}"
  @resource = model_class.new
end

#showObject



13
14
15
# File 'app/controllers/admin_resources/resources_controller.rb', line 13

def show
  puts "[AdminResources::ResourcesController] show #{model_name}##{@resource.id}"
end

#updateObject



38
39
40
41
42
43
44
45
46
# File 'app/controllers/admin_resources/resources_controller.rb', line 38

def update
  puts "[AdminResources::ResourcesController] update #{model_name}##{@resource.id}"
  if @resource.update(resource_params)
    sync_join_associations
    redirect_to admin_path_for(model_name, :show, @resource), notice: "#{model_name} was successfully updated."
  else
    render :edit, status: :unprocessable_entity
  end
end