Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created December 3, 2008 16:06
Show Gist options
  • Save snusnu/31596 to your computer and use it in GitHub Desktop.
Save snusnu/31596 to your computer and use it in GitHub Desktop.
# config/router.rb
Merb::Router.prepare do
resources :articles do
resources :comments do
resources :ratings, :controller => "Community::Ratings"
end
resource :editor
end
resources :comments do
resources :ratings
end
end
# app/controllers/community/ratings.rb
module Community
class Ratings < Application
controlling "Community::Rating" do |r|
r.belongs_to [ :article, :comment ]
end
end
end
# specs/article_comment_ratings_spec.rb
require File.dirname(__FILE__) + '/spec_helper'
given "a Comment exists" do
Community::Rating.all.destroy!
Comment.all.destroy!
Article.all.destroy!
request(
resource(:articles),
:method => "POST",
:params => { :article => { :id => nil, :title => "yo", :body => "snusnu" }}
)
request(
resource(Article.first, :comments),
:method => "POST",
:params => { :comment => { :id => nil, :article_id => Article.first.id, :body => "wassup" } }
)
end
given "a Rating exists" do
Community::Rating.all.destroy!
Comment.all.destroy!
Article.all.destroy!
request(
resource(:articles),
:method => "POST",
:params => { :article => { :id => nil, :title => "yo", :body => "snusnu" }}
)
request(
resource(Article.first, :comments),
:method => "POST",
:params => { :comment => { :id => nil, :article_id => Article.first.id, :body => "wassup" } }
)
request(
resource(Article.first, Comment.first, :ratings),
:method => "POST",
:params => { :rating => { :id => nil, :comment_id => Comment.first.id, :rate => 1 } }
)
end
describe "resource(@article, @comment, :ratings)" do
describe "GET", :given => "a Rating exists" do
before(:each) do
@response = request(resource(Article.first, Comment.first, :ratings))
end
it "should respond successfully" do
@response.should be_successful
end
end
describe "a successful POST", :given => "a Comment exists" do
before(:each) do
Community::Rating.all.destroy!
@response = request(resource(Article.first, Comment.first, :ratings),
:method => "POST",
:params => { :rating => { :id => nil, :comment_id => Comment.first.id, :rate => 1 }}
)
end
it "should redirect to resource(@article, @comment, @rating)" do
@response.should redirect_to(
resource(Article.first, Comment.first, Community::Rating.first),
:message => {:notice => "Rating was successfully created"}
)
end
end
end
describe "resource(@article, @comment, @rating)" do
describe "a successful DELETE", :given => "a Rating exists" do
before(:each) do
@response = request(resource(Article.first, Comment.first, Community::Rating.first), :method => "DELETE")
end
it "should redirect to resource(@article, :comments)" do
@response.should redirect_to(resource(Article.first, Comment.first, :ratings))
end
end
end
describe "resource(@article, @comment, :ratings, :new)", :given => "a Comment exists" do
before(:each) do
@response = request(resource(Article.first, Comment.first, :ratings, :new))
end
it "should respond successfully" do
@response.should be_successful
end
end
describe "resource(@article, @comment, @rating, :edit)", :given => "a Rating exists" do
before(:each) do
@response = request(resource(Article.first, Comment.first, Community::Rating.first, :edit))
end
it "should respond successfully" do
@response.should be_successful
end
end
describe "resource(@article, @comment, @rating)", :given => "a Rating exists" do
describe "GET" do
before(:each) do
@response = request(resource(Article.first, Comment.first, Community::Rating.first))
end
it "should respond successfully" do
@response.should be_successful
end
end
describe "PUT" do
before(:each) do
@article = Article.first
@comment = Comment.first
@rating = Community::Rating.first
@response = request(resource(@article, @comment, @rating), :method => "PUT",
:params => { :rating => { :id => @rating.id } })
end
it "should redirect to resource(@article, @comment, @rating)" do
@response.should redirect_to(resource(@article, @comment, @rating))
end
end
end
# rspec output
1)
Merb::Router::GenerationError in 'resource(@article, @comment, :ratings) GET should respond successfully'
Resource route not found: [#<Article id=1 title="yo" body="snusnu" editor_id=nil>, #<Comment id=1 article_id=1 body="wassup">, :community_ratings]
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:34:
2)
Merb::Router::GenerationError in 'resource(@article, @comment, :ratings) a successful POST should redirect to resource(@article, @comment, @rating)'
Resource route not found: [#<Article id=2 title="yo" body="snusnu" editor_id=nil>, #<Comment id=2 article_id=2 body="wassup">, nil]
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:67:
3)
Merb::Router::GenerationError in 'resource(@article, @comment, @rating) a successful DELETE should redirect to resource(@article, :comments)'
Resource route not found: [#<Article id=3 title="yo" body="snusnu" editor_id=nil>, #<Comment id=3 article_id=3 body="wassup">, :community_ratings]
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:34:
4)
'resource(@article, @comment, :ratings, :new) should respond successfully' FAILED
Expected a GET to 'http://example.org/articles/4/comments/4/ratings/new' to be successful, but it returned a 404
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:99:
5)
Merb::Router::GenerationError in 'resource(@article, @comment, @rating, :edit) should respond successfully'
Resource route not found: [#<Article id=5 title="yo" body="snusnu" editor_id=nil>, #<Comment id=5 article_id=5 body="wassup">, :community_ratings]
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:34:
6)
Merb::Router::GenerationError in 'resource(@article, @comment, @rating) GET should respond successfully'
Resource route not found: [#<Article id=6 title="yo" body="snusnu" editor_id=nil>, #<Comment id=6 article_id=6 body="wassup">, :community_ratings]
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:34:
7)
Merb::Router::GenerationError in 'resource(@article, @comment, @rating) PUT should redirect to resource(@article, @comment, @rating)'
Resource route not found: [#<Article id=7 title="yo" body="snusnu" editor_id=nil>, #<Comment id=7 article_id=7 body="wassup">, :community_ratings]
/Users/snusnu/projects/merb_resource_controller/spec/article_comment_ratings_spec.rb:34:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment