Class: AppKit::ResourcesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- AppKit::ResourcesController
- Defined in:
- app/controllers/app_kit/resources_controller.rb
Overview
The base resource controller. This controller contains all the functionality for REST actions for all resources. Resource specific controllers are generated that inherit from this class. It should not be nessicary to use this class directly.
Instance Method Summary collapse
-
#create ⇒ Object
POST /resource Creates a new resource record if valid, renders new form if not.
-
#destroy ⇒ Object
DELETE /resource/:id Deletes a given resource and redirects to index.
-
#edit ⇒ Object
GET /resource/:id/edit Shows the edit form for a given resource.
- #history ⇒ Object
-
#index ⇒ Object
GET /resource Lists all records for an invoice.
-
#new ⇒ Object
GET /resource/new Renders new form for a given resource.
-
#perform_action ⇒ Object
GET /resource/:id/:action_name A catchall action for any custom actions defined in the DSL.
-
#show ⇒ Object
GET /resource/:id Shows the details of a given resource.
- #show_version ⇒ Object
-
#update ⇒ Object
PATCH /resource/:id Updates a given resource if valid, renders the edit form if not.
- #version ⇒ Object
Instance Method Details
#create ⇒ Object
POST /resource Creates a new resource record if valid, renders new form if not.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/controllers/app_kit/resources_controller.rb', line 41 def create @record = model.new(record_params) # call before actions created in the DSL if resource.before_actions[:create] resource.before_actions[:create].call(@record) end if @record.save redirect_to polymorphic_path([app_kit, @record]) else puts @record.errors..inspect render 'new' end end |
#destroy ⇒ Object
DELETE /resource/:id Deletes a given resource and redirects to index.
86 87 88 89 90 91 |
# File 'app/controllers/app_kit/resources_controller.rb', line 86 def destroy if @record.destroy flash[:success] = "Record deleted successfully." redirect_to polymorphic_path([app_kit, model]) end end |
#edit ⇒ Object
GET /resource/:id/edit Shows the edit form for a given resource.
64 65 66 |
# File 'app/controllers/app_kit/resources_controller.rb', line 64 def edit resource.before_actions[:edit].call(@record) if resource.before_actions[:edit] end |
#history ⇒ Object
93 |
# File 'app/controllers/app_kit/resources_controller.rb', line 93 def history; end |
#index ⇒ Object
GET /resource Lists all records for an invoice.
20 21 22 23 24 25 26 27 |
# File 'app/controllers/app_kit/resources_controller.rb', line 20 def index filter_params = params["#{model.name.underscore}_filter"].try(:first) @records = process_filters(model, filter_params) # page resources if the request is for html. For JSON and XML we will # return the entire recordset @records = @records.page(get_page) respond_with(@records) end |
#new ⇒ Object
GET /resource/new Renders new form for a given resource.
31 32 33 34 35 36 37 |
# File 'app/controllers/app_kit/resources_controller.rb', line 31 def new @record = model.new # call before actions created in the DSL if resource.before_actions[:new] resource.before_actions[:new].call(@record) end end |
#perform_action ⇒ Object
GET /resource/:id/:action_name A catchall action for any custom actions defined in the DSL. The action name is passed by the route as a param and a block given in the DSL is called (with the record instance).
Actions can be defined using blocks are a symbol name of a method in the model.
114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/controllers/app_kit/resources_controller.rb', line 114 def perform_action action_name = params[:action_name].to_sym action = resource.member_actions[action_name] if action.is_method_action? @record.send(action.method_name) end if action.is_block_action? action.block.call(@record) end return redirect_to([app_kit, @record]) end |
#show ⇒ Object
GET /resource/:id Shows the details of a given resource.
57 58 59 |
# File 'app/controllers/app_kit/resources_controller.rb', line 57 def show instance_exec(&resource.before_actions[:show]) if resource.before_actions[:show] end |
#show_version ⇒ Object
101 102 103 104 105 |
# File 'app/controllers/app_kit/resources_controller.rb', line 101 def show_version version = PaperTrail::Version.find(params[:version_id]) @record = version.reify render 'show' end |
#update ⇒ Object
PATCH /resource/:id Updates a given resource if valid, renders the edit form if not.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/controllers/app_kit/resources_controller.rb', line 70 def update flash[:success] = "Record updated successfully." # call before actions created in the DSL if resource.before_actions[:update] resource.before_actions[:update].call(@record) end if @record.update(record_params) redirect_to polymorphic_path([app_kit, @record]) else render 'edit' end end |
#version ⇒ Object
95 96 97 98 99 |
# File 'app/controllers/app_kit/resources_controller.rb', line 95 def version version_index = params[:version_id].to_i @record = model.find_by_id(params[:id]).versions[version_index].reify(dup:true) render 'show' end |