From f0a3296e521ef61e285036a5b670085b683ff436 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Thu, 16 Oct 2014 16:22:18 +1100 Subject: [PATCH] change enterprise category name ans use symbols instead of strings --- .../darkswarm/services/enterprises.js.coffee | 4 +- .../darkswarm/services/variants.js.coffee | 3 +- app/models/enterprise.rb | 25 +++++------- .../api/admin/enterprise_serializer.rb | 2 +- app/serializers/api/enterprise_serializer.rb | 38 +++++++++---------- .../_sidebar_enterprise_fees.html.haml | 2 +- .../services/enterprise_spec.js.coffee | 16 ++++---- spec/models/enterprise_spec.rb | 12 +++--- 8 files changed, 48 insertions(+), 54 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index 7e6aa0a1c7..6040f1d150 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -12,9 +12,9 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, @dereferenceTaxons() @visible_enterprises = visibleFilter @enterprises @producers = @visible_enterprises.filter (enterprise)-> - enterprise.enterprise_category in ["producer_hub", "producer_shop", "producer"] + enterprise.category in ["producer_hub", "producer_shop", "producer"] @hubs = @visible_enterprises.filter (enterprise)-> - enterprise.enterprise_category in ["hub", "hub_profile", "producer_hub", "producer_shop"] + enterprise.category in ["hub", "hub_profile", "producer_hub", "producer_shop"] dereferenceEnterprises: -> if CurrentHub.hub?.id diff --git a/app/assets/javascripts/darkswarm/services/variants.js.coffee b/app/assets/javascripts/darkswarm/services/variants.js.coffee index 1b87be5369..6562bd9e0b 100644 --- a/app/assets/javascripts/darkswarm/services/variants.js.coffee +++ b/app/assets/javascripts/darkswarm/services/variants.js.coffee @@ -5,8 +5,7 @@ Darkswarm.factory 'Variants', -> @variants[variant.id] ||= @extend variant extend: (variant)-> - # Add totalPrice and basPricePercentage methods to calculate line item values. - # This should probably be on a line item object! + # Add totalPrice method to calculate line item total. This should be on a line item! variant.totalPrice = -> variant.price_with_fees * variant.line_item.quantity variant.basePricePercentage = Math.round(variant.price / variant.price_with_fees * 100) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c81ac042e5..ab4dccf8ec 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -215,31 +215,26 @@ class Enterprise < ActiveRecord::Base not self.sells == "none" end - def is_hub - self.enterprise_category.in Array['hub', 'hub_profile', 'producer_hub', 'producer_shop'] - end - # Simplify enterprise categories for frontend logic and icons, and maybe other things. - def enterprise_category + def category # Make this crazy logic human readable so we can argue about it sanely. - # This can be simplified later, it's like this for readablitlty during changes. - category = is_primary_producer ? "producer_" : "non_producer_" - category << "sells_" + sells + cat = is_primary_producer ? "producer_" : "non_producer_" + cat << "sells_" + sells # Map backend cases to front end cases. - case category + case cat when "producer_sells_any" - "producer_hub" # Producer hub who sells own and others produce and supplies other hubs. + :producer_hub # Producer hub who sells own and others produce and supplies other hubs. when "producer_sells_own" - "producer_shop" # Producer with shopfront and supplies other hubs. + :producer_shop # Producer with shopfront and supplies other hubs. when "producer_sells_none" - "producer" # Producer only supplies through others. + :producer # Producer only supplies through others. when "non_producer_sells_any" - "hub" # Hub selling others products in order cycles. + :hub # Hub selling others products in order cycles. when "non_producer_sells_own" - "hub" # Wholesaler selling through own shopfront? Does this need a separate name? Should it exist? + :hub # Wholesaler selling through own shopfront? Does this need a separate name? Should it exist? when "non_producer_sells_none" - "hub_profile" # Hub selling outside the system. + :hub_profile # Hub selling outside the system. end end diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index 5164256424..e6d00b7aae 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -1,3 +1,3 @@ class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer - attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :payment_method_ids, :shipping_method_ids + attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category, :payment_method_ids, :shipping_method_ids end diff --git a/app/serializers/api/enterprise_serializer.rb b/app/serializers/api/enterprise_serializer.rb index 892027c5a0..3e6708b526 100644 --- a/app/serializers/api/enterprise_serializer.rb +++ b/app/serializers/api/enterprise_serializer.rb @@ -36,7 +36,7 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer :long_description, :website, :instagram, :linkedin, :twitter, :facebook, :is_primary_producer, :is_distributor, :phone, :visible, :email, :hash, :logo, :promo_image, :path, :pickup, :delivery, - :icon, :icon_font, :producer_icon_font, :enterprise_category + :icon, :icon_font, :producer_icon_font, :category has_many :distributed_taxons, key: :taxons, serializer: Api::IdSerializer has_many :supplied_taxons, serializer: Api::IdSerializer @@ -78,25 +78,25 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer # 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", - "producer_shop" => "/assets/map_003-producer-shop.svg", - "producer" => "/assets/map_001-producer-only.svg", + :hub => "/assets/map_005-hub.svg", + :hub_profile => "/assets/map_006-hub-profile.svg", + :producer_hub => "/assets/map_005-hub.svg", + :producer_shop => "/assets/map_003-producer-shop.svg", + :producer => "/assets/map_001-producer-only.svg", } - icons[object.enterprise_category] + icons[object.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", + :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", } - icon_fonts[object.enterprise_category] + icon_fonts[object.category] end # Choose producer page icon font - yes, sadly its got to be different. @@ -104,12 +104,12 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer # 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", + :hub => "", + :hub_profile => "", + :producer_hub => "ofn-i_059-producer", + :producer_shop => "ofn-i_059-producer", + :producer => "ofn-i_059-producer", } - icon_fonts[object.enterprise_category] + icon_fonts[object.category] end end diff --git a/app/views/admin/enterprises/_sidebar_enterprise_fees.html.haml b/app/views/admin/enterprises/_sidebar_enterprise_fees.html.haml index b192201528..566d87b832 100644 --- a/app/views/admin/enterprises/_sidebar_enterprise_fees.html.haml +++ b/app/views/admin/enterprises/_sidebar_enterprise_fees.html.haml @@ -1,5 +1,5 @@ - enterprise_fees_color = @enterprise_fees.count > 0 ? "blue" : "red" -.sidebar_item.four.columns.alpha#enterprise_fees{ ng: { show: 'Enterprise.sells != "none" || Enterprise.is_primary_producer' } } +.sidebar_item.four.columns.alpha#enterprise_fees{ ng: { show: 'Enterprise.category != "producer_hub"' } } .four.columns.alpha.header{ class: "#{enterprise_fees_color}" } %span.four.columns.alpha.centered Enterprise Fees .four.columns.alpha.list{ class: "#{enterprise_fees_color}" } diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee index ff051b1d43..94dd7d39d2 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee @@ -5,14 +5,14 @@ describe "Enterprises service", -> {id: 1, name: "test"} ] enterprises = [ - {id: 1, visible: true, enterprise_category: "hub", producers: [{id: 5}], taxons: [{id: 1}]}, - {id: 2, visible: true, enterprise_category: "hub", producers: [{id: 6}]} - {id: 3, visible: true, enterprise_category: "hub_profile"} - {id: 4, visible: false, enterprise_category: "hub", producers: [{id: 7}]} - {id: 5, visible: true, enterprise_category: "producer_hub", hubs: [{id: 1}]}, - {id: 6, visible: true, enterprise_category: "producer_shop", hubs: [{id: 2}]}, - {id: 7, visible: true, enterprise_category: "producer", hubs: [{id: 2}]} - {id: 8, visible: false, enterprise_category: "producer", hubs: [{id: 2}]} + {id: 1, visible: true, category: "hub", producers: [{id: 5}], taxons: [{id: 1}]}, + {id: 2, visible: true, category: "hub", producers: [{id: 6}]} + {id: 3, visible: true, category: "hub_profile"} + {id: 4, visible: false, category: "hub", producers: [{id: 7}]} + {id: 5, visible: true, category: "producer_hub", hubs: [{id: 1}]}, + {id: 6, visible: true, category: "producer_shop", hubs: [{id: 2}]}, + {id: 7, visible: true, category: "producer", hubs: [{id: 2}]} + {id: 8, visible: false, category: "producer", hubs: [{id: 2}]} ] H1: 0 beforeEach -> diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index ccd4b6b75f..6fae275583 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -528,12 +528,12 @@ describe Enterprise do producer_sell_all.is_primary_producer.should == true producer_sell_all.sells.should == "any" - producer_sell_all.enterprise_category.should == "producer_hub" - producer_sell_own.enterprise_category.should == "producer_shop" - producer_sell_none.enterprise_category.should == "producer" - non_producer_sell_all.enterprise_category.should == "hub" - non_producer_sell_own.enterprise_category.should == "hub" - non_producer_sell_none.enterprise_category.should == "hub_profile" + producer_sell_all.category.should == :producer_hub + producer_sell_own.category.should == :producer_shop + producer_sell_none.category.should == :producer + non_producer_sell_all.category.should == :hub + non_producer_sell_own.category.should == :hub + non_producer_sell_none.category.should == :hub_profile end end end