Class: CrudController

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

Overview

Abstract controller providing basic CRUD actions.

Some enhancements were made to ease extensibility. 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.

Direct Known Subclasses

CrudTestModelsController

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ListController

#index

Methods included from DryCrud::GenericModel

#ivar_name, #model_ivar_get, #model_ivar_set, #model_scope, #path_args

Methods included from DryCrud::Nestable

prepended

Class Method Details

.before_render_form(*methods) ⇒ Object

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



240
241
242
243
# File 'app/controllers/crud_controller.rb', line 240

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

Instance Method Details

#create(**options, &block) ⇒ Object

POST /entries

POST /entries.json

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 for certain formats, you may overwrite this action and call super with a block that gets the format and success parameters. Calling a format action (e.g. format.html) in the given block will take precedence over the one defined here.

Specify a :location option if you wish to do a custom redirect.



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

def create(**options, &block)
  model_class.transaction do
    assign_attributes
    created = with_callbacks(:create, :save) { entry.save }
    respond(created,
            **options.merge(status: :created, render_on_failure: :new),
            &block)
    raise ActiveRecord::Rollback unless created
  end
end

#destroy(**options, &block) ⇒ Object

DELETE /entries/1

DELETE /entries/1.json

Destroy an existing entry of this model. There are before and after destroy callbacks to hook into the action.

To customize the response for certain formats, you may overwrite this action and call super with a block that gets format and success parameters. Calling a format action (e.g. format.html) in the given block will take precedence over the one defined here.

Specify a :location option if you wish to do a custom redirect.



106
107
108
109
110
111
112
113
114
# File 'app/controllers/crud_controller.rb', line 106

def destroy(**options, &block)
  model_class.transaction do
    destroyed = run_callbacks(:destroy) { entry.destroy }
    respond(destroyed,
            **options.merge(status: :no_content),
            &block)
    raise ActiveRecord::Rollback unless destroyed
  end
end

#editObject

GET /entries/1/edit

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



46
# File 'app/controllers/crud_controller.rb', line 46

def edit; end

#newObject

GET /entries/new

GET /entries/new.json

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



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

def new
  assign_attributes if params[model_identifier]
end

#showObject

GET /entries/1

GET /entries/1.json

Show one entry of this model.



33
# File 'app/controllers/crud_controller.rb', line 33

def show; end

#update(**options, &block) ⇒ Object

PUT /entries/1

PUT /entries/1.json

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 for certain formats, you may overwrite this action and call super with a block that gets the format and success parameters. Calling a format action (e.g. format.html) in the given block will take precedence over the one defined here.

Specify a :location option if you wish to do a custom redirect.



83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/crud_controller.rb', line 83

def update(**options, &block)
  model_class.transaction do
    assign_attributes
    updated = with_callbacks(:update, :save) { entry.save }
    respond(updated,
            **options.merge(status: :ok, render_on_failure: :edit),
            &block)
    raise ActiveRecord::Rollback unless updated
  end
end