Commit 48d47386

mo khan <mo@mokhan.ca>
2014-05-29 04:07:10
split photos controller between public and logged in controllers.
1 parent 03e3808
app/controllers/my/photos_controller.rb
@@ -0,0 +1,61 @@
+module My
+  class PhotosController < BaseController
+    before_filter :find_creation
+
+    def index
+      @photos = @cake.photos
+      render json: { files: @photos.map { |photo| PhotoToJQJsonMapper.new.map_from(photo) } }
+    end
+
+    def new
+      @photo = Photo.new
+    end
+
+    def create
+      attributes = photo_params
+      if params[:photo][:image].class == Array
+        attributes[:image] = params[:photo][:image].first
+      end
+
+      @photo = @cake.photos.build(attributes)
+      if @photo.save
+        render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json
+      else
+        render :json => [{:error => "oops... we're sorry but we weren't able to upload your photo."}], :status => 304
+      end
+    end
+
+    def destroy
+      @photo = @cake.photos.find(params[:id])
+      if @photo.destroy
+        @cake.touch
+        render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json
+      else
+        render :json => [{:error => "could not remove the photo"}], :status => 304
+      end
+    end
+
+    private
+
+    def find_creation
+      @cake = current_user.creations.find(params[:cake_id])
+      raise ActiveRecord::RecordNotFound unless @cake
+    end
+
+    def photo_params
+      params.require(:photo).permit(:image)
+    end
+  end
+end
+
+class PhotoToJQJsonMapper
+  def map_from(photo)
+    {
+      :name => photo.read_attribute(:image),
+      :url => photo.image.url,
+      :thumbnail_url => photo.is_processed? ? photo.image.thumb.url : photo.image.thumb.default_url,
+      :delete_url => photo.id,
+      :delete_type => "DELETE"
+    }
+  end
+end
app/controllers/creations_controller.rb
@@ -26,7 +26,7 @@ class CreationsController < ApplicationController
 
   def create_cake_succeeded(cake)
     @creation = cake
-    redirect_to new_creation_photo_url(@creation)
+    redirect_to new_my_cake_photo_path(@creation)
   end
 
   def create_cake_failed(cake)
@@ -41,7 +41,7 @@ class CreationsController < ApplicationController
 
   def update_cake_succeeded(cake)
     @creation = cake
-    redirect_to new_creation_photo_url(@creation)
+    redirect_to new_my_cake_photo_path(@creation)
   end
 
   def update_cake_failed(cake)
@@ -52,7 +52,7 @@ class CreationsController < ApplicationController
 
   def destroy
     RemoveCakeCommand.new(self).run(params[:id])
-    redirect_to(creations_url)
+    redirect_to my_dashboard_path
   end
 
   private
app/controllers/photos_controller.rb
@@ -1,72 +1,11 @@
 class PhotosController < ApplicationController
-  before_filter :authenticate_user!, :except => [:index, :show]
-  before_filter :find_creation, :except => [:index, :show]
-
   def index
     @creation = Creation.find(params[:creation_id])
     @photos = @creation.photos
-    respond_to do |format|
-      format.html # index.html.erb
-      format.json { render json: { files: @photos.map { |photo| PhotoToJQJsonMapper.new.map_from(photo) } } }
-    end
   end
 
   def show
     @creation = Creation.find(params[:creation_id])
     @photo = @creation.photos.find(params[:id])
   end
-
-  def new
-    @photo = Photo.new
-  end
-
-  def create
-    attributes = photo_params
-    if params[:photo][:image].class == Array
-      attributes[:image] = params[:photo][:image].first
-    end
-
-    @photo = @creation.photos.build(attributes)
-    if @photo.save
-      respond_to do |format|
-        format.html { render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json, :content_type => 'text/html', :layout => false }
-        format.json { render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json }
-      end
-    else
-      render :json => [{:error => "oops... we're sorry but we weren't able to upload your photo."}], :status => 304
-    end
-  end
-
-  def destroy
-    @photo = @creation.photos.find(params[:id])
-    if @photo.destroy
-      @creation.touch
-      render :json => {files: [PhotoToJQJsonMapper.new.map_from(@photo)]}.to_json
-    else
-      render :json => [{:error => "could not remove the photo"}], :status => 304
-    end
-  end
-
-  private
-
-  def find_creation
-    @creation = current_user.creations.find(params[:creation_id])
-    raise ActiveRecord::RecordNotFound unless @creation
-  end
-
-  def photo_params
-    params.require(:photo).permit(:image)
-  end
-
-  class PhotoToJQJsonMapper
-    def map_from(photo)
-      {
-        :name => photo.read_attribute(:image),
-        :url => photo.image.url,
-        :thumbnail_url => photo.is_processed? ? photo.image.thumb.url : photo.image.thumb.default_url,
-        :delete_url => photo.id,
-        :delete_type => "DELETE"
-      }
-    end
-  end
 end
