diff --git a/app/assets/javascripts/darkswarm/services/hubs.js.coffee b/app/assets/javascripts/darkswarm/services/hubs.js.coffee index 402b5a0603..65940c5708 100644 --- a/app/assets/javascripts/darkswarm/services/hubs.js.coffee +++ b/app/assets/javascripts/darkswarm/services/hubs.js.coffee @@ -3,7 +3,6 @@ Darkswarm.factory 'Hubs', ($filter, Enterprises) -> constructor: -> @hubs = @filter Enterprises.enterprises.filter (hub)-> hub.type == "hub" - filter: (hubs)-> $filter('orderBy')(hubs, ['-active', '+orders_close_at']) diff --git a/app/assets/javascripts/darkswarm/services/map.js.coffee b/app/assets/javascripts/darkswarm/services/map.js.coffee index eb1a195490..f2050a6f25 100644 --- a/app/assets/javascripts/darkswarm/services/map.js.coffee +++ b/app/assets/javascripts/darkswarm/services/map.js.coffee @@ -2,6 +2,7 @@ Darkswarm.factory "OfnMap", (enterprisesForMap, MapModal)-> new class OfnMap constructor: -> @enterprises = (@extend(enterprise) for enterprise in enterprisesForMap) + console.log @enterprises # Adding methods to each enterprise extend: (enterprise)-> diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index ba53ff4a0f..1a4c0aac8d 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -11,4 +11,12 @@ class BaseController < ApplicationController include Spree::ProductsHelper before_filter :check_order_cycle_expiry + + def load_active_distributors + @active_distributors ||= Enterprise.distributors_with_active_order_cycles + end + + def load_visible_enterprises + @enterprises = Enterprise.visible + end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 54c200beff..97b386157d 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,11 +1,12 @@ class HomeController < BaseController layout 'darkswarm' + before_filter :load_active_distributors + before_filter :load_visible_enterprises def index - @active_distributors ||= Enterprise.distributors_with_active_order_cycles - @enterprises = Enterprise.visible end def about_us end end + diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index b5c9ceba83..d63d7f86a9 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -1,7 +1,8 @@ class MapController < BaseController layout 'darkswarm' + before_filter :load_active_distributors + before_filter :load_visible_enterprises + def index - @active_distributors ||= Enterprise.distributors_with_active_order_cycles - @enterprises = Enterprise.visible end end diff --git a/app/controllers/producers_controller.rb b/app/controllers/producers_controller.rb index c5a133a798..d7d665de90 100644 --- a/app/controllers/producers_controller.rb +++ b/app/controllers/producers_controller.rb @@ -1,7 +1,8 @@ class ProducersController < BaseController layout 'darkswarm' + before_filter :load_active_distributors + before_filter :load_visible_enterprises def index - @producers = Enterprise.is_primary_producer.visible end end diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index 3dee7eefbf..f421107994 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -1,4 +1,7 @@ module SharedHelper + def inject_enterprises + inject_json "enterprises" , "enterprises", collection: @enterprises + end def inject_json(name, partial, opts = {}) render "json/injection", {name: name, partial: partial}.merge(opts) diff --git a/app/views/home/_hubs.html.haml b/app/views/home/_hubs.html.haml index 17a4cc13a5..55b8cc29ca 100644 --- a/app/views/home/_hubs.html.haml +++ b/app/views/home/_hubs.html.haml @@ -1,5 +1,5 @@ #hubs.hubs{"ng-controller" => "HubsCtrl"} - = inject_json "enterprises" , "enterprises", collection: @enterprises + = inject_enterprises .row .small-12.columns.text-center diff --git a/app/views/json/_enterprises_for_map.rabl b/app/views/json/_enterprises_for_map.rabl new file mode 100644 index 0000000000..641cb34a82 --- /dev/null +++ b/app/views/json/_enterprises_for_map.rabl @@ -0,0 +1,2 @@ +collection @enterprises +extends 'json/partials/enterprise' diff --git a/app/views/map/index.html.haml b/app/views/map/index.html.haml index 2c49deb4d4..277178bfdc 100644 --- a/app/views/map/index.html.haml +++ b/app/views/map/index.html.haml @@ -1,4 +1,4 @@ -= inject_json "enterprisesForMap" , "enterprises", collection: @enterprises += inject_json "enterprisesForMap" , "enterprises_for_map", collection: @enterprises .map-container{"fill-vertical" => true} %map{"ng-controller" => "MapCtrl"} diff --git a/app/views/producers/index.haml b/app/views/producers/index.haml index e30475fdd8..2c3253a141 100644 --- a/app/views/producers/index.haml +++ b/app/views/producers/index.haml @@ -1,5 +1,5 @@ .producers{"ng-controller" => "ProducersCtrl"} - = inject_json "producers" , "producers", collection: @producers + = inject_enterprises .row .small-12.columns.text-center.pad-top diff --git a/spec/controllers/base_controller_spec.rb b/spec/controllers/base_controller_spec.rb index 8269c9374d..85afcbc7ff 100644 --- a/spec/controllers/base_controller_spec.rb +++ b/spec/controllers/base_controller_spec.rb @@ -18,4 +18,14 @@ describe BaseController do response.should redirect_to root_url flash[:info].should == "The order cycle you've selected has just closed. Please try again!" end + + it "loads active_distributors" do + Enterprise.should_receive(:distributors_with_active_order_cycles) + controller.load_active_distributors + end + + it "loads visible enterprises" do + Enterprise.should_receive(:visible) + controller.load_visible_enterprises + end end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index babb1c84e6..54b6611438 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -7,6 +7,7 @@ describe HomeController do before do Enterprise.stub(:distributors_with_active_order_cycles).and_return [distributor] + Enterprise.stub(:visible).and_return [distributor] end it "sets active distributors" do @@ -14,6 +15,11 @@ describe HomeController do assigns[:active_distributors].should == [distributor] end + it "loads visible enterprises" do + get :index + assigns[:enterprises].should == [distributor] + end + it "does not show invisible hubs" do get :index response.body.should_not have_content invisible_distributor.name diff --git a/spec/controllers/map_controller_spec.rb b/spec/controllers/map_controller_spec.rb index dedb534b1b..455420380d 100644 --- a/spec/controllers/map_controller_spec.rb +++ b/spec/controllers/map_controller_spec.rb @@ -5,4 +5,9 @@ describe MapController do Enterprise.should_receive(:visible) get :index end + + it "loads active distributors" do + Enterprise.should_receive(:distributors_with_active_order_cycles) + get :index + end end diff --git a/spec/controllers/producers_controller_spec.rb b/spec/controllers/producers_controller_spec.rb index 885b3981a1..7b399df209 100644 --- a/spec/controllers/producers_controller_spec.rb +++ b/spec/controllers/producers_controller_spec.rb @@ -1,9 +1,20 @@ require 'spec_helper' describe ProducersController do - it "gets all active producers" do - Enterprise.stub_chain(:is_primary_producer, :visible) - Enterprise.should_receive(:is_primary_producer) + let!(:distributor) { create(:distributor_enterprise) } + + before do + Enterprise.stub(:distributors_with_active_order_cycles).and_return [distributor] + Enterprise.stub(:visible).and_return [distributor] + end + + it "sets active distributors" do get :index + assigns[:active_distributors].should == [distributor] + end + + it "loads visible enterprises" do + get :index + assigns[:enterprises].should == [distributor] end end diff --git a/spec/javascripts/unit/darkswarm/services/map_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/map_spec.js.coffee new file mode 100644 index 0000000000..19ee666e19 --- /dev/null +++ b/spec/javascripts/unit/darkswarm/services/map_spec.js.coffee @@ -0,0 +1,19 @@ +describe "Hubs service", -> + OfnMap = null + enterprises = [ + { + id: 2 + active: false + orders_close_at: new Date() + type: "hub" + } + ] + + beforeEach -> + module 'Darkswarm' + angular.module('Darkswarm').value('enterprises', enterprises) + inject ($injector)-> + OfnMap = $injector.get("OfnMap") + + it "builds MapMarkers from enterprises", -> + expect(OfnMap.enterprises[0].enterprise).toBe enterprises[0]