From f33a9e1a5844ef614a8a6accd695f87c3ba347cf Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Fri, 12 Sep 2014 01:45:51 +1000 Subject: [PATCH] 16 enterprise categories from four checks. --- app/models/enterprise.rb | 91 ++++++++++++++ app/serializers/api/enterprise_serializer.rb | 118 +++++++++++++++---- 2 files changed, 184 insertions(+), 25 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 004923097e..23cec57c40 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -204,6 +204,97 @@ class Enterprise < ActiveRecord::Base Spree::Variant.joins(:product => :product_distributions).where('product_distributions.distributor_id=?', self.id) end + def enterprise_category + + # Explanation: We needed to add the can_supply flag because this case (the most common for producers) was not covered: + # Short version: meets front end and back end needs. + # + # when "producer_without_shopfront_cant_aggregate_can_supply" + # "producer" # Producer selling only through others. + # + # And needs to be separate from this case: + # + # when "producer_with_shopfront_cant_aggregate_can_supply" + # "producer_shop" # Producer with shopfront for own products. + # + # And from this case + # + # when "producer_without_shopfront_cant_aggregate_cant_supply" + # "producer_profile" # Producer profile. + # + # And the front end very clearly distinguishes between producers with a shop and without, + # so the combined case was not enough, and it was going to be a bit weird anyway. + # As a side benefit, this also allows non producer suppliers. Yay. + # + # can_aggregate could depend on with_shopfront, as the combination always shouldn't exist. + # + # So it could be a combo box: + # Sell V + # none + # own + # any + # + # So 12 is possible anyway and seems to cover usefull cases. But the variables could be different. + + # Make this crazy logic human readable so we can argue about it sanely. + category = is_primary_producer ? "producer_" : "non_producer_" + category << (has_shopfront ? "with_shopfront_" : "without_shopfront_") + category << (can_aggregate ? "can_aggregate_" : "cant_aggregate_") + category << (can_supply ? "can_supply" : "cant_supply") + + # Map backend cases to front end cases. + case category + when "producer_with_shopfront_can_aggregate_can_supply" + "producer_hub" # Producer hub who sells own and others produce and supplies other hubs. + when "producer_with_shopfront_can_aggregate_cant_supply" + "producer_hub" # Producer hub who sells own and others products but does not supply other hubs. + when "producer_with_shopfront_cant_aggregate_can_supply" + "producer_shop" # Producer with shopfront and supplies other hubs. + when "producer_with_shopfront_cant_aggregate_cant_supply" + "producer_shop" # Producer with shopfront. + when "producer_without_shopfront_can_aggregate_can_supply" + "empty" # Shouldn't exist. + when "producer_without_shopfront_can_aggregate_cant_supply" + "empty" # Shouldn't exist. + when "producer_without_shopfront_cant_aggregate_can_supply" + "producer" # Producer selling only through others. + when "producer_without_shopfront_cant_aggregate_cant_supply" + "producer_profile" # Producer profile. + + when "non_producer_with_shopfront_can_aggregate_can_supply" + "hub" # Hub selling in products without origin. + when "non_producer_with_shopfront_can_aggregate_cant_supply" + "hub" # Regular hub. + when "non_producer_with_shopfront_cant_aggregate_can_supply" + "hub" # Wholesaler selling through own shopfront and others? + when "non_producer_with_shopfront_cant_aggregate_cant_supply" + "hub" # Wholesaler selling through own shopfront? + when "non_producer_without_shopfront_can_aggregate_can_supply" + "empty" # Shouldn't exist? + when "non_producer_without_shopfront_can_aggregate_cant_supply" + "empty" # Shouldn't exist? + when "non_producer_without_shopfront_cant_aggregate_can_supply" + "hub_profile" # Wholesaler selling to others. + when "non_producer_without_shopfront_cant_aggregate_cant_supply" + "hub_profile" # Hub with just a profile. + end + end + + # TODO: Remove this when flags on enterprises are switched over + def has_shopfront + type != 'profile' + end + + # TODO: Remove this when flags on enterprises are switched over + def can_aggregate + is_distributor# && suppliers != [self] + end + + # TODO: Remove this when flags on enterprises are switched over + def can_supply + is_primary_producer #and has distributors? + end + # Return all taxons for all distributed products def distributed_taxons Spree::Taxon. diff --git a/app/serializers/api/enterprise_serializer.rb b/app/serializers/api/enterprise_serializer.rb index c76cffd7d1..4667862a3c 100644 --- a/app/serializers/api/enterprise_serializer.rb +++ b/app/serializers/api/enterprise_serializer.rb @@ -18,7 +18,7 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer attributes :orders_close_at, :active #TODO: Remove these later - attributes :icon, :has_shopfront, :can_aggregate + attributes :icon, :icon_font, :producer_icon_font, :has_shopfront, :can_aggregate, :enterprise_category def orders_close_at OrderCycle.first_closing_for(object).andand.orders_close_at @@ -28,32 +28,99 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer @options[:active_distributors].andand.include? object end - # TODO: Move this back to uncached section when relavant properties are defined on the Enterprise model - def icon - # TODO: Replace with object.has_shopfront when this property exists - if has_shopfront - if can_aggregate - "/assets/map_005-hub.svg" - else - if object.is_distributor - "/assets/map_003-producer-shop.svg" - else - "/assets/map_001-producer-only.svg" - end - end - else - if can_aggregate - "/assets/map_006-hub-profile.svg" - else - if object.is_distributor - "/assets/map_004-producer-shop-profile.svg" - else - "/assets/map_002-producer-only-profile.svg" - end - end - end + # def enterprise_category + # object.enterprise_category + # end + + # # TODO: Remove this when flags on enterprises are switched over + # def has_shopfront + # object.has_shopfront + # end + + # # TODO: Remove this when flags on enterprises are switched over + # def can_aggregate + # object.can_aggregate + # end + + + def enterprise_category + object.enterprise_category end + # todo: remove this when flags on enterprises are switched over + def has_shopfront + object.has_shopfront + end + + # TODO: Remove this when flags on enterprises are switched over + def can_aggregate + object.can_aggregate + end + + # Map svg icons. + def icon + icons = { + "hub" => "/assets/map_005-hub.svg", + "hub_profile" => "/assets/map_006-hub-profile.svg", + "producer_hub" => "/assets/map_005-hub.svg", + "prodshop_shop" => "/assets/map_003-producer-shop.svg", + "producer" => "map_001-producer-only.svg", + "producer_profile" => "/assets/map_002-producer-only-profile.svg", + "empty" => "", + } + icons[object.enterprise_category] + end + + # Choose regular icon font for enterprises. + def icon_font + icon_fonts = { + "hub" => "ofn-i_063-hub", + "hub_profile" => "ofn-i_064-hub-reversed", + "producer_hub" => "ofn-i_063-hub", + "producer_shop" => "ofn-i_059-producer", + "producer" => "ofn-i_059-producer", + "producer_profile" => "ofn-i_060-producer-reversed", + "empty" => "", + } + icon_fonts[object.enterprise_category] + end + + # Choose producser page icon font - yes, sadly its got to be different. + # This duplicates some code but covers the producer page edge case where + # producer-hub has a producer icon without needing to duplicate the category logic in angular. + def producer_icon_font + icon_fonts = { + "hub" => "", + "hub_profile" => "", + "producer_hub" => "ofn-i_059-producer", + "producer_shop" => "ofn-i_059-producer", + "producer" => "ofn-i_059-producer", + "producer_profile" => "ofn-i_060-producer-reversed", + "empty" => "", + } + icon_fonts[object.enterprise_category] + end + +end + +class Api::CachedEnterpriseSerializer < ActiveModel::Serializer + cached + delegate :cache_key, to: :object + + attributes :name, :id, :description, :latitude, :longitude, + :long_description, :website, :instagram, :linkedin, :twitter, + :facebook, :is_primary_producer, :is_distributor, :phone, :visible, + :email, :hash, :logo, :promo_image, :path, + :pickup, :delivery + + has_many :distributed_taxons, key: :taxons, serializer: Api::IdSerializer + has_many :supplied_taxons, serializer: Api::IdSerializer + has_many :distributors, key: :hubs, serializer: Api::IdSerializer + has_many :suppliers, key: :producers, serializer: Api::IdSerializer + + has_one :address, serializer: Api::AddressSerializer + + # TODO: Remove this when flags on enterprises are switched over def has_shopfront object.type != 'profile' @@ -63,6 +130,7 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer def can_aggregate object.is_distributor && object.suppliers != [object] end + end class Api::CachedEnterpriseSerializer < ActiveModel::Serializer