app/views/creations/show.html.erb
@@ -14,7 +14,7 @@
     <%= link_to edit_creation_path(@creation), class: 'btn' do %>
       <i class="icon-edit"></i> <strong>EDIT</strong>
     <% end %>
-    <%= link_to new_creation_photo_path(@creation), class: 'btn' do %>
+    <%= link_to new_my_cake_photo_path(@creation), class: 'btn' do %>
       <i class="icon-plus"></i><strong>ADD IMAGES</strong>
     <% end %>
     <%= link_to creation_favorites_path(:creation_id => @creation.id), class: 'btn' do %>
app/views/my/dashboard/_creation.html.erb
@@ -9,7 +9,7 @@
     <%= link_to edit_creation_path(item) do %>
       <i class="icon-edit"> edit</i>
     <% end %>
-    <%= link_to new_creation_photo_path(item) do %>
+    <%= link_to new_my_cake_photo_path(item) do %>
       <i class="icon-plus"> images</i>
     <% end %>
     <%= link_to creation_favorites_path(:creation_id => item.id) do %>
app/views/photos/_form.html.erb → app/views/my/photos/_form.html.erb
@@ -14,7 +14,7 @@
   });
 </script>
 <% end %>
-  <%= form_for [@creation, @photo], :html => { :multipart => true, :id => "fileupload"  } do |f| %>
+  <%= form_for [@cake, @photo], url: my_cake_photos_path, :html => { :multipart => true, :id => "fileupload"  } do |f| %>
     <div class="row fileupload-buttonbar">
       <div class="span10">&nbsp;</div>
       <div class="span2">
app/views/my/photos/new.html.erb
@@ -0,0 +1,8 @@
+<% provide(:title, "Upload images for this cake") -%>
+<div class="row">
+  <div class="span12">
+    <h1><small>Upload images of </small> <%= @cake.name %> <small> (Step 2 of 2)</small></h1>
+    <hr>
+    <%= render 'form' %>
+  </div>
+</div>
app/views/photos/new.html.erb
@@ -1,8 +0,0 @@
-<% provide(:title, "Upload images for this creation") -%>
-<div class="row">
-  <div class="span12">
-    <h1><small>Upload images of </small> <%= @creation.name %> <small> (Step 2 of 2)</small></h1>
-    <hr>
-    <%= render 'form' %>
-  </div>
-</div>
config/routes.rb
@@ -12,7 +12,7 @@ Cake::Application.routes.draw do
   end
 
   resources :creations do
-    resources :photos, :only => [:index, :show, :new, :create, :destroy]
+    resources :photos, only: [:index, :show]
     resources :favorites, :only => [:index, :create]
     get 'page/:page', :action => :index, :on => :collection
   end
@@ -59,7 +59,9 @@ Cake::Application.routes.draw do
 
   namespace :my do
     get 'dashboard', to: 'dashboard#index'
-    resources :cakes, only: [:index]
+    resources :cakes, only: [:index] do
+      resources :photos, only: [:index, :new, :create, :destroy]
+    end
     resources :favorites, only: [:index]
     resources :settings, only: [:index, :update]
     resources :passwords, only: [:index, :update]
