Only query visible enterprises

This commit is contained in:
Matt-Yorkley
2019-05-10 15:04:29 +01:00
parent 11e83af0b6
commit a91ae8947b
12 changed files with 19 additions and 28 deletions

View File

@@ -1,20 +1,18 @@
Darkswarm.controller "GroupPageCtrl", ($scope, enterprises, Enterprises, MapConfiguration, OfnMap, visibleFilter, Navigation) ->
Darkswarm.controller "GroupPageCtrl", ($scope, enterprises, Enterprises, MapConfiguration, OfnMap, Navigation) ->
$scope.Enterprises = Enterprises
enterprises_by_id = enterprises.map (enterprise) =>
Enterprises.enterprises_by_id[enterprise.id]
visible_enterprises = visibleFilter enterprises_by_id
# TODO: this is duplicate code with app/assets/javascripts/darkswarm/services/enterprises.js.coffee
# It would be better to load only the needed enterprises (group + related shops).
$scope.group_producers = visible_enterprises.filter (enterprise) ->
$scope.group_producers = enterprises_by_id.filter (enterprise) ->
enterprise.category in ["producer_hub", "producer_shop", "producer"]
$scope.group_hubs = visible_enterprises.filter (enterprise) ->
$scope.group_hubs = enterprises_by_id.filter (enterprise) ->
enterprise.category in ["hub", "hub_profile", "producer_hub", "producer_shop"]
$scope.producers_to_filter = $scope.group_producers
$scope.map = angular.copy MapConfiguration.options
$scope.mapMarkers = OfnMap.enterprise_markers visible_enterprises
$scope.mapMarkers = OfnMap.enterprise_markers enterprises_by_id
$scope.embedded_layout = window.location.search.indexOf("embedded_shopfront=true") != -1

View File

@@ -1,4 +1,4 @@
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope) ->
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, Matcher, Geo, $rootScope) ->
new class Enterprises
enterprises_by_id: {}
@@ -13,10 +13,9 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
# Replace enterprise and taxons ids with actual objects.
@dereferenceEnterprises()
@visible_enterprises = visibleFilter @enterprises
@producers = @visible_enterprises.filter (enterprise)->
@producers = @enterprises.filter (enterprise)->
enterprise.category in ["producer_hub", "producer_shop", "producer"]
@hubs = @visible_enterprises.filter (enterprise)->
@hubs = @enterprises.filter (enterprise)->
enterprise.category in ["hub", "hub_profile", "producer_hub", "producer_shop"]
dereferenceEnterprises: ->

View File

@@ -1,4 +1,4 @@
Darkswarm.factory "OfnMap", (Enterprises, EnterpriseModal, visibleFilter) ->
Darkswarm.factory "OfnMap", (Enterprises, EnterpriseModal) ->
new class OfnMap
constructor: ->
@enterprises = @enterprise_markers(Enterprises.enterprises)
@@ -6,7 +6,7 @@ Darkswarm.factory "OfnMap", (Enterprises, EnterpriseModal, visibleFilter) ->
enterprise.latitude != null || enterprise.longitude != null # Remove enterprises w/o lat or long
enterprise_markers: (enterprises) ->
@extend(enterprise) for enterprise in visibleFilter(enterprises)
@extend(enterprise) for enterprise in enterprises
# Adding methods to each enterprise
extend: (enterprise) ->

View File

@@ -6,6 +6,7 @@ class ProducersController < BaseController
def index
@enterprises = Enterprise
.activated
.visible
.is_primary_producer
.includes(address: :state)
.includes(:properties)

View File

@@ -6,6 +6,7 @@ class ShopsController < BaseController
def index
@enterprises = Enterprise
.activated
.visible
.is_distributor
.includes(address: :state)
.includes(:properties)

View File

@@ -27,7 +27,7 @@ module InjectionHelper
inject_json_ams(
"enterprises",
Enterprise.activated.select(select_only).includes(address: :state).all,
Enterprise.activated.visible.select(select_only).includes(address: :state).all,
Api::EnterpriseShopfrontListSerializer
)
end

View File

