Commit 5d83fa78

mo khan <mo@mokhan.ca>
2014-10-29 03:24:18
add products catalog.
1 parent f0c3cb7
app/controllers/products_controller.rb
@@ -0,0 +1,14 @@
+class ProductsController < ApplicationController
+  def initialize(product_api = Spank::IOC.resolve(:product_api))
+    @product_api = product_api
+    super()
+  end
+
+  def index
+    @products = @product_api.search(params[:q])
+  end
+
+  def show
+    @product = @product_api.find(params[:id])
+  end
+end
app/views/products/_index.html.erb
@@ -0,0 +1,12 @@
+<table class="table table-striped">
+  <tbody>
+  <% @products.each do |product| %>
+    <tr>
+      <td>
+        <%= link_to product.item_attributes.title, product.detail_page_url %> is made by <%= product.item_attributes.manufacturer %> in category <%= product.item_attributes.product_group %>
+      </td>
+      <td> <%= link_to 'View on Amazon', product.detail_page_url, class: 'btn', target: "_blank" %> </td>
+    </tr>
+  <% end %>
+  </tbody>
+</table>
app/views/products/index.html.erb
@@ -0,0 +1,14 @@
+<%= provide(:search_path, products_path) %>
+<div class="row-fluid">
+  <div class="span12">
+    <h1>Product Catalog</h1>
+    <%= form_tag products_path(js: true), method: :get, name: 'search', class: 'form-horizontal', remote: true do %>
+      <%= text_field_tag :q, params[:q], class: "search-query", placeholder: "Search" %>
+      <%= submit_tag 'Search', class: 'btn btn-primary' %>
+    <% end %>
+    <div id="results-container">
+      <%= render partial: "index" %>
+    </div>
+  </div>
+</div>
+
app/views/products/index.js.erb
@@ -0,0 +1,1 @@
+$("#results-container").html('<%= escape_javascript(render partial: "index") %>');
app/views/products/show.html.erb
@@ -0,0 +1,47 @@
+<%= provide(:search_path, admin_products_path) %>
+<div class="row-fluid">
+  <div class="span2">
+    <%= render partial: "my/shared/my_nav" %>
+  </div>
+  <div class="span10">
+    <table class="table">
+    <tr>
+      <td>asin</td>
+      <td><%= link_to @product.asin, @product.detail_page_url %></td>
+    </tr>
+    <tr>
+      <td>images</td>
+      <td>
+        <%= image_tag(@product.large_image.url) %>
+        <%= image_tag(@product.medium_image.url) %>
+        <%= image_tag(@product.small_image.url) %>
+      </td>
+    </tr>
+    <tr>
+      <td>attributes</td>
+      <td>
+        <% @product.item_attributes.each do |(key, value)| %>
+          <p><%= key %>: <%= value %></p>
+        <% end %>
+      </td>
+    </tr>
+    <tr>
+      <td>reviews</td>
+      <td>
+        <%= @product.editorial_reviews.editorial_review.content %>
+      </td>
+    </tr>
+    <tr>
+      <td>links</td>
+      <td>
+        <ul>
+          <% @product.item_links.item_link.each do |link| %>
+            <li><%= link_to link.description, link.url %></li>
+          <% end %>
+        </ul>
+      </td>
+    </tr>
+    </table>
+  </div>
+</div>
+
config/routes.rb
@@ -29,6 +29,7 @@ Cake::Application.routes.draw do
   get '/creations' => redirect('/cakes')
   get 'creations/:id', to: redirect('/cakes/%{id}')
   get 'creations/page/:page', to: redirect('/cakes/page/%{page}')
+  resources :products, only: [:index, :show]
 
   resources :profiles, only: [:index, :show] do
     get 'page/:page', action: :index, on: :collection, as: :paginate