spec/controllers/my/photos_controller_spec.rb
@@ -0,0 +1,63 @@
+require 'spec_helper'
+
+module My
+  describe PhotosController do
+    let(:user){ create(:user) }
+    let(:cake){ create(:creation) }
+
+    before(:each) do
+      user.creations << cake
+      http_login(user)
+    end
+
+    describe :post do
+      let(:image) { Rack::Test::UploadedFile.new("spec/fixtures/images/gorilla.jpg", "image/jpeg") }
+
+      before :each do
+        post :create, :cake_id => cake.id, :photo => { :image => image }
+      end
+
+      it "returns http success" do
+        response.should be_success
+      end
+
+      it "should upload a new photo" do
+        assigns(:photo).should_not be_nil
+        assigns(:photo).image.to_s.should include("gorilla.jpg")
+      end
+    end
+
+    describe :delete do
+      let!(:photo) { create(:photo) }
+
+      before :each do
+        cake.photos << photo
+        cake.save!
+        photo.update_attribute(:image_processing, nil)
+        delete :destroy, :cake_id => cake.id, :id => photo.id
+      end
+
+      it "returns http success" do
+        response.should be_success
+      end
+
+      it "should destroy the photo" do
+        Photo.exists?(photo.id).should be_false
+      end
+
+      it "should respond with the proper json" do
+        response.body.should ==
+          {
+          :files => [
+            {
+              :name => "example.png",
+              :url => "/uploads/photo/image/#{photo.id}/example.png",
+              :thumbnail_url => "/uploads/photo/image/#{photo.id}/thumb_example.png",
+              :delete_url => photo.id,
+              :delete_type => "DELETE"
+            }]
+        }.to_json
+      end
+    end
+  end
+end
spec/controllers/creations_controller_spec.rb
@@ -73,7 +73,7 @@ describe CreationsController do
         end
 
         it "redirects to the created creation" do
-          response.should redirect_to(new_creation_photo_path(assigns(:creation)))
+          response.should redirect_to(new_my_cake_photo_path(assigns(:creation)))
         end
       end
 
@@ -99,7 +99,7 @@ describe CreationsController do
         end
 
         it "redirects to the creation" do
-          response.should redirect_to new_creation_photo_path(creation.reload)
+          response.should redirect_to new_my_cake_photo_path(creation.reload)
         end
       end
 
@@ -130,7 +130,7 @@ describe CreationsController do
       end
 
       it "redirects to the creations list" do
-        response.should redirect_to(creations_url)
+        response.should redirect_to(my_dashboard_path)
       end
     end
   end
spec/controllers/photos_controller_spec.rb
@@ -1,61 +1,36 @@
 require 'spec_helper'
 
 describe PhotosController do
-  let(:user){ create(:user) }
   let(:creation){ create(:creation) }
 
-  before(:each) do
-    user.creations << creation
-    http_login(user)
-  end
-
-  describe :post do
-    let(:image) { Rack::Test::UploadedFile.new("spec/fixtures/images/gorilla.jpg", "image/jpeg") }
-
+  describe "#index" do
     before :each do
-      post :create, :creation_id => creation.id, :photo => { :image => image }
+      get :index, creation_id: creation.id
     end
 
-    it "returns http success" do
-      response.should be_success
+    it "loads the creation" do
+      assigns(:creation).should == creation
     end
 
-    it "should upload a new photo" do
-      assigns(:photo).should_not be_nil
-      assigns(:photo).image.to_s.should include("gorilla.jpg")
+    it "loads the photos" do
+      assigns(:photos).should == creation.photos
     end
   end
 
-  describe :delete do
-    let!(:photo) { create(:photo) }
+  describe "#show" do
+    let(:photo) { create(:photo) }
 
     before :each do
-      creation.photos << photo
-      creation.save!
-      photo.update_attribute(:image_processing, nil)
-      delete :destroy, :creation_id => creation.id, :id => photo.id
-    end
-
-    it "returns http success" do
-      response.should be_success
+      creation.photos.push(photo)
+      get :show, creation_id: creation.id, id: photo.id
     end
 
-    it "should destroy the photo" do
-      Photo.exists?(photo.id).should be_false
+    it "loads the cake" do
+      assigns(:creation).should == creation
     end
 
-    it "should respond with the proper json" do
-      response.body.should ==
-      {
-        :files => [
-        {
-           :name => "example.png",
-           :url => "/uploads/photo/image/#{photo.id}/example.png",
-           :thumbnail_url => "/uploads/photo/image/#{photo.id}/thumb_example.png",
-           :delete_url => photo.id,
-           :delete_type => "DELETE"
-        }]
-      }.to_json
+    it "loads the photo" do
+      assigns(:photo).should == photo
     end
   end
 end