@@ -2,7 +2,7 @@
module Api
class EnterpriseShopfrontListSerializer < ActiveModel::Serializer
attributes :name, :id, :latitude, :longitude, :is_primary_producer, :is_distributor,
:visible, :path, :icon, :icon_font, :producer_icon_font, :address_id, :sells,
:path, :icon, :icon_font, :producer_icon_font, :address_id, :sells,
:permalink
has_one :address, serializer: Api::AddressSerializer

View File

@@ -1,6 +1,6 @@
module Api
class EnterpriseThinSerializer < ActiveModel::Serializer
attributes :name, :id, :active, :path, :visible
attributes :name, :id, :active, :path
has_one :address, serializer: Api::AddressSerializer

View File

@@ -78,7 +78,7 @@
&nbsp;
.row.cta-container
.columns.small-12
%a.cta-hub{"ng-repeat" => "hub in producer.hubs | visible | orderBy:'-active'",
%a.cta-hub{"ng-repeat" => "hub in producer.hubs | orderBy:'-active'",
"ng-href" => "{{::hub.path}}", "ng-attr-target" => "{{ embedded_layout ? '_blank' : undefined }}",
"ng-class" => "::{primary: hub.active, secondary: !hub.active}"}
%i.ofn-i_068-shop-reversed{"ng-if" => "::hub.active"}

View File

@@ -16,7 +16,7 @@
.small-12.columns
.active_table
%producer.active_table_node.row.animate-repeat{id: "{{producer.path}}",
"ng-repeat" => "producer in filteredEnterprises = (Enterprises.producers | visible | searchEnterprises:query | taxons:activeTaxons | properties:activeProperties:'supplied_properties')",
"ng-repeat" => "producer in filteredEnterprises = (Enterprises.producers | searchEnterprises:query | taxons:activeTaxons | properties:activeProperties:'supplied_properties')",
"ng-controller" => "ProducerNodeCtrl",
"ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !producer.active}",
id: "{{producer.hash}}"}

View File

@@ -13,7 +13,7 @@
= t :hubs_filter_by
= t :hubs_filter_type
%filter-selector.small-block-grid-2.medium-block-grid-4.large-block-grid-5{ "selector-set" => "filterSelectors", objects: "#{resource} | visible | taxonsOf", "active-selectors" => "activeTaxons" }
%filter-selector.small-block-grid-2.medium-block-grid-4.large-block-grid-5{ "selector-set" => "filterSelectors", objects: "#{resource} | taxonsOf", "active-selectors" => "activeTaxons" }
.small-12.large-3.columns
%h5.tdhead
.light

View File

@@ -20,11 +20,11 @@ describe "Enterprises service", ->
{id: 1, visible: true, name: 'a', category: "hub", producers: [{id: 5}], taxons: [{id: 1}]},
{id: 2, visible: true, name: 'b', category: "hub", producers: [{id: 6}]}
{id: 3, visible: true, name: 'c', category: "hub_profile"}
{id: 4, visible: false,name: 'd', category: "hub", producers: [{id: 7}]}
{id: 4, visible: true, name: 'd', category: "hub", producers: [{id: 7}]}
{id: 5, visible: true, name: 'e', category: "producer_hub", hubs: [{id: 1}]},
{id: 6, visible: true, name: 'f', category: "producer_shop", hubs: [{id: 2}]},
{id: 7, visible: true, name: 'g', category: "producer", hubs: [{id: 2}]}
{id: 8, visible: false,name: 'h', category: "producer", hubs: [{id: 2}]}
{id: 8, visible: true, name: 'h', category: "producer", hubs: [{id: 2}]}
]
H1: 0
beforeEach ->
@@ -64,14 +64,6 @@ describe "Enterprises service", ->
Enterprises.enterprises[4].active = false
expect(Enterprises.producers[0].active).toBe false
it "only includes visible enterprises in hubs array", ->
expect(Enterprises.hubs).toContain Enterprises.enterprises[0]
expect(Enterprises.hubs).not.toContain Enterprises.enterprises[3]
it "only includes visible enterprises in producers array", ->
expect(Enterprises.producers).toContain Enterprises.enterprises[4]
expect(Enterprises.producers).not.toContain Enterprises.enterprises[7]
it "includes hub, hub_profile, producer_hub and, producer_shop enterprises in hubs array", ->
expect(Enterprises.hubs).toContain Enterprises.enterprises[0]
expect(Enterprises.hubs).toContain Enterprises.enterprises[2]