mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-03 02:21:33 +00:00
Refactoring JSON injection a bit further
This commit is contained in:
@@ -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'])
|
||||
|
||||
@@ -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)->
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#hubs.hubs{"ng-controller" => "HubsCtrl"}
|
||||
= inject_json "enterprises" , "enterprises", collection: @enterprises
|
||||
= inject_enterprises
|
||||
|
||||
.row
|
||||
.small-12.columns.text-center
|
||||
|
||||
2
app/views/json/_enterprises_for_map.rabl
Normal file
2
app/views/json/_enterprises_for_map.rabl
Normal file
@@ -0,0 +1,2 @@
|
||||
collection @enterprises
|
||||
extends 'json/partials/enterprise'
|
||||
@@ -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"}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
.producers{"ng-controller" => "ProducersCtrl"}
|
||||
= inject_json "producers" , "producers", collection: @producers
|
||||
= inject_enterprises
|
||||
|
||||
.row
|
||||
.small-12.columns.text-center.pad-top
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
19
spec/javascripts/unit/darkswarm/services/map_spec.js.coffee
Normal file
19
spec/javascripts/unit/darkswarm/services/map_spec.js.coffee
Normal file
@@ -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]
|
||||
Reference in New Issue
Block a user