Class: Wallaby::AbstractResourcesController

Inherits:
BaseController show all
Includes:
ResourcesHelperMethods
Defined in:
app/controllers/wallaby/abstract_resources_controller.rb

Overview

Generic CRUD controller

Direct Known Subclasses

ResourcesController

Constant Summary

Constants inherited from ApplicationController

Wallaby::ApplicationController::ERROR_PATH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#current_model_class, #current_resources_name

Methods inherited from SecureController

#current_user, #forbidden, #security_config, #unauthorized

Methods inherited from ApplicationController

#bad_request, #configuration, configuration, #healthy, #not_found, #unprocessable_entity

Class Method Details

.model_classClass

Returns model class for controller.

Returns:

  • (Class)

    model class for controller



21
22
23
24
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 21

def model_class
  return unless self < configuration.mapping.resources_controller
  Map.model_class_map resources_name
end

.resources_nameString

Returns resources name for controller.

Returns:

  • (String)

    resources name for controller



15
16
17
18
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 15

def resources_name
  return unless self < configuration.mapping.resources_controller
  Map.resources_name_map name.gsub('Controller', EMPTY_STRING)
end

Instance Method Details

#createObject

Resourceful create action to create a record.

You can override this method in subclasses in the following ways:

  • To perform action before ‘new` is run

    def create
      # do something beforehand
      super
    end
    
  • To perform action after ‘create`, but before rendering.

The reason is that we use responder at the end of the action

def create
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def create
      # do something completely different
    end
    


125
126
127
128
129
130
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 125

def create
  authorize! :create, resource
  current_model_service.create resource, params
  yield if block_given? # after_create
  respond_with resource, location: helpers.show_path(resource)
end

#current_model_serviceWallaby::ModelServicer

Model servicer associated to current modal class.

This model servicer will take care of all the CRUD operations

For how to override model service, see (Wallaby::ModelServicer)

Returns:



264
265
266
267
268
269
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 264

def current_model_service
  @current_model_service ||= begin
    model_class = current_model_class
    Map.servicer_map(model_class).new model_class, authorizer
  end
end

#destroyObject

Resourceful destroy action to delete a record.

You can override this method in subclasses in the following ways:

  • To perform action before ‘destroy` is run

    def destroy
      # do something beforehand
      super
    end
    
  • To perform action after ‘destroy`, but before rendering.

The reason is that we use responder at the end of the action

def destroy
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def destroy
      # do something completely different
    end
    


251
252
253
254
255
256
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 251

def destroy
  authorize! :destroy, resource
  current_model_service.destroy resource, params
  yield if block_given? # after_destroy
  respond_with resource, location: helpers.index_path(current_model_class)
end

#editObject

Resourceful edit action to show a form for editing a record.

You can override this method in subclasses in the following ways:

  • To perform action before ‘edit` is run

    def edit
      # do something beforehand
      super
    end
    
  • To perform action after ‘edit`, but before rendering.

The reason is that we use responder at the end of the action

def edit
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def edit
      # do something completely different
    end
    


188
189
190
191
192
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 188

def edit
  authorize! :edit, resource
  yield if block_given? # after_edit
  respond_with resource
end

#homeObject

Home page

You can completely override this action:

def home
  # do something differently
end


34
35
36
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 34

def home
  # do nothing
end

#indexObject

Resourceful action to list paginated records.

You can override this method in subclasses in the following ways:

  • To perform action before ‘index` is run

    def index
      # do something beforehand
      super
    end
    
  • To perform action after ‘index`, but before rendering.

The reason is that we use responder at the end of the action

def index
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def index
      # do something completely different
    end
    


63
64
65
66
67
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 63

def index
  authorize! :index, current_model_class
  yield if block_given? # after_index
  respond_with collection
end

#newObject

Resourceful new action to show a form for creating a record.

You can override this method in subclasses in the following ways:

  • To perform action before ‘new` is run

    def new
      # do something beforehand
      super
    end
    
  • To perform action after ‘new`, but before rendering.

The reason is that we use responder at the end of the action

def new
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def new
      # do something completely different
    end
    


94
95
96
97
98
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 94

def new
  authorize! :new, resource
  yield if block_given? # after_new
  respond_with resource
end

#resource_paramsActionController::Parameters

To whitelist the params for CRUD actions

Returns:

  • (ActionController::Parameters)

    whitelisted params

See Also:

  • ModelServicer#permit


274
275
276
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 274

def resource_params
  @resource_params ||= current_model_service.permit params
end

#showObject

Resourceful show action to display values for a record.

You can override this method in subclasses in the following ways:

  • To perform action before ‘show` is run

    def show
      # do something beforehand
      super
    end
    
  • To perform action after ‘show`, but before rendering.

The reason is that we use responder at the end of the action

def show
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def show
      # do something completely different
    end
    


157
158
159
160
161
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 157

def show
  authorize! :show, resource
  yield if block_given? # after_show
  respond_with resource
end

#updateObject

Resourceful update action to update a record.

You can override this method in subclasses in the following ways:

  • To perform action before ‘update` is run

    def update
      # do something beforehand
      super
    end
    
  • To perform action after ‘update`, but before rendering.

The reason is that we use responder at the end of the action

def update
  super do
    # do something afterwards
  end
end
  • To perform completely different action

    def update
      # do something completely different
    end
    


219
220
221
222
223
224
# File 'app/controllers/wallaby/abstract_resources_controller.rb', line 219

def update
  authorize! :update, resource
  current_model_service.update resource, params
  yield if block_given? # after_update
  respond_with resource, location: helpers.show_path(resource)
end