Module: Coast

Defined in:
lib/coast.rb,
lib/coast/version.rb

Overview

Makes any controller resourceful by providing the following actions:

  • new

  • edit

  • index

  • show

  • create

  • update

  • destroy

There are 3 callbacks that you can leverage to manage the RESTful behavior:

  • before - Happens before any logic, just like a Rails before_filter.

    Note that this callback is invoked before any authorization is applied
    
  • respond_to - Happens after CRUD operations but before rendering a response

  • after - Happens after any logic, just like a Rails after_filter

You can hook into the controller’s lifecycle like so:

before([action]) do
  # logic here
end

respond_to([action]) do
  # logic here
end

after([action]) do
  # logic here
end

Resourceful leans heavily on Rails naming conventions. If you are using Rails naming convetions, all this power is yours for free.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



87
88
89
# File 'lib/coast.rb', line 87

def self.included(mod)
  mod.extend(ClassMethods)
end

Instance Method Details

#abstract_authorize(*args) ⇒ Object



91
# File 'lib/coast.rb', line 91

def abstract_authorize(*args); end

#createObject

begin MUTATING actions



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/coast.rb', line 161

def create
  invoke_callback(:before_create)
  @resourceful_item ||= resourceful_model.new(params[resourceful_model.name.underscore])
  send(self.class.authorize_method, :create, @resourceful_item, request)
  init_instance_variables
  success = @skip_db_create || @resourceful_item.valid? && @resourceful_item.save
  invoke_callback(:respond_to_create)
  unless performed?
    respond_to do |format|
      if success
        write_to_flash :notice, "#{resourceful_model.name} was successfully created."
        format.html { redirect_to(@resourceful_item) }
        format_json_and_xml(format, @resourceful_item, :status => :created, :location => @resourceful_item)
      else
        format.html { render :action => "new" }
        format_json_and_xml(format, @resourceful_item.errors, :status => :unprocessable_entity)
      end
    end
  end
  invoke_callback(:after_create)
end

#destroyObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/coast.rb', line 205

def destroy
  invoke_callback(:before_destroy)
  @resourceful_item ||= resourceful_model.find(params[:id])
  send(self.class.authorize_method, :destroy, @resourceful_item, request)
  init_instance_variables
  @resourceful_item.destroy unless @skip_db_destroy
  invoke_callback(:respond_to_destroy)
  unless performed?
    write_to_flash(:notice, "#{resourceful_model.name} was successfully destroyed.") if @resourceful_item.destroyed?
    respond_to do |format|
      format.html { redirect_to root_url }
      format_json_and_xml(format, @resourceful_item)
    end
  end
  invoke_callback(:after_destroy)
end

#editObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/coast.rb', line 112

def edit
  invoke_callback(:before_edit)
  @resourceful_item ||= resourceful_model.find(params[:id])
  send(self.class.authorize_method, :edit, @resourceful_item, request)
  init_instance_variables
  invoke_callback(:respond_to_edit)
  unless performed?
    respond_to do |format|
      format.html { render :edit }
      format_json_and_xml(format, :message => "Format not supported! Use the html format.")
    end
  end
  invoke_callback(:after_edit)
end

#indexObject

begin READ actions



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/coast.rb', line 129

def index
  invoke_callback(:before_index)
  @resourceful_list ||= resourceful_model.all
  send(self.class.authorize_method, :index, @resourceful_list, request)
  init_instance_variables
  invoke_callback(:respond_to_index)
  unless performed?
    respond_to do |format|
      format.html { render :index }
      format_json_and_xml(format, @resourceful_list)
    end
  end
  invoke_callback(:after_index)
end

#newObject

begin UI actions



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/coast.rb', line 97

def new
  invoke_callback(:before_new)
  @resourceful_item ||= resourceful_model.new
  send(self.class.authorize_method, :new, @resourceful_item, request)
  init_instance_variables
  invoke_callback(:respond_to_new)
  unless performed?
    respond_to do |format|
      format.html { render :new }
      format_json_and_xml(format, :message => "Format not supported! Use the html format.")
    end
  end
  invoke_callback(:after_new)
end

#showObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/coast.rb', line 144

def show
  invoke_callback(:before_show)
  @resourceful_item ||= resourceful_model.find(params[:id])
  send(self.class.authorize_method, :show, @resourceful_item, request)
  init_instance_variables
  invoke_callback(:respond_to_show)
  unless performed?
    respond_to do |format|
      format.html { render :show }
      format_json_and_xml(format, @resourceful_item)
    end
  end
  invoke_callback(:after_show)
end

#updateObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/coast.rb', line 183

def update
  invoke_callback(:before_update)
  @resourceful_item ||= resourceful_model.find(params[:id])
  send(self.class.authorize_method, :update, @resourceful_item, request)
  init_instance_variables
  success = @skip_db_update || @resourceful_item.update_attributes(params[resourceful_model.name.underscore])
  invoke_callback(:respond_to_update)
  unless performed?
    respond_to do |format|
      if success
        write_to_flash :notice, "#{resourceful_model.name} was successfully updated."
        format.html { redirect_to(@resourceful_item) }
        format_json_and_xml(format, @resourceful_item)
      else
        format.html { render :action => "edit" }
        format_json_and_xml(format, @resourceful_item.errors, :status => :unprocessable_entity)
      end
    end
  end
  invoke_callback(:after_update)
end