Commit 4eb7dd4f

mo khan <mo@mokhan.ca>
2014-10-17 02:43:15
remove usage of find cake by id query object.
1 parent f2eef36
app/controllers/creations_controller.rb
@@ -4,11 +4,7 @@ class CreationsController < ApplicationController
   end
 
   def show
-    @creation = FindCreationQuery.new.fetch(params[:id])
-    if params[:photo_id].present?
-      @primary_image = @creation.photos.find(params[:photo_id])
-    else
-      @primary_image = @creation.primary_image
-    end
+    @creation = Creation.find(params[:id])
+    @primary_image = params[:photo_id].present? ?  @creation.photos.find(params[:photo_id]) : @creation.primary_image
   end
 end
app/controllers/favorites_controller.rb
@@ -7,7 +7,7 @@ class FavoritesController < ApplicationController
   end
 
   def index
-    @creation = FindCreationQuery.new.fetch(params[:creation_id])
+    @creation = Creation.find(params[:creation_id])
     @favorites = @creation.favorites
   end
 
app/services/application/find_creation_query.rb
@@ -1,9 +0,0 @@
-class FindCreationQuery
-  def initialize(repository = CreationRepository.new)
-    @repository = repository
-  end
-
-  def fetch(id)
-    @repository.find(id)
-  end
-end
spec/controllers/creations_controller_spec.rb
@@ -1,26 +1,32 @@
 require 'rails_helper'
 
 describe CreationsController do
-  let(:user){ create(:user) }
-  let(:creation){ create(:creation, :user => user) }
+  let(:user) { create(:user) }
+  let(:cake) { create(:cake, user: user) }
 
   before(:each) do
     photo = 'spec/fixtures/images/example.png'
-    creation.photos.create(image: photo, image_processing: nil)
+    cake.photos.create(image: photo)
   end
 
   describe "#index" do
     before { get :index }
 
     it "should display all creations" do
-      assigns(:creations).should include(creation)
+      expect(assigns(:creations)).to include(cake)
     end
   end
 
   describe "#show" do
-    it "assigns the requested creation" do
-      get :show, :id => creation.id
-      assigns(:creation).should == creation
+    it "loads the cake" do
+      get :show, id: cake.id
+      expect(assigns(:creation)).to eql(cake)
+    end
+
+    it 'loads the selected image' do
+      photo = cake.photos.first
+      get :show, id: cake.id, photo_id: photo.id
+      expect(assigns(:primary_image)).to eql(photo)
     end
   end
 end