From a91ae8947b52d827fa8b82b31c5fa443931427e9 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 10 May 2019 15:04:29 +0100 Subject: [PATCH] Only query visible enterprises --- .../controllers/group_page_controller.js.coffee | 10 ++++------ .../darkswarm/services/enterprises.js.coffee | 7 +++---- .../javascripts/darkswarm/services/map.js.coffee | 4 ++-- app/controllers/producers_controller.rb | 1 + app/controllers/shops_controller.rb | 1 + app/helpers/injection_helper.rb | 2 +- .../api/enterprise_shopfront_list_serializer.rb | 2 +- app/serializers/api/enterprise_thin_serializer.rb | 2 +- app/views/producers/_fat.html.haml | 2 +- app/views/producers/index.html.haml | 2 +- app/views/shops/_filters.html.haml | 2 +- .../darkswarm/services/enterprise_spec.js.coffee | 12 ++---------- 12 files changed, 19 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee index 04c8e8c0b9..0d2f244b32 100644 --- a/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee @@ -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 \ No newline at end of file diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index 6d667e0001..d3b7deec3e 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -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: -> diff --git a/app/assets/javascripts/darkswarm/services/map.js.coffee b/app/assets/javascripts/darkswarm/services/map.js.coffee index f4bad04acc..45370061fd 100644 --- a/app/assets/javascripts/darkswarm/services/map.js.coffee +++ b/app/assets/javascripts/darkswarm/services/map.js.coffee @@ -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) -> diff --git a/app/controllers/producers_controller.rb b/app/controllers/producers_controller.rb index 4dd7800003..0f45546fde 100644 --- a/app/controllers/producers_controller.rb +++ b/app/controllers/producers_controller.rb @@ -6,6 +6,7 @@ class ProducersController < BaseController def index @enterprises = Enterprise .activated + .visible .is_primary_producer .includes(address: :state) .includes(:properties) diff --git a/app/controllers/shops_controller.rb b/app/controllers/shops_controller.rb index 25c5995170..611baa11b8 100644 --- a/app/controllers/shops_controller.rb +++ b/app/controllers/shops_controller.rb @@ -6,6 +6,7 @@ class ShopsController < BaseController def index @enterprises = Enterprise .activated + .visible .is_distributor .includes(address: :state) .includes(:properties) diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 748aed6510..e83a1dbe36 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -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 diff --git a/app/serializers/api/enterprise_shopfront_list_serializer.rb b/app/serializers/api/enterprise_shopfront_list_serializer.rb index a8ded21bf1..dd83ce4323 100644 --- a/app/serializers/api/enterprise_shopfront_list_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_list_serializer.rb @@ -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 diff --git a/app/serializers/api/enterprise_thin_serializer.rb b/app/serializers/api/enterprise_thin_serializer.rb index 7127ab12bd..cdc4d6b9eb 100644 --- a/app/serializers/api/enterprise_thin_serializer.rb +++ b/app/serializers/api/enterprise_thin_serializer.rb @@ -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 diff --git a/app/views/producers/_fat.html.haml b/app/views/producers/_fat.html.haml index 2dc7454671..1526f4f198 100644 --- a/app/views/producers/_fat.html.haml +++ b/app/views/producers/_fat.html.haml @@ -78,7 +78,7 @@   .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"} diff --git a/app/views/producers/index.html.haml b/app/views/producers/index.html.haml index 1558fc131c..c6b62b2725 100644 --- a/app/views/producers/index.html.haml +++ b/app/views/producers/index.html.haml @@ -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}}"} diff --git a/app/views/shops/_filters.html.haml b/app/views/shops/_filters.html.haml index 33d5ac3a5e..1e62405be3 100644 --- a/app/views/shops/_filters.html.haml +++ b/app/views/shops/_filters.html.haml @@ -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 diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee index 120e5c8032..8b393a6aab 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee @@ -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]