Module: ResourcesController::Actions

Included in:
SingletonActions
Defined in:
lib/resources_controller/actions.rb

Overview

standard CRUD actions, with html, js and xml responses, re-written to mnake best use of resources_cotroller. This helps if you’re writing controllers that you want to share via mixin or inheritance.

This module is used as the actions for the controller by default, but you can change this behaviour:

resources_controller_for :foos, :actions_include => false               # don't include any actions
resources_controller_for :foos, :actions_include => Some::Other::Module # use this module instead

Why?

The idea is to decouple the model name from the action code.

Here’s how:

finding and making new resources

Instead of this:

@post = Post.find(params[:id])
@post = Post.new
@posts = Post.find(:all)

do this:

self.resource = find_resource
self.resource = new_resource
self.resources = find_resources

referring to resources

Instead of this:

format.xml { render :xml => @post }
format.xml { render :xml => @posts }

do this:

format.xml { render :xml => resource }
format.xml { render :xml => resources }

urls

Instead of this:

redirect_to posts_url
redirect_to new_post_url

do this:

redirect_to resources_url
redirect_to new_resource_url

Instance Method Summary collapse

Instance Method Details

#createObject

POST /events POST /events.xml



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/resources_controller/actions.rb', line 93

def create
  self.resource = new_resource
  
  respond_to do |format|
    if resource.save
      format.html do
        flash[:notice] = "#{resource_name.humanize} was successfully created."
        redirect_to resource_url
      end
      format.js
      format.xml  { render :xml => resource, :status => :created, :location => resource_url }
    else
      format.html { render :action => "new" }
      format.js   { render :action => "new" }
      format.xml  { render :xml => resource.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /events/1 DELETE /events/1.xml



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/resources_controller/actions.rb', line 135

def destroy
  self.resource = destroy_resource
  respond_to do |format|
    format.html do
      flash[:notice] = "#{resource_name.humanize} was successfully destroyed."
      redirect_to resources_url
    end
    format.js
    format.xml  { head :ok }
  end
end

#editObject

GET /events/1/edit



82
83
84
85
86
87
88
89
# File 'lib/resources_controller/actions.rb', line 82

def edit
  self.resource = find_resource
  respond_to do |format|
    format.html # edit.html.erb
    format.js
    format.xml  { render :xml => resource }
  end
end

#indexObject

GET /events GET /events.xml



48
49
50
51
52
53
54
55
56
# File 'lib/resources_controller/actions.rb', line 48

def index
  self.resources = find_resources

  respond_to do |format|
    format.html # index.rhtml
    format.js
    format.xml  { render :xml => resources }
  end
end

#newObject

GET /events/new



71
72
73
74
75
76
77
78
79
# File 'lib/resources_controller/actions.rb', line 71

def new
  self.resource = new_resource

  respond_to do |format|
    format.html # new.html.erb
    format.js
    format.xml  { render :xml => resource }
  end
end

#showObject

GET /events/1 GET /events/1.xml



60
61
62
63
64
65
66
67
68
# File 'lib/resources_controller/actions.rb', line 60

def show
  self.resource = find_resource

  respond_to do |format|
    format.html # show.erb.html
    format.js
    format.xml  { render :xml => resource }
  end
end

#updateObject

PUT /events/1 PUT /events/1.xml



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/resources_controller/actions.rb', line 114

def update
  self.resource = find_resource
  
  respond_to do |format|
    if resource.update_attributes(params[resource_name])
      format.html do
        flash[:notice] = "#{resource_name.humanize} was successfully updated."
        redirect_to resource_url
      end
      format.js
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.js   { render :action => "edit" }
      format.xml  { render :xml => resource.errors, :status => :unprocessable_entity }
    end
  end
end