Refactoring the RABL injection and the Hubs/Producers/Enterprises services

This commit is contained in:
Will Marshall
2014-06-18 15:40:02 +10:00
parent c8384f1a71
commit a2965696da
14 changed files with 103 additions and 15 deletions

View File

@@ -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

View File

@@ -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'])

View File

@@ -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"

View File

@@ -3,6 +3,7 @@ class HomeController < BaseController
def index
@active_distributors ||= Enterprise.distributors_with_active_order_cycles
@enterprises = Enterprise.visible
end
def about_us

View File

@@ -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

View File

@@ -1,4 +1,4 @@
collection Enterprise.visible.is_distributor
collection Enterprise.is_distributor.visible
extends 'json/partials/enterprise'
extends 'json/partials/hub'

View File

@@ -1,3 +1,3 @@
collection @producers
collection Enterprise.is_primary_producer.visible
extends 'json/partials/enterprise'
extends 'json/partials/producer'

View File

@@ -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

View File

@@ -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"

View File

@@ -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"}

View File

@@ -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

View File

@@ -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"]

View 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

View File

@@ -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]