Commit a41905de

mo khan <mo@mokhan.ca>
2013-03-02 17:42:04
start to clean up the creations controller
1 parent 4654586
Changed files (3)
app/controllers/creations_controller.rb
@@ -2,7 +2,7 @@ class CreationsController < ApplicationController
   before_filter :authenticate_user!, :except => [:show, :index]
   # GET /creations
   def index
-    @creations = resolve(FindAllCreationsQuery).fetch(params)
+    @creations = FindAllCreationsQuery.new.fetch(params)
   end
 
   # GET /creations/1
app/services/queries/find_all_creations_query.rb
@@ -1,6 +1,5 @@
 class FindAllCreationsQuery
-  def initialize(user, mapper = CreationToDisplayCreationDTOMapper.new)
-    @user = user
+  def initialize(mapper = CreationToDisplayCreationDTOMapper.new)
     @mapper = mapper
   end
   def fetch(params)
spec/controllers/creations_controller_spec.rb
@@ -5,111 +5,119 @@ describe CreationsController do
   let(:creation){ FactoryGirl.create(:creation, :user => user) }
 
   before(:each) do
-    http_login(user)
-    photo = File.new(File.join( Rails.root, 'spec/fixtures/images/example.png')) 
+    photo = File.new(File.join(Rails.root, 'spec/fixtures/images/example.png'))
     creation.add_photo(photo)
   end
 
   describe "GET index" do
-    it "assigns all creations as @creations" do
-      get :index
-      creation = assigns(:creations).first
-      creation.id.should == creation.id
-      creation.name.should == creation.name
-      creation.story.should == creation.story
-      creation.user.should == creation.user
-    end
-  end
+    let(:restricted_creation){ FactoryGirl.create(:creation, :user => user, :is_restricted => true) }
 
-  describe "GET show" do
-    it "assigns the requested creation as @creation" do
-      get :show, :id => creation.id
-      assigns(:creation).should eq(creation)
+    before { get :index }
+
+    it "should display all creations" do
+      assigns(:creations).should include(creation)
     end
-  end
 
-  describe "GET new" do
-    it "assigns a new creation as @creation" do
-      new_creation = fake
-      Creation.stub(:new) { new_creation }
-      get :new
-      assigns(:creation).should be(new_creation)
+    it "should not include restricted creations" do
+      assigns(:creations).should_not include(restricted_creation)
     end
   end
 
-  describe "GET edit" do
-    it "assigns the requested creation as @creation" do
-      get :edit, :id => creation.id
-      assigns(:creation).should eq(creation)
+  context "when logged in" do
+    before(:each) do
+      http_login(user)
     end
-  end
 
-  describe "POST create" do
-    describe "with valid params" do
-      before :each do
-        creations = fake
-        user.stub(:creations).and_return(creations)
-        creations.stub(:create).and_return(creation)
-        post :create, :creation => {:id => creation.id, :name => 'new name'}
-      end
-      it "assigns a newly created creation as @creation" do
+    describe "GET show" do
+      it "assigns the requested creation as @creation" do
+        get :show, :id => creation.id
         assigns(:creation).should eq(creation)
       end
-      pending "redirects to the created creation" do
-        response.should redirect_to(creations_url)
-      end
     end
 
-    describe "with invalid params" do
-      before :each do
-        post :create, :creation => {:name => ''}
-      end
-      it "re-renders the 'new' template" do
-        response.should render_template("new")
-      end
-      it "should include the errors" do
-        assigns(:creation).errors.any?.should be_true
+    describe "GET new" do
+      it "assigns a new creation as @creation" do
+        new_creation = fake
+        Creation.stub(:new) { new_creation }
+        get :new
+        assigns(:creation).should be(new_creation)
       end
     end
-  end
 
-  describe "PUT update" do
-    describe "with valid params" do
-      before :each do
-        put :update, :id => creation.id, :creation => {:name => 'params'}
-      end
+    describe "GET edit" do
       it "assigns the requested creation as @creation" do
+        get :edit, :id => creation.id
         assigns(:creation).should eq(creation)
       end
+    end
 
-      it "redirects to the creation" do
-        response.should redirect_to("/creations/#{creation.id}-params/photos/new")
+    describe "POST create" do
+      describe "with valid params" do
+        before :each do
+          creations = fake
+          user.stub(:creations).and_return(creations)
+          creations.stub(:create).and_return(creation)
+          post :create, :creation => {:id => creation.id, :name => 'new name'}
+        end
+        it "assigns a newly created creation as @creation" do
+          assigns(:creation).should eq(creation)
+        end
+        pending "redirects to the created creation" do
+          response.should redirect_to(creations_url)
+        end
       end
-    end
 
-    describe "with invalid params" do
-      before :each do
-        put :update, :id => creation.id, :creation => {:name=> nil, :category_ids => [] }
+      describe "with invalid params" do
+        before :each do
+          post :create, :creation => {:name => ''}
+        end
+        it "re-renders the 'new' template" do
+          response.should render_template("new")
+        end
+        it "should include the errors" do
+          assigns(:creation).errors.any?.should be_true
+        end
       end
-      it "assigns the creation as @creation" do
-        assigns(:creation).should eq(creation)
+    end
+
+    describe "PUT update" do
+      describe "with valid params" do
+        before :each do
+          put :update, :id => creation.id, :creation => {:name => 'params'}
+        end
+        it "assigns the requested creation as @creation" do
+          assigns(:creation).should eq(creation)
+        end
+
+        it "redirects to the creation" do
+          response.should redirect_to("/creations/#{creation.id}-params/photos/new")
+        end
       end
 
-      it "re-renders the 'edit' template" do
-        response.should render_template("edit")
+      describe "with invalid params" do
+        before :each do
+          put :update, :id => creation.id, :creation => {:name=> nil, :category_ids => [] }
+        end
+        it "assigns the creation as @creation" do
+          assigns(:creation).should eq(creation)
+        end
+
+        it "re-renders the 'edit' template" do
+          response.should render_template("edit")
+        end
       end
     end
-  end
 
-  describe "DELETE destroy" do
-    before :each do
-      delete :destroy, :id => creation.id
-    end
-    it "destroys the requested creation" do
-      user.creations.count.should == 0
-    end
-    it "redirects to the creations list" do
-      response.should redirect_to(creations_url)
+    describe "DELETE destroy" do
+      before :each do
+        delete :destroy, :id => creation.id
+      end
+      it "destroys the requested creation" do
+        user.creations.count.should == 0
+      end
+      it "redirects to the creations list" do
+        response.should redirect_to(creations_url)
+      end
     end
   end
 end