Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented decent exposure gem for nested resource Widget.

  • Loading branch information...
commit bedea5462fa2a1273d64192cec3906a9eba96b61 1 parent 54df61c
@philmill authored
View
1  .gitignore
@@ -2,3 +2,4 @@
db/*.sqlite3
log/*.log
tmp/
+.rvmrc
View
1  .rvmrc
@@ -1 +0,0 @@
-rvm --create use default@rails309
View
2  Gemfile
@@ -6,8 +6,8 @@ gem 'rails', '3.0.9'
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
-#gem 'memcache-client'
gem 'dalli'
+gem 'decent_exposure'
# Use unicorn as the web server
# gem 'unicorn'
View
2  Gemfile.lock
@@ -33,6 +33,7 @@ GEM
builder (2.1.2)
columnize (0.3.3)
dalli (1.0.5)
+ decent_exposure (1.0.1)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
@@ -86,6 +87,7 @@ PLATFORMS
DEPENDENCIES
dalli
+ decent_exposure
rails (= 3.0.9)
ruby-debug-base19x
ruby-debug-ide (= 0.4.17.beta.5)
View
101 app/controllers/widgets_controller.rb
@@ -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
View
6 app/views/widgets/_form.html.erb
@@ -1,10 +1,10 @@
-<%= form_for([@user, @widget]) do |f| %>
- <% if @widget.errors.any? %>
+<%= form_for([user, widget]) do |f| %>
+ <% if widget.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@widget.errors.count, "error") %> prohibited this widget from being saved:</h2>
<ul>
- <% @widget.errors.full_messages.each do |msg| %>
+ <% widget.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
View
4 app/views/widgets/edit.html.erb
@@ -2,6 +2,6 @@
<%= render 'form' %>
-<%= link_to 'Show', [@user, @widget] %> |
-<%= link_to 'Back', user_widgets_path(@user) %>
+<%= link_to 'Show', [user, widget] %> |
+<%= link_to 'Back', user_widgets_path(user) %>
View
16 app/views/widgets/index.html.erb
@@ -1,4 +1,4 @@
-<h1>Listing <%= @user.name %> widgets</h1>
+<h1>Listing <%= user.name %> widgets</h1>
<table>
<tr>
@@ -9,19 +9,19 @@
<th></th>
</tr>
-<% @widgets.each do |widget| %>
+<% widgets.each do |w| %>
<tr>
- <td><%= widget.title %></td>
- <td><%= widget.description %></td>
- <td><%= link_to 'Show', user_widget_path(@user,widget) %></td>
- <td><%= link_to 'Edit', edit_user_widget_path(@user, widget) %></td>
- <td><%= link_to 'Destroy', user_widget_path(@user, widget), :confirm => 'Are you sure?', :method => :delete %></td>
+ <td><%= w.title %></td>
+ <td><%= w.description %></td>
+ <td><%= link_to 'Show', user_widget_path(user,w) %></td>
+ <td><%= link_to 'Edit', edit_user_widget_path(user, w) %></td>
+ <td><%= link_to 'Destroy', user_widget_path(user, w), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
-<%= link_to 'New Widget', new_user_widget_path(@user) %>
+<%= link_to 'New Widget', new_user_widget_path(user) %>
<%= link_to 'Back', users_path %>
View
2  app/views/widgets/new.html.erb
@@ -2,5 +2,5 @@
<%= render 'form' %>
-<%= link_to 'Back', user_widgets_path(@user) %>
+<%= link_to 'Back', user_widgets_path(user) %>
View
8 app/views/widgets/show.html.erb
@@ -2,15 +2,15 @@
<p>
<b>Title:</b>
- <%= @widget.title %>
+ <%= widget.title %>
</p>
<p>
<b>Description:</b>
- <%= @widget.description %>
+ <%= widget.description %>
</p>
-<%= link_to 'Edit', edit_user_widget_path(@widget) %> |
-<%= link_to 'Back', user_widgets_path(@user) %>
+<%= link_to 'Edit', edit_user_widget_path(user,widget) %> |
+<%= link_to 'Back', user_widgets_path(user) %>
Please sign in to comment.
Something went wrong with that request. Please try again.