Class: Admin::CrudController

Inherits:
ListController
  • Object
show all
Defined in:
app/controllers/admin/crud_controller.rb

Overview

Abstract controller providing basic CRUD actions. This implementation mainly follows the one of the Rails scaffolding controller and responds to HTML and JSON requests. Some enhancements were made to ease extendability. The current model entry is available in the view as an instance variable named after the model_class or in the helper method entry. Several protected helper methods are there to be (optionally) overriden by subclasses. With the help of additional callbacks, it is possible to hook into the action procedures without overriding the entire method.

Defined Under Namespace

Classes: Responder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.before_render_form(*methods) ⇒ Object

Convenience callback to apply a callback on both form actions (new and edit).


203
204
205
206
# File 'app/controllers/admin/crud_controller.rb', line 203

def before_render_form(*methods)
  before_render_new(*methods)
  before_render_edit(*methods)
end

.model_identifierObject

The identifier of the model used for form parameters. I.e., the symbol of the underscored model name.


197
198
199
# File 'app/controllers/admin/crud_controller.rb', line 197

def model_identifier
  @model_identifier ||= model_class.model_name.param_key
end

Instance Method Details

#create(options = {}, &block) ⇒ Object

Create a new entry of this model from the passed params. There are before and after create callbacks to hook into the action. To customize the response, you may overwrite this action and call super with a block that gets the format parameter. Specify a :location option if you wish to do a custom redirect.

POST /entries
POST /entries.json

58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/admin/crud_controller.rb', line 58

def create(options = {}, &block)
  assign_attributes
  created = with_callbacks(:create, :save) { with_organization
                                             entry.save 
                                           }
  respond_options = options.reverse_merge(success: created)
  if redirect_on_create and !request.env["HTTP_REFERER"].blank? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
    redirect_to redirect_on_create
  else
    respond_with(:admin, entry, respond_options, &block)
  end
end

#destroy(options = {}, &block) ⇒ Object

Destroy an existing entry of this model. There are before and after destroy callbacks to hook into the action. To customize the response, you may overwrite this action and call super with a block that gets success and format parameters. Specify a :location option if you wish to do a custom redirect.

DELETE /entries/1
DELETE /entries/1.json

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/admin/crud_controller.rb', line 99

def destroy(options = {}, &block)
  destroyed = run_callbacks(:destroy) { entry.destroy }
  unless destroyed
    set_failure_notice
    location = request.env['HTTP_REFERER'].presence
  end
  location ||= index_url
  respond_options = options.reverse_merge(success: destroyed,
                                          location: location)
  if redirect_on_delete and !request.env["HTTP_REFERER"].blank? and request.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
    redirect_to redirect_on_delete
  else
    location = !destroyed && request.env["HTTP_REFERER"].presence || index_url
    respond_with(:admin, entry, options.reverse_merge(:success => destroyed, :location => location), &block)
  end
end

#edit(&block) ⇒ Object

Display a form to edit an exisiting entry of this model.

GET /entries/1/edit

73
74
75
76
# File 'app/controllers/admin/crud_controller.rb', line 73

def edit(&block)
  # respond_with(:admin, entry, &block)
  redirect_to index_url
end

#new(&block) ⇒ Object

Display a form to create a new entry of this model.

GET /entries/new
GET /entries/new.json

46
47
48
49
# File 'app/controllers/admin/crud_controller.rb', line 46

def new(&block)
  assign_attributes if params[model_identifier]
  respond_with(:admin, entry, &block)
end

#show(&block) ⇒ Object

Show one entry of this model.

GET /entries/1
GET /entries/1.json

39
40
41
# File 'app/controllers/admin/crud_controller.rb', line 39

def show(&block)
  respond_with(:admin, entry, &block)
end

#update(options = {}, &block) ⇒ Object

Update an existing entry of this model from the passed params. There are before and after update callbacks to hook into the action. To customize the response, you may overwrite this action and call super with a block that gets the format parameter. Specify a :location option if you wish to do a custom redirect.

PUT /entries/1
PUT /entries/1.json

85
86
87
88
89
90
# File 'app/controllers/admin/crud_controller.rb', line 85

def update(options = {}, &block)
  assign_attributes
  updated = with_callbacks(:update, :save) { entry.save }
  respond_options = options.reverse_merge(success: updated)
  respond_with(:admin, entry, respond_options, &block)
end