From 4d03f657663166f0f36ffb1b0088ef7af96f013a Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Tue, 7 Jan 2014 15:06:47 +1100 Subject: [PATCH] Reworking everything to use RABL --- .../javascripts/darkswarm/shop.js.coffee | 1 + app/controllers/shop_controller.rb | 17 +++++---- app/views/shop/_order_cycle.rabl | 3 ++ app/views/shop/_order_cycles.html.haml | 2 +- app/views/shop/_products.html.haml | 5 ++- app/views/shop/products.rabl | 25 +++++++++++++ config/routes.rb | 1 + spec/controllers/shop_controller_spec.rb | 37 ++++++++++++++----- spec/features/consumer/shopping_spec.rb | 4 +- 9 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 app/views/shop/_order_cycle.rabl create mode 100644 app/views/shop/products.rabl diff --git a/app/assets/javascripts/darkswarm/shop.js.coffee b/app/assets/javascripts/darkswarm/shop.js.coffee index 3b30e222d6..85c94211e6 100644 --- a/app/assets/javascripts/darkswarm/shop.js.coffee +++ b/app/assets/javascripts/darkswarm/shop.js.coffee @@ -5,6 +5,7 @@ window.Shop = angular.module("Shop", ["ngResource", "filters"]).config ($httpPro angular.module("filters", []).filter "truncate", -> (text, length, end) -> + text = text || "" length = 10 if isNaN(length) end = "..." if end is `undefined` if text.length <= length or text.length - end.length <= length diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 36d3504c5a..dde9af99f4 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -10,25 +10,28 @@ class ShopController < BaseController end def products - if products = current_order_cycle.andand.products_distributed_by(@distributor) - render json: products, root: false - else + unless @products = current_order_cycle.andand.products_distributed_by(@distributor) render json: "", status: 404 end end def order_cycle - if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id]) - current_order(true).set_order_cycle! oc - render status: 200, json: oc + if request.post? + if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id]) + current_order(true).set_order_cycle! oc + render partial: "shop/order_cycle" + else + render status: 404, json: "" + end else - render status: 404, json: "" + render partial: "shop/order_cycle" end end private def set_distributor + unless @distributor = current_distributor redirect_to root_path end diff --git a/app/views/shop/_order_cycle.rabl b/app/views/shop/_order_cycle.rabl new file mode 100644 index 0000000000..493dee1de2 --- /dev/null +++ b/app/views/shop/_order_cycle.rabl @@ -0,0 +1,3 @@ +object current_order_cycle +attributes :orders_close_at +attribute :id => :order_cycle_id diff --git a/app/views/shop/_order_cycles.html.haml b/app/views/shop/_order_cycles.html.haml index 4b0a7b4bbd..60d5997cf3 100644 --- a/app/views/shop/_order_cycles.html.haml +++ b/app/views/shop/_order_cycles.html.haml @@ -1,7 +1,7 @@ %ordercycle{"ng-controller" => "OrderCycleCtrl"} :javascript - angular.module('Shop').value('orderCycleData', #{OrderCycleSerializer.new(current_order_cycle).to_json}) + angular.module('Shop').value('orderCycleData', #{render "shop/order_cycle"}) - if @order_cycles.empty? diff --git a/app/views/shop/_products.html.haml b/app/views/shop/_products.html.haml index 1d76f80b1b..034cd53ab6 100644 --- a/app/views/shop/_products.html.haml +++ b/app/views/shop/_products.html.haml @@ -13,13 +13,14 @@ %tbody{"ng-repeat" => "product in data.products | filter:query"} %tr.product %td - %img{src: "{{ product.master.images[0].small_url }}"} - {{product.master.images[0].alt}} + -#%img{src: "{{ product.master.images[0].small_url }}"} + -#{{product.master.images[0].alt}} %td %h5 {{ product.name }} {{ product.supplier.name }} %td.notes {{ product.description | truncate:80 }} + %td {{ product.master.options_text }} %td %input{type: :number, value: 0, min: 0, name: "variants[{{product.master.id}}]"} diff --git a/app/views/shop/products.rabl b/app/views/shop/products.rabl new file mode 100644 index 0000000000..7d245447e9 --- /dev/null +++ b/app/views/shop/products.rabl @@ -0,0 +1,25 @@ +collection @products +attributes :id, :name, :description, :price, :permalink + +child :supplier do + attributes :id, :name, :description +end +child :master => :master do + attributes :id, :is_master, :count_on_hand, :options_text + child :images => :images do + attributes :id, :alt + node do |img| + {:small_url => img.attachment.url(:small, false)} + end + end +end +child :variants => :variants do |variant| + attributes :id, :is_master, :count_on_hand, :options_text + child :images => :images do + attributes :id, :alt + node do |img| + {:small_url => img.attachment.url(:small, false)} + end + end +end + diff --git a/config/routes.rb b/config/routes.rb index 7a67be8d06..6e05b80384 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Openfoodnetwork::Application.routes.draw do resource :shop, controller: :shop do get :products post :order_cycle + get :order_cycle end resources :enterprises do diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 5f2a677679..71f776163a 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -37,14 +37,27 @@ describe ShopController do controller.current_order_cycle.should == oc2 end - it "should return the order cycle details when the oc is selected" do - oc1 = create(:order_cycle, distributors: [d]) - oc2 = create(:order_cycle, distributors: [d]) - - spree_post :order_cycle, order_cycle_id: oc2.id - response.body.should have_content OrderCycleSerializer.new(oc2).to_json + context "RABL tests" do + render_views + it "should return the order cycle details when the oc is selected" do + oc1 = create(:order_cycle, distributors: [d]) + oc2 = create(:order_cycle, distributors: [d]) + + spree_post :order_cycle, order_cycle_id: oc2.id + response.should be_success + response.body.should have_content oc2.id + end + + it "should return the current order cycle when hit with GET" do + oc1 = create(:order_cycle, distributors: [d]) + controller.stub(:current_order_cycle).and_return oc1 + spree_get :order_cycle + response.body.should have_content oc1.id + end + end + it "should not allow the user to select an invalid order cycle" do oc1 = create(:order_cycle, distributors: [d]) oc2 = create(:order_cycle, distributors: [d]) @@ -95,11 +108,15 @@ describe ShopController do response.body.should be_empty end - it "only returns products for the current order cycle" do - controller.stub(:current_order_cycle).and_return order_cycle - xhr :get, :products - response.body.should == [Spree::ProductSerializer.new(product)].to_json + context "RABL tests" do + render_views + it "only returns products for the current order cycle" do + controller.stub(:current_order_cycle).and_return order_cycle + xhr :get, :products + response.body.should have_content product.name + end end + end end end diff --git a/spec/features/consumer/shopping_spec.rb b/spec/features/consumer/shopping_spec.rb index 9a136df99d..bbf127f1cb 100644 --- a/spec/features/consumer/shopping_spec.rb +++ b/spec/features/consumer/shopping_spec.rb @@ -108,8 +108,8 @@ feature "As a consumer I want to shop with a distributor", js: true do select "frogs", :from => "order_cycle_id" end it "should let us add products to our cart" do - fill_in "quantity_variant_#{variant.id}", with: "1" - find("form.custom > input.button.right:first-child").click + fill_in "variants[#{variant.id}]", with: "1" + first("form.custom > input.button.right").click current_path.should == "/cart" page.should have_content product.name end