Class: AppKit::ResourcesController

Inherits:
ApplicationController show all
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

Instance Method Details

#createObject

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.full_messages.inspect
    render 'new'
  end
end

#destroyObject

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

#editObject

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

#historyObject



93
# File 'app/controllers/app_kit/resources_controller.rb', line 93

def history; end

#indexObject

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

#newObject

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_actionObject

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

#showObject

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_versionObject



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

#updateObject

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

#versionObject



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