Minor refactoring from code review with Rohan

This commit is contained in:
Will Marshall
2014-06-19 11:41:24 +10:00
parent c02c7cf7ca
commit b9561ecf20
18 changed files with 19 additions and 53 deletions

View File

@@ -2,7 +2,7 @@ Darkswarm.factory 'Hubs', ($filter, Enterprises) ->
new class Hubs
constructor: ->
@hubs = @order Enterprises.enterprises.filter (hub)->
hub.enterprise_type == "hub"
hub.is_distributor
order: (hubs)->
$filter('orderBy')(hubs, ['-active', '+orders_close_at'])

View File

@@ -7,8 +7,8 @@ Darkswarm.factory "OfnMap", (Enterprises, MapModal)->
# Adding methods to each enterprise
extend: (enterprise)->
new class MapMarker
# We're whitelisting attributes because Gmaps tries to crawl
# our data, and our data is recursive
# We're whitelisting attributes because GMaps tries to crawl
# our data, and our data is recursive, so it breaks
latitude: enterprise.latitude
longitude: enterprise.longitude
icon: enterprise.icon

View File

@@ -2,9 +2,10 @@ Darkswarm.factory "MapModal", ($modal, $rootScope)->
new class MapModal
open: (enterprise)->
scope = $rootScope.$new(true) # Spawn an isolate to contain the enterprise
if enterprise.enterprise_type == "producer"
scope.producer = enterprise
$modal.open(templateUrl: "map_modal_producer.html", scope: scope)
else
if enterprise.is_distributor
scope.hub = enterprise
$modal.open(templateUrl: "map_modal_hub.html", scope: scope)
else
scope.producer = enterprise
$modal.open(templateUrl: "map_modal_producer.html", scope: scope)

View File

@@ -2,5 +2,5 @@ Darkswarm.factory 'Producers', (Enterprises) ->
new class Producers
constructor: ->
@producers = Enterprises.enterprises.filter (enterprise)->
enterprise.enterprise_type == "producer"
enterprise.is_primary_producer

View File

@@ -15,8 +15,4 @@ class BaseController < ApplicationController
def load_active_distributors
@active_distributors ||= Enterprise.distributors_with_active_order_cycles
end
def load_visible_enterprises
@enterprises = Enterprise.visible
end
end

View File

@@ -1,7 +1,6 @@
class HomeController < BaseController
layout 'darkswarm'
before_filter :load_active_distributors
before_filter :load_visible_enterprises
def index
end

View File

@@ -1,7 +1,6 @@
class MapController < BaseController
layout 'darkswarm'
before_filter :load_active_distributors
before_filter :load_visible_enterprises
def index
end

View File

@@ -1,7 +1,6 @@
class ProducersController < BaseController
layout 'darkswarm'
before_filter :load_active_distributors
before_filter :load_visible_enterprises
def index
end

View File

@@ -1,3 +1,6 @@
# TODO: This should be moved into the controller
# RABL is tricky to pass variables into: so we do this as a workaround for now
# I noticed some vague comments on Rabl github about this, but haven't looked into
collection Enterprise.visible
extends 'json/partials/enterprise'
extends 'json/partials/producer'

View File

@@ -1,2 +0,0 @@
collection @enterprises
extends "json/partials/enterprise"

View File

@@ -1,12 +1,4 @@
attributes :name, :id, :description, :latitude, :longitude, :long_description, :website, :instagram, :linkedin, :twitter, :facebook
node :enterprise_type do |enterprise|
if enterprise.is_primary_producer?
"producer"
elsif enterprise.is_distributor?
"hub"
end
end
attributes :name, :id, :description, :latitude, :longitude, :long_description, :website, :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor
node :email do |enterprise|
enterprise.email.to_s.reverse

View File

@@ -8,10 +8,10 @@ node :path do |enterprise|
shop_enterprise_path(enterprise)
end
node :pickup do |hub|
not hub.shipping_methods.where(:require_ship_address => false).empty?
hub.shipping_methods.where(:require_ship_address => false).present?
end
node :delivery do |hub|
not hub.shipping_methods.where(:require_ship_address => true).empty?
hub.shipping_methods.where(:require_ship_address => true).present?
end
node :active do |hub|
@active_distributors.include?(hub)

View File

@@ -23,9 +23,4 @@ describe BaseController 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

View File

@@ -7,7 +7,6 @@ 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
@@ -15,11 +14,6 @@ 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

View File

@@ -1,11 +1,6 @@
require 'spec_helper'
describe MapController do
it "loads all visible enterprises" do
Enterprise.should_receive(:visible)
get :index
end
it "loads active distributors" do
Enterprise.should_receive(:distributors_with_active_order_cycles)
get :index

View File

@@ -12,9 +12,4 @@ describe ProducersController do
get :index
assigns[:active_distributors].should == [distributor]
end
it "loads visible enterprises" do
get :index
assigns[:enterprises].should == [distributor]
end
end

View File

@@ -6,19 +6,19 @@ describe "Hubs service", ->
id: 2
active: false
orders_close_at: new Date()
enterprise_type: "hub"
is_distributor: true
}
{
id: 3
active: false
orders_close_at: new Date()
enterprise_type: "hub"
is_distributor: true
}
{
id: 1
active: true
orders_close_at: new Date()
enterprise_type: "hub"
is_distributor: true
}
]

View File

@@ -2,7 +2,7 @@ describe "Producers service", ->
Producers = null
Enterprises = null
enterprises = [
{enterprise_type: "producer"}
{is_primary_producer: true}
]
beforeEach ->