Commit 50b94a4f

luu <luuduong@gmail.com>
2014-12-13 16:41:51
add tool only if it is not in the toolbox.
1 parent 0a61f52
Changed files (3)
app
controllers
views
admin
spec
app/controllers/admin/products_controller.rb
@@ -1,5 +1,7 @@
 module Admin
   class ProductsController < AdminController
+    attr_reader :product_api
+
     def initialize(product_api = Spank::IOC.resolve(:product_api))
       @product_api = product_api
       super()
@@ -10,12 +12,13 @@ module Admin
     end
 
     def show
-      @product = @product_api.find(params[:id])
+      @product = product_api.find(params[:id])
+      @tool = Tool.find_by(:asin=>params[:id])
     end
 
     def create
       Tool.create(:name=>params[:name],:asin=>params[:asin])
-      redirect_to admin_products_path(params[:asin])
+      redirect_to admin_product_path(params[:asin])
     end
   end
 end
app/views/admin/products/show.html.erb
@@ -22,10 +22,14 @@
         Add to tools:
       </td>
       <td>
-        <%= form_tag admin_products_path do %>
-          <%= text_field_tag :name, @product.item_attributes['title'] %>
-          <%= hidden_field_tag "asin", @product.asin %>
-          <%= submit_tag "Add to tools" %>
+        <% if @tool.present? %>
+          In the toolbox as <%= @tool.name %>
+        <% else %>        
+          <%= form_tag admin_products_path do %>
+            <%= text_field_tag :name, @product.item_attributes['title'] %>
+            <%= hidden_field_tag "asin", @product.asin %>
+            <%= submit_tag "Add to tools" %>
+          <% end %>
         <% end %>
       </td>
     </tr>  
spec/controllers/admin/products_controller_spec.rb
@@ -1,13 +1,48 @@
 require 'rails_helper'
+
 module Admin
   describe ProductsController do 
-    describe "#create" do
-      let(:admin) { create(:admin)}
+    let(:admin) { create(:admin)}
+
+    before :each do
+      http_login(admin)
+    end
+
+    describe "#show" do
+      let(:product_api) { double(find: true) }
 
       before :each do
-        http_login(admin)
+        controller.stub(:product_api).and_return(product_api)
+      end
+
+      it 'loads the product details from amazon' do
+        asin = 'asin'
+        product = "product"
+        product_api.stub(:find).with(asin).and_return(product)
+
+        get :show, id: asin
+
+        expect(assigns(:product)).to eql(product)
+      end
+
+      context "when the tool has been added to the toolbox" do
+        let(:tool) { create(:tool) }
+
+        it "loads the tool" do
+          get :show, id: tool.asin
+          expect(assigns(:tool)).to eql(tool)
+        end
       end
 
+      context "when the tool is not in the toolbox" do
+        it "does not load a tool" do
+          get :show, id: "not_added"
+          expect(assigns(:tool)).to be_nil
+        end
+      end
+    end
+
+    describe "#create" do
       it "creates new tool" do
         post :create, {:name=>"pan", :asin=>"34234"}
 
@@ -18,7 +53,7 @@ module Admin
 
       it "redirects back to the detail page" do
         post :create, {name: 'blah', asin: 'blah' }
-        expect(response).to redirect_to(admin_products_path('blah'))
+        expect(response).to redirect_to(admin_product_path('blah'))
       end
     end
   end