mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Refactoring the RABL injection and the Hubs/Producers/Enterprises services
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
Darkswarm.factory 'Enterprises', (enterprises)->
|
||||
new class Enterprises
|
||||
enterprises_by_id: {} # id/object pairs for lookup
|
||||
constructor: ->
|
||||
@enterprises = enterprises
|
||||
@dereference()
|
||||
for enterprise in enterprises
|
||||
@enterprises_by_id[enterprise.id] = enterprise
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
Darkswarm.factory 'Hubs', (hubs, $filter) ->
|
||||
Darkswarm.factory 'Hubs', ($filter, Enterprises) ->
|
||||
new class Hubs
|
||||
constructor: ->
|
||||
@hubs = $filter('orderBy')(hubs, ['-active', '+orders_close_at'])
|
||||
@hubs = @filter Enterprises.enterprises.filter (hub)->
|
||||
hub.type == "hub"
|
||||
|
||||
|
||||
filter: (hubs)->
|
||||
$filter('orderBy')(hubs, ['-active', '+orders_close_at'])
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
Darkswarm.factory 'Producers', (producers) ->
|
||||
Darkswarm.factory 'Producers', (Enterprises) ->
|
||||
new class Producers
|
||||
constructor: ->
|
||||
@producers = producers
|
||||
@producers = Enterprises.enterprises.filter (enterprise)->
|
||||
enterprise.type == "producer"
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ class HomeController < BaseController
|
||||
|
||||
def index
|
||||
@active_distributors ||= Enterprise.distributors_with_active_order_cycles
|
||||
@enterprises = Enterprise.visible
|
||||
end
|
||||
|
||||
def about_us
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#hubs.hubs{"ng-controller" => "HubsCtrl"}
|
||||
:javascript
|
||||
angular.module('Darkswarm').value('hubs', #{render "json/hubs"})
|
||||
= inject_json "enterprises" , "enterprises", collection: @enterprises
|
||||
|
||||
.row
|
||||
.small-12.columns.text-center
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
collection Enterprise.visible.is_distributor
|
||||
collection Enterprise.is_distributor.visible
|
||||
extends 'json/partials/enterprise'
|
||||
extends 'json/partials/hub'
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
collection @producers
|
||||
collection Enterprise.is_primary_producer.visible
|
||||
extends 'json/partials/enterprise'
|
||||
extends 'json/partials/producer'
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
attributes :name, :id, :description, :latitude, :longitude, :long_description, :website, :instagram, :linkedin, :twitter, :facebook, :is_primary_producer?, :is_distributor?
|
||||
|
||||
node :type do |enterprise|
|
||||
if enterprise.is_primary_producer?
|
||||
"producer"
|
||||
elsif enterprise.is_distributor?
|
||||
"hub"
|
||||
end
|
||||
end
|
||||
|
||||
node :email do |enterprise|
|
||||
enterprise.email.to_s.reverse
|
||||
end
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
|
||||
|
||||
= inject_json "currentHub", "current_hub"
|
||||
= inject_json "enterprises", "enterprises"
|
||||
= inject_json "currentOrder", "current_order"
|
||||
= inject_json "user", "current_user"
|
||||
= inject_json "railsFlash", "flash"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
= inject_json "enterprisesForMap" , "enterprises_for_map", collection: @enterprises
|
||||
= inject_json "enterprisesForMap" , "enterprises", collection: @enterprises
|
||||
|
||||
.map-container{"fill-vertical" => true}
|
||||
%map{"ng-controller" => "MapCtrl"}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
.producers{"ng-controller" => "ProducersCtrl"}
|
||||
:javascript
|
||||
angular.module('Darkswarm').value('producers', #{render partial: "json/producers", object: @producers})
|
||||
-#%pre
|
||||
-#{{ Producers.producers | json }}
|
||||
= inject_json "producers" , "producers", collection: @producers
|
||||
|
||||
.row
|
||||
.small-12.columns.text-center.pad-top
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
describe "Enterprises service", ->
|
||||
Enterprises = null
|
||||
enterprises = [
|
||||
{id: 1, type: "hub"},
|
||||
{id: 2, type: "producer"}
|
||||
]
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
|
||||
inject ($injector)->
|
||||
Enterprises = $injector.get("Enterprises")
|
||||
|
||||
it "stores enterprises as id/object pairs", ->
|
||||
expect(Enterprises.enterprises_by_id["1"]).toBe enterprises[0]
|
||||
expect(Enterprises.enterprises_by_id["2"]).toBe enterprises[1]
|
||||
|
||||
it "stores enterprises as an array", ->
|
||||
expect(Enterprises.enterprises).toBe enterprises
|
||||
|
||||
it "puts the same objects in enterprises and enterprises_by_id", ->
|
||||
expect(Enterprises.enterprises[0]).toBe Enterprises.enterprises_by_id["1"]
|
||||
39
spec/javascripts/unit/darkswarm/services/hubs_spec.js.coffee
Normal file
39
spec/javascripts/unit/darkswarm/services/hubs_spec.js.coffee
Normal file
@@ -0,0 +1,39 @@
|
||||
describe "Hubs service", ->
|
||||
Hubs = null
|
||||
Enterprises = null
|
||||
hubs = [
|
||||
{
|
||||
id: 2
|
||||
active: false
|
||||
orders_close_at: new Date()
|
||||
type: "hub"
|
||||
}
|
||||
{
|
||||
id: 3
|
||||
active: false
|
||||
orders_close_at: new Date()
|
||||
type: "hub"
|
||||
}
|
||||
{
|
||||
id: 1
|
||||
active: true
|
||||
orders_close_at: new Date()
|
||||
type: "hub"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('enterprises', hubs)
|
||||
inject ($injector)->
|
||||
Enterprises = $injector.get("Enterprises")
|
||||
Hubs = $injector.get("Hubs")
|
||||
|
||||
it "filters Enterprise.hubs into a new array", ->
|
||||
expect(Hubs.hubs[0]).toBe Enterprises.enterprises[2]
|
||||
# Because the $filter is a new sorted array
|
||||
# We check to see the objects in both arrays are still the same
|
||||
Enterprises.enterprises[2].active = false
|
||||
expect(Hubs.hubs[0].active).toBe false
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
describe "Producers service", ->
|
||||
Producers = null
|
||||
Enterprises = null
|
||||
enterprises = [
|
||||
{type: "producer"}
|
||||
]
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('enterprises', enterprises)
|
||||
inject ($injector)->
|
||||
Producers = $injector.get("Producers")
|
||||
|
||||
it "delegates producers array to Enterprises", ->
|
||||
expect(Producers.producers[0]).toBe enterprises[0]
|
||||
Reference in New Issue
Block a user