diff --git a/app/assets/javascripts/darkswarm/all.js.coffee b/app/assets/javascripts/darkswarm/all.js.coffee index 96486a4a69..4b8269dedd 100644 --- a/app/assets/javascripts/darkswarm/all.js.coffee +++ b/app/assets/javascripts/darkswarm/all.js.coffee @@ -8,7 +8,7 @@ #= require ./shop #= require_tree . -$ -> - $(document).foundation() +#$ -> + #$(document).foundation() diff --git a/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee new file mode 100644 index 0000000000..eee5bb0175 --- /dev/null +++ b/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee @@ -0,0 +1,5 @@ +#Shop.controller "OrderCycleCtrl", ($scope, OrderCycle) -> + + #$scope.setOrderCycle = ()-> + #console.log "foo" + ##OrderCycle. diff --git a/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee b/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee new file mode 100644 index 0000000000..2b285c64bc --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee @@ -0,0 +1,6 @@ +Shop.factory 'OrderCycle', ($resource) -> + class OrderCycle + @set_order_cycle: (id)-> + new $resource("/shop/order_cycle").$save () -> + console.log "pushed" + # Push id to endpoint diff --git a/app/assets/javascripts/darkswarm/services/product.js.coffee b/app/assets/javascripts/darkswarm/services/product.js.coffee index e0f6658c8f..5fc09dae54 100644 --- a/app/assets/javascripts/darkswarm/services/product.js.coffee +++ b/app/assets/javascripts/darkswarm/services/product.js.coffee @@ -1,8 +1,5 @@ Shop.factory 'Product', ($resource) -> - #return $resource("/shop/products") class Product @all: -> - $resource("/shop/products").query() - - #new Product + response = $resource("/shop/products").query() diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 0df04edb50..5d3f8caf02 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -4,7 +4,15 @@ class ShopController < BaseController before_filter :set_distributor before_filter :set_order_cycles - def index + def show + end + + def products + if current_order_cycle + render json: Spree::Product.all.to_json + else + render json: "", status: 404 + end end private diff --git a/app/views/shop/_order_cycles.html.haml b/app/views/shop/_order_cycles.html.haml index 21c4af6825..f485ad3d21 100644 --- a/app/views/shop/_order_cycles.html.haml +++ b/app/views/shop/_order_cycles.html.haml @@ -1,10 +1,14 @@ -- if @order_cycles.empty? - Orders are currently closed for this hub - %p Please contact your hub directly to see if they accept late orders, or wait until the next cycle opens. +%ordercycle + - if @order_cycles.empty? + Orders are currently closed for this hub + %p Please contact your hub directly to see if they accept late orders, or wait until the next cycle opens. - = render partial: "shop/next_order_cycle" - = render partial: "shop/last_order_cycle" + = render partial: "shop/next_order_cycle" + = render partial: "shop/last_order_cycle" + + - else + Ready for: + = select_tag :order_cycle_id, + options_for_select(order_cycles_name_and_pickup_times(@order_cycles), + current_order_cycle.andand.id), :prompt => "Select an Order Cycle!" -- else - Ready for: - = select_tag :order_cycle_id, options_for_select(order_cycles_name_and_pickup_times(@order_cycles), current_order_cycle.andand.id), :prompt => "Select an Order Cycle!" diff --git a/app/views/shop/index.html.haml b/app/views/shop/index.html.haml deleted file mode 100644 index 23760a8c94..0000000000 --- a/app/views/shop/index.html.haml +++ /dev/null @@ -1,13 +0,0 @@ -= @distributor.name - -= render partial: "shop/order_cycles" - -%description - = @distributor.long_description.andand.html_safe - -%products{"ng-app" => "Shop", "ng-controller" => "ProductsCtrl"} - {{ products | json }} - -= render partial: "enterprises/contact_us" -= render partial: "enterprises/about_us" - diff --git a/app/views/shop/show.html.haml b/app/views/shop/show.html.haml new file mode 100644 index 0000000000..59fed17c5b --- /dev/null +++ b/app/views/shop/show.html.haml @@ -0,0 +1,14 @@ +%shop{"ng-app" => "Shop"} + = @distributor.name + + = render partial: "shop/order_cycles" + + %description + = @distributor.long_description.andand.html_safe + + %products{"ng-controller" => "ProductsCtrl"} + {{ products }} + + = render partial: "enterprises/contact_us" + = render partial: "enterprises/about_us" + diff --git a/config/ng-test.conf.js b/config/ng-test.conf.js index cc03b1588c..95d05a75fc 100644 --- a/config/ng-test.conf.js +++ b/config/ng-test.conf.js @@ -11,6 +11,8 @@ module.exports = function(config) { 'app/assets/javascripts/admin/order_cycle.js.erb.coffee', 'app/assets/javascripts/admin/bulk_product_update.js', + 'app/assets/javascripts/darkswarm/*.js*', + 'app/assets/javascripts/darkswarm/**/*.js*', 'spec/javascripts/unit/**/*.js*' ], diff --git a/config/routes.rb b/config/routes.rb index 3c6b0b26cc..7991f605ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,9 @@ Openfoodnetwork::Application.routes.draw do root :to => 'home#temp_landing_page' - resources :shop + resource :shop, controller: :shop do + get :products + end resources :enterprises do collection do diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 1747dcff5c..49257e5de0 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -1,22 +1,44 @@ require 'spec_helper' describe ShopController do - it "should select an order cycle when only one order cycle is open" do - d = create(:distributor_enterprise) - oc1 = create(:order_cycle, distributors: [d]) + let(:d) { create(:distributor_enterprise) } + before do controller.stub(:current_distributor).and_return d - spree_get :index - controller.current_order_cycle.should == oc1 end - it "should not set an order cycle when multiple order cycles are open" do - d = create(:distributor_enterprise) - oc1 = create(:order_cycle, distributors: [d]) - oc2 = create(:order_cycle, distributors: [d]) - controller.stub(:current_distributor).and_return d - spree_get :index - controller.current_order_cycle.should == nil + describe "Selecting order cycles" do + it "should select an order cycle when only one order cycle is open" do + oc1 = create(:order_cycle, distributors: [d]) + spree_get :show + controller.current_order_cycle.should == oc1 + end + + it "should not set an order cycle when multiple order cycles are open" do + oc1 = create(:order_cycle, distributors: [d]) + oc2 = create(:order_cycle, distributors: [d]) + spree_get :show + controller.current_order_cycle.should == nil + end end - it "should create/load an order when loading show" + describe "returning products" do + let(:product) { create(:product) } + let(:order_cycle) { create(:order_cycle, distributors: [d]) } + before do + Spree::Product.stub(:all).and_return([product]) + end + + it "returns products via json" do + controller.stub(:current_order_cycle).and_return order_cycle + xhr :get, :products + response.should be_success + response.body.should_not be_empty + end + + it "does not return products if no order_cycle is selected" do + xhr :get, :products + response.status.should == 404 + response.body.should be_empty + end + end end diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 6d75d2b8ea..53f451dd54 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -754,4 +754,4 @@ describe 'OrderCycle services', -> expect(data.incoming_exchanges[0].enterprise_fees).toBeUndefined() expect(data.outgoing_exchanges[0].enterprise_fees).toBeUndefined() expect(data.incoming_exchanges[0].enterprise_fee_ids).toEqual [1, 2] - expect(data.outgoing_exchanges[0].enterprise_fee_ids).toEqual [3, 4] \ No newline at end of file + expect(data.outgoing_exchanges[0].enterprise_fee_ids).toEqual [3, 4] diff --git a/spec/javascripts/unit/product_spec.js.coffee b/spec/javascripts/unit/product_spec.js.coffee new file mode 100644 index 0000000000..d48d09244f --- /dev/null +++ b/spec/javascripts/unit/product_spec.js.coffee @@ -0,0 +1,15 @@ +describe 'Shop services', -> + $httpBackend = null + Product = null + + beforeEach -> + module 'Shop' + inject ($injector, _$httpBackend_)-> + Product = $injector.get("Product") + $httpBackend = _$httpBackend_ + + it "Fetches products from the backend", -> + $httpBackend.expectGET("/shop/products").respond([{test : "cats"}]) + products = Product.all() + $httpBackend.flush() + expect(products[0].test).toEqual "cats" diff --git a/spec/javascripts/unit/shop_spec.js.coffee b/spec/javascripts/unit/shop_spec.js.coffee new file mode 100644 index 0000000000..d29c83b35c --- /dev/null +++ b/spec/javascripts/unit/shop_spec.js.coffee @@ -0,0 +1,18 @@ +describe 'Shop controllers', -> + describe 'ProductsCtrl', -> + ctrl = null + scope = null + event = null + Product = null + + beforeEach -> + module('Shop') + scope = {} + Product = + all: -> + 'testy mctest' + inject ($controller) -> + ctrl = $controller 'ProductsCtrl', {$scope: scope, Product : Product} + + it 'Fetches products from Product', -> + expect(scope.products).toEqual 'testy mctest'