Skip to content

Instantly share code, notes, and snippets.

@skanev
Created December 16, 2009 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skanev/257946 to your computer and use it in GitHub Desktop.
Save skanev/257946 to your computer and use it in GitHub Desktop.
class TopicsController < ApplicationController
before_filter :set_topic, :only => [:show, :edit, :update, :destroy]
def index
@topics = current_user.topics
end
def show
@topic.update_topic_hits
@topic_hits = @topic.topic_hits.paginate(:page => params[:page] || 1, :per_page => params[:per_page])
end
def new
@topic = Topic.new
end
def edit
end
def create
@topic = Topic.new(params[:topic])
@topic.user = current_user
if @topic.save
flash[:notice] = 'News Monitor was successfully created.'
redirect_to @topic
else
render :action => "new"
end
end
def update
if @topic.update_attributes(params[:topic])
flash[:notice] = 'News Monitor was successfully updated.'
redirect_to @topic
else
render :action => "edit"
end
end
def destroy
@topic = Topic.find(params[:id])
@topic.destroy
redirect_to(topics_url)
end
protected
def set_topic
@topic = Topic.find(params[:id])
access_denied unless @topic.user == current_user
end
end
require 'spec_helper'
describe TopicsController do
log_in_as { Factory(:user) }
before(:each) { @user = controller.send(:current_user) }
describe "GET /index" do
it "should assign the current user's topics to @topics" do
controller.stub! :current_user => mock('current user', :topics => 'news-monitors')
get :index
assigns[:topics].should == 'news-monitors'
end
end
describe "GET /show" do
before(:each) do
@topic = mock_model(Topic, :user => @user, :topic_hits => [])
@topic.stub! :update_topic_hits
Topic.should_receive(:find).with(@topic.id.to_s).and_return @topic
end
it "should assign @topic" do
get :show, :id => @topic.id
assigns[:topic].should == @topic
end
it "should tell the Topic to update topic_hits" do
@topic.should_receive(:update_topic_hits)
get :show, :id => @topic.id
end
it "should assign @topic_hits" do
@topic.should_receive(:topic_hits).and_return %w(one two)
get :show, :id => @topic.id
assigns[:topic_hits].should == %w(one two)
end
it "should restrict access to owner only" do
@topic.stub! :user => Factory(:user)
get :show, :id => @topic.id
response.should deny_access
end
end
describe "GET /new" do
it "should assign @topic to a new record" do
Topic.should_receive(:new).and_return 'new-news-monitor'
get :new
assigns[:topic].should == 'new-news-monitor'
end
end
describe "POST /create" do
before(:each) do
@topic = mock_model(Topic)
@topic.stub! :user=
Topic.stub! :new => @topic
@topic.stub! :save
end
it "should assign @topic with a Topic created from params[:topic]" do
Topic.should_receive(:new).with('news-monitor-attributes').and_return @topic
post :create, :topic => 'news-monitor-attributes'
assigns[:topic].should == @topic
end
it "should assign the Topic's user to the current user" do
@topic.should_receive(:user=).with(@user)
post :create
end
context "when successful" do
it "should redirect to the newly created topic" do@topic.stub! :save => true
@topic.stub! :save => true
post :create
response.should redirect_to(topic_path(@topic))
end
end
context "when unsuccessful" do
it "should render 'new" do
@topic.stub! :save => false
post :create
response.should render_template(:new)
end
end
end
describe "GET /edit" do
before(:each) do
@topic = mock_model(Topic, :user => @user)
Topic.should_receive(:find).with(@topic.id.to_s).and_return @topic
end
it "should assign @topic" do
get :edit, :id => @topic.id
assigns[:topic].should == @topic
end
it "should restrict access to owner only" do
@topic.stub! :user => Factory(:user)
get :edit, :id => @topic.id
response.should deny_access
end
end
describe "PUT /update" do
before(:each) do
@topic = mock_model(Topic, :user => @user)
Topic.should_receive(:find).with(@topic.id.to_s).and_return @topic
@topic.stub! :update_attributes
end
it "should assign @topic" do
put :update, :id => @topic.id
assigns[:topic].should == @topic
end
it "should update with params[:topic]" do
@topic.should_receive(:update_attributes).with('news-monitor-attributes')
put :update, :id => @topic.id, :topic => 'news-monitor-attributes'
end
it "should restrict access to owner only" do
@topic.stub! :user => Factory(:user)
put :update, :id => @topic.id
@topic.should_not_receive(:update_attributes)
response.should deny_access
end
context "when successful" do
before(:each) do
@topic.stub! :update_attributes => true
end
it "should redirect to the topic" do
put :update, :id => @topic.id
response.should redirect_to(topic_path(@topic))
end
end
context "when unsuccessful" do
before(:each) do
@topic.stub! :update_attributes => false
end
it "should render 'edit'" do
put :update, :id => @topic.id
response.should render_template(:edit)
end
end
end
describe "DELETE /destroy" do
before(:each) do
@topic = mock_model(Topic, :user => @user)
Topic.stub!(:find).with(@topic.id.to_s).and_return @topic
@topic.stub!(:destroy)
end
it "should destroy the topic" do
@topic.should_receive(:destroy)
delete :destroy, :id => @topic.id
end
it "should redirect to topics_path" do
delete :destroy, :id => @topic.id
response.should redirect_to(topics_path)
end
it "should restrict access to owner only" do
controller.stub! :current_user => Factory(:user)
delete :destroy, :id => @topic.id
response.should deny_access
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment