Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented decent exposure gem for nested resource Widget.
- Loading branch information
Showing
10 changed files
with
51 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
db/*.sqlite3 | ||
log/*.log | ||
tmp/ | ||
.rvmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,50 @@ | ||
class WidgetsController < ApplicationController | ||
|
||
expose(:user) { User.find(params[:user_id]) } | ||
expose(:widgets) { user.widgets } | ||
expose(:widget) | ||
|
||
before_filter :grab_user_from_user_id | ||
cache_sweeper :user_sweeper, :only => [ :create, :update, :delete ] | ||
|
||
# GET /widgets | ||
# GET /widgets.xml | ||
def index | ||
@widgets = @user.widgets.all | ||
|
||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.xml { render :xml => @widgets } | ||
end | ||
# GET /widgets | ||
def index | ||
end | ||
|
||
# GET /widgets/1 | ||
# GET /widgets/1.xml | ||
def show | ||
@widget = @user.widgets.find(params[:id]) | ||
|
||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.xml { render :xml => @widget } | ||
end | ||
# GET /widgets/1 | ||
def show | ||
end | ||
|
||
# GET /widgets/new | ||
# GET /widgets/new.xml | ||
def new | ||
@widget = @user.widgets.new | ||
|
||
respond_to do |format| | ||
format.html # new.html.erb | ||
format.xml { render :xml => @widget } | ||
end | ||
# GET /widgets/new | ||
def new | ||
end | ||
|
||
# GET /widgets/1/edit | ||
def edit | ||
@widget = @user.widgets.find(params[:id]) | ||
def edit | ||
end | ||
|
||
# POST /widgets | ||
# POST /widgets.xml | ||
def create | ||
@widget = @user.widgets.new(params[:widget]) | ||
|
||
respond_to do |format| | ||
if @widget.save | ||
format.html { redirect_to([@user, @widget], :notice => 'Widget was successfully created.') } | ||
format.xml { render :xml => @widget, :status => :created, :location => @widget } | ||
else | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @widget.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
# POST /widgets | ||
def create | ||
if widget.save | ||
redirect_to([user, widget], :notice => 'Widget was successfully created.') | ||
else | ||
render :action => "new" | ||
end | ||
end | ||
|
||
# PUT /widgets/1 | ||
# PUT /widgets/1.xml | ||
def update | ||
@widget = @user.widgets.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @widget.update_attributes(params[:widget]) | ||
format.html { redirect_to([@user, @widget], :notice => 'Widget was successfully updated.') } | ||
format.xml { head :ok } | ||
else | ||
format.html { render :action => "edit" } | ||
format.xml { render :xml => @widget.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
# PUT /widgets/1 | ||
def update | ||
if widget.update_attributes(params[:widget]) | ||
redirect_to([user, widget], :notice => 'Widget was successfully updated.') | ||
else | ||
render :action => "edit" | ||
end | ||
end | ||
|
||
# DELETE /widgets/1 | ||
# DELETE /widgets/1.xml | ||
# DELETE /widgets/1 | ||
def destroy | ||
Widget.find(params[:id]).destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to(user_widgets_url(@user)) } | ||
format.xml { head :ok } | ||
end | ||
end | ||
|
||
private | ||
|
||
def grab_user_from_user_id | ||
@user = User.find(params[:user_id]) | ||
widget.destroy | ||
redirect_to(user_widgets_url(@user)) | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters