diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 23cec57c40..3452f620c8 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -205,92 +205,58 @@ class Enterprise < ActiveRecord::Base 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. + # Explanation: I added and extra flag then pared it back to a combo, before realising we allready had one. + # Short version: meets front end and back end needs, without changing much at all. # - # 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. + # Ditch is_distributor, add can_supply and swap combo names as below. + # using profile instead of cant_sell was blocking the non selling supplier case and limiting more than it needed to. + # 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") + # This can be simplified later, like this for readablitlty during changes. + category = is_primary_producer ? "producer_" : "non_producer_" + case type + when "full" + category << "sell_all_" + when "single" + category << "sell_own_" + when "profile" + category << "cant_sell_" + end + category << (can_supply ? "can_supply" : "cant_supply") # Map backend cases to front end cases. case category - when "producer_with_shopfront_can_aggregate_can_supply" + when "producer_sell_all_can_supply" "producer_hub" # Producer hub who sells own and others produce and supplies other hubs. - when "producer_with_shopfront_can_aggregate_cant_supply" + when "producer_sell_all_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" + when "producer_sell_own_can_supply" "producer_shop" # Producer with shopfront and supplies other hubs. - when "producer_with_shopfront_cant_aggregate_cant_supply" + when "producer_sell_own_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" + when "producer_cant_sell_can_supply" "producer" # Producer selling only through others. - when "producer_without_shopfront_cant_aggregate_cant_supply" + when "producer_cant_sell_cant_supply" "producer_profile" # Producer profile. - when "non_producer_with_shopfront_can_aggregate_can_supply" + when "non_producer_sell_all_can_supply" "hub" # Hub selling in products without origin. - when "non_producer_with_shopfront_can_aggregate_cant_supply" + when "non_producer_sell_all_cant_supply" "hub" # Regular hub. - when "non_producer_with_shopfront_cant_aggregate_can_supply" + when "non_producer_sell_own_can_supply" "hub" # Wholesaler selling through own shopfront and others? - when "non_producer_with_shopfront_cant_aggregate_cant_supply" + when "non_producer_sell_own_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" + when "non_producer_cant_sell_can_supply" "hub_profile" # Wholesaler selling to others. - when "non_producer_without_shopfront_cant_aggregate_cant_supply" + when "non_producer_cant_sell_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 + # Obviously this is duplicated is producer currently, needs to def can_supply is_primary_producer #and has distributors? end @@ -311,7 +277,6 @@ class Enterprise < ActiveRecord::Base select('DISTINCT spree_taxons.*') end - private def strip_url(url) diff --git a/app/serializers/api/enterprise_serializer.rb b/app/serializers/api/enterprise_serializer.rb index 4667862a3c..77627efffe 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, :icon_font, :producer_icon_font, :has_shopfront, :can_aggregate, :enterprise_category + attributes :icon, :icon_font, :producer_icon_font, :has_shopfront, :enterprise_category def orders_close_at OrderCycle.first_closing_for(object).andand.orders_close_at @@ -28,33 +28,13 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer @options[:active_distributors].andand.include? object 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 + def has_shopfront + object.type != 'profile' end # Map svg icons. @@ -66,7 +46,6 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer "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 @@ -80,7 +59,6 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer "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 @@ -103,36 +81,6 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer 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' - end - - # TODO: Remove this when flags on enterprises are switched over - def can_aggregate - object.is_distributor && object.suppliers != [object] - end - -end - class Api::CachedEnterpriseSerializer < ActiveModel::Serializer cached delegate :cache_key, to: :object diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index c517a3ed41..25282dcb69 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -466,4 +466,65 @@ describe Enterprise do supplier.producer_properties.first.property.presentation.should == 'Organic Certified' end end + + describe "provide enterprise category" do + + # Swap type values full > sell_all, single > sell_own profile > sell_none + # swap is_distributor for new can_supply flag. + let(:producer_sell_all_can_supply) { + create(:enterprise, is_primary_producer: true, type: "full", is_distributor: true) + } + let(:producer_sell_all_cant_supply) { + create(:enterprise, is_primary_producer: true, type: "full", is_distributor: false) + } + let(:producer_sell_own_can_supply) { + create(:enterprise, is_primary_producer: true, type: "single", is_distributor: true) + } + let(:producer_sell_own_cant_supply) { + create(:enterprise, is_primary_producer: true, type: "single", is_distributor: false) + } + let(:producer_cant_sell_can_supply) { + create(:enterprise, is_primary_producer: true, type: "profile", is_distributor: true) + } + let(:producer_cant_sell_cant_supply) { + create(:enterprise, is_primary_producer: true, type: "profile", is_distributor: false) + } + let(:non_producer_sell_all_can_supply) { + create(:enterprise, is_primary_producer: true, type: "full", is_distributor: true) + } + let(:non_producer_sell_all_cant_supply) { + create(:enterprise, is_primary_producer: true, type: "full", is_distributor: false) + } + let(:non_producer_sell_own_can_supply) { + create(:enterprise, is_primary_producer: true, type: "single", is_distributor: true) + } + let(:non_producer_sell_own_cant_supply) { + create(:enterprise, is_primary_producer: true, type: "single", is_distributor: false) + } + let(:non_producer_cant_sell_can_supply) { + create(:enterprise, is_primary_producer: false, type: "profile", is_distributor: true) + } + let(:non_producer_cant_sell_cant_supply) { + create(:enterprise, is_primary_producer: false, type: "profile", is_distributor: false) + } + + it "should output enterprise categories" do + producer_sell_all_can_supply.is_primary_producer.should == true + producer_sell_all_can_supply.can_supply.should == true + producer_sell_all_can_supply.type.should == "full" + + producer_sell_all_can_supply.enterprise_category.should == "producer_hub" + producer_sell_all_cant_supply.enterprise_category.should == "producer_hub" + producer_sell_own_can_supply.enterprise_category.should == "producer_shop" + producer_sell_own_cant_supply.enterprise_category.should == "producer_shop" + producer_cant_sell_can_supply.enterprise_category.should == "producer" + producer_cant_sell_cant_supply.enterprise_category.should == "producer_profile" + non_producer_sell_all_can_supply.enterprise_category.should == "hub" + non_producer_sell_all_cant_supply.enterprise_category.should == "hub" + non_producer_sell_own_can_supply.enterprise_category.should == "hub" + non_producer_sell_own_cant_supply.enterprise_category.should == "hub" + non_producer_cant_sell_can_supply.enterprise_category.should == "hub_profile" + non_producer_cant_sell_cant_supply.enterprise_category.should == "hub_profile" + end + end end