From beb85e862a5def9b3724fd2d7267f6321d71b282 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Sat, 27 Sep 2014 15:21:27 +1000 Subject: [PATCH] get type > sells migration mostly working --- .../admin/enterprises_controller.rb | 15 +++++----- .../admin/overview_controller_decorator.rb | 3 +- app/models/enterprise.rb | 29 ++++++++----------- app/models/spree/ability_decorator.rb | 2 +- app/serializers/api/enterprise_serializer.rb | 11 ++++--- app/views/admin/enterprises/_form.html.haml | 22 ++++++-------- app/views/admin/enterprises/index.html.haml | 5 +--- app/views/json/partials/_enterprise.rabl | 4 +-- .../overview/_enterprises_hubs_tab.html.haml | 2 +- ...140927005043_enterprise_config_refactor.rb | 23 +++++++++++++++ db/schema.rb | 11 ++++--- 11 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 db/migrate/20140927005043_enterprise_config_refactor.rb diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index bde7bff431..bbb8073deb 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -3,8 +3,8 @@ module Admin before_filter :load_enterprise_set, :only => :index before_filter :load_countries, :except => :index before_filter :load_methods_and_fees, :only => [:new, :edit, :update, :create] - before_filter :check_type, only: :update - before_filter :check_bulk_type, only: :bulk_update + before_filter :check_sells, only: :update + before_filter :check_bulk_sells, only: :bulk_update before_filter :override_owner, only: :create before_filter :check_owner, only: :update before_filter :check_bulk_owner, only: :bulk_update @@ -50,7 +50,8 @@ module Admin end def collection - Enterprise.managed_by(spree_current_user).order('is_distributor DESC, is_primary_producer ASC, name') + # TODO is_distributor DESC, + Enterprise.managed_by(spree_current_user).order('is_primary_producer ASC, name') end def collection_actions @@ -63,16 +64,16 @@ module Admin @enterprise_fees = EnterpriseFee.managed_by(spree_current_user).for_enterprise(@enterprise).order(:fee_type, :name).all end - def check_bulk_type + def check_bulk_sells unless spree_current_user.admin? params[:enterprise_set][:collection_attributes].each do |i, enterprise_params| - enterprise_params.delete :type + enterprise_params.delete :sells end end end - def check_type - params[:enterprise].delete :type unless spree_current_user.admin? + def check_sells + params[:enterprise].delete :sells unless spree_current_user.admin? end def override_owner diff --git a/app/controllers/spree/admin/overview_controller_decorator.rb b/app/controllers/spree/admin/overview_controller_decorator.rb index a2288dab88..1cf8cb6f4e 100644 --- a/app/controllers/spree/admin/overview_controller_decorator.rb +++ b/app/controllers/spree/admin/overview_controller_decorator.rb @@ -1,6 +1,7 @@ Spree::Admin::OverviewController.class_eval do def index - @enterprises = Enterprise.managed_by(spree_current_user).order('is_distributor DESC, is_primary_producer ASC, name') + # TODO is_distributor DESC, + @enterprises = Enterprise.managed_by(spree_current_user).order('is_primary_producer ASC, name') @product_count = Spree::Product.active.managed_by(spree_current_user).count @order_cycle_count = OrderCycle.active.managed_by(spree_current_user).count end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index a81655a1de..a078652d68 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,5 +1,5 @@ class Enterprise < ActiveRecord::Base - TYPES = %w(full single profile) + SELLS = %w(none own any) ENTERPRISE_SEARCH_RADIUS = 100 self.inheritance_column = nil @@ -47,7 +47,7 @@ class Enterprise < ActiveRecord::Base validates :name, presence: true - validates :type, presence: true, inclusion: {in: TYPES} + validates :sells, presence: true, inclusion: {in: SELLS} validates :address, presence: true, associated: true validates_presence_of :owner validate :enforce_ownership_limit, if: lambda { owner_id_changed? } @@ -59,7 +59,7 @@ class Enterprise < ActiveRecord::Base scope :by_name, order('name') scope :visible, where(:visible => true) scope :is_primary_producer, where(:is_primary_producer => true) - scope :is_distributor, where(:is_distributor => true) + scope :is_distributor, where('sells != ?', 'none') scope :supplying_variant_in, lambda { |variants| joins(:supplied_products => :variants_including_master).where('spree_variants.id IN (?)', variants).select('DISTINCT enterprises.*') } scope :with_supplied_active_products_on_hand, lambda { joins(:supplied_products) @@ -210,13 +210,8 @@ class Enterprise < ActiveRecord::Base Spree::Variant.joins(:product => :product_distributions).where('product_distributions.distributor_id=?', self.id) end - # Replaces currententerprse type field. - def sells - # Type: full - single - profile becomes Sells: all - own - none - # Remove this return later. - return "none" if !is_distributor || type == "profile" - return "own" if type == "single" || suppliers == [self] - "all" + def is_distributor + not self.sells == "none" end # Simplify enterprise categories for frontend logic and icons, and maybe other things. @@ -224,21 +219,21 @@ class Enterprise < ActiveRecord::Base # 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 << "sell_" + sells + category << "sells_" + sells # Map backend cases to front end cases. case category - when "producer_sell_all" + when "producer_sells_any" "producer_hub" # Producer hub who sells own and others produce and supplies other hubs. - when "producer_sell_own" + when "producer_sells_own" "producer_shop" # Producer with shopfront and supplies other hubs. - when "producer_sell_none" + when "producer_sells_none" "producer" # Producer only supplies through others. - when "non_producer_sell_all" + when "non_producer_sells_any" "hub" # Hub selling others products in order cycles. - when "non_producer_sell_own" + when "non_producer_sells_own" "hub" # Wholesaler selling through own shopfront? - when "non_producer_sell_none" + when "non_producer_sells_none" "hub_profile" # Hub selling outside the system. end end diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 3ce7066867..1391e0485c 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -73,7 +73,7 @@ class AbilityDecorator # during the order creation process from the admin backend order.distributor.nil? || user.enterprises.include?(order.distributor) end - can [:admin, :bulk_management], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor?) + can [:admin, :bulk_management], Spree::Order if user.admin? || user.enterprises.any?(&:is_distributor) can [:admin, :create], Spree::LineItem can [:admin, :index, :read, :create, :edit, :update, :fire], Spree::Payment diff --git a/app/serializers/api/enterprise_serializer.rb b/app/serializers/api/enterprise_serializer.rb index 6841337024..20379d5193 100644 --- a/app/serializers/api/enterprise_serializer.rb +++ b/app/serializers/api/enterprise_serializer.rb @@ -17,8 +17,7 @@ end class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer attributes :orders_close_at, :active - #TODO: Remove these later - attributes :icon, :icon_font, :producer_icon_font, :has_shopfront, :has_hub_listing, :enterprise_category + attributes :icon, :icon_font, :producer_icon_font, :has_shopfront, :has_hub_listing, :enterprise_category, :is_distributor def orders_close_at OrderCycle.first_closing_for(object).andand.orders_close_at @@ -33,12 +32,16 @@ class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer end def has_shopfront - object.is_distributor && object.type != 'profile' + object.is_distributor + end + + def is_distributor + object.is_distributor end # Used to select enterprises for hub listing def has_hub_listing - has_shopfront || object.enterprise_category == "hub_profile" + object.is_distributor || object.enterprise_category == "hub_profile" end # Map svg icons. diff --git a/app/views/admin/enterprises/_form.html.haml b/app/views/admin/enterprises/_form.html.haml index 6f86c3e703..8afa2750fa 100644 --- a/app/views/admin/enterprises/_form.html.haml +++ b/app/views/admin/enterprises/_form.html.haml @@ -33,12 +33,8 @@ .row .three.columns.alpha %label Enterprise Type(s) - .with-tip{'data-powertip' => "Select 'Producer' if you are a primary producer of food. Select 'Hub' if you want a shop-front. You can choose either or both."} + .with-tip{'data-powertip' => "Select 'Producer' if you are a primary producer of food."} %a What's this? - .two.columns - = f.check_box :is_distributor, 'ng-model' => 'Enterprise.is_distributor' -   - = f.label :is_distributor, 'Hub' .five.columns.omega = f.check_box :is_primary_producer, 'ng-model' => 'Enterprise.is_primary_producer'   @@ -47,21 +43,21 @@ .row .alpha.eleven.columns .three.columns.alpha - = f.label :type, 'Profile type' - .with-tip{'data-powertip' => "Full - enterprise may have products and relationships.
Single - enterprise may have products but no relationships.
Profile - enterprise has a profile but no products or relationships.
"} + = f.label :sells, 'Sells' + .with-tip{'data-powertip' => "None - enterprise does not sell to customers directly.
Own - Enterprise sells own products to customers.
Any - Enterprise can sell own or other enterprises products.
"} %a What's this? .two.columns - = f.radio_button :type, "full" + = f.radio_button :sells, "none"   - = f.label :type, "Full", value: "full" + = f.label :sells, "None", value: "none" .two.columns - = f.radio_button :type, "single" + = f.radio_button :sells, "own"   - = f.label :type, "Single", value: "single" + = f.label :sells, "Own", value: "own" .four.columns.omega - = f.radio_button :type, "profile" + = f.radio_button :sells, "any"   - = f.label :type, "Profile", value: "profile" + = f.label :sells, "Any", value: "any" .row .three.columns.alpha %label Visible in search? diff --git a/app/views/admin/enterprises/index.html.haml b/app/views/admin/enterprises/index.html.haml index 03e5335155..79f59d7171 100644 --- a/app/views/admin/enterprises/index.html.haml +++ b/app/views/admin/enterprises/index.html.haml @@ -37,12 +37,9 @@ %td = enterprise_form.check_box :is_primary_producer Producer - %br/ - = enterprise_form.check_box :is_distributor - Hub %td= enterprise_form.check_box :visible - if spree_current_user.admin? - %td= enterprise_form.select :type, Enterprise::TYPES, {}, class: 'select2 fullwidth' + %td= enterprise_form.select :sells, Enterprise::SELLS, {}, class: 'select2 fullwidth' - if spree_current_user.admin? %td= enterprise_form.select :owner_id, enterprise.users.map{ |e| [ e.email, e.id ] }, {}, class: "select2 fullwidth" %td{"data-hook" => "admin_users_index_row_actions"} diff --git a/app/views/json/partials/_enterprise.rabl b/app/views/json/partials/_enterprise.rabl index 5b52898482..b8800e22ae 100644 --- a/app/views/json/partials/_enterprise.rabl +++ b/app/views/json/partials/_enterprise.rabl @@ -21,9 +21,9 @@ node :promo_image do |enterprise| end node :icon do |e| - if e.is_primary_producer? and e.is_distributor? + if e.is_primary_producer and e.is_distributor image_path "map_003-producer-shop.svg" - elsif e.is_primary_producer? + elsif e.is_primary_producer image_path "map_001-producer-only.svg" else image_path "map_005-hub.svg" diff --git a/app/views/spree/admin/overview/_enterprises_hubs_tab.html.haml b/app/views/spree/admin/overview/_enterprises_hubs_tab.html.haml index cb177f9fb3..6cfd9c3bc5 100644 --- a/app/views/spree/admin/overview/_enterprises_hubs_tab.html.haml +++ b/app/views/spree/admin/overview/_enterprises_hubs_tab.html.haml @@ -8,7 +8,7 @@ - if can? :admin, EnterpriseFee %span.centered.three.columns Enterprise Fees %div.sixteen.columns.alpha.list - - @enterprises.is_distributor.each do |enterprise| + - @enterprises.each do |enterprise| %a.sixteen.columns.alpha.list-item{ class: "#{cycle('odd','even')}", href: "#{main_app.edit_admin_enterprise_path(enterprise)}" } %span.five.columns.alpha = enterprise.name diff --git a/db/migrate/20140927005043_enterprise_config_refactor.rb b/db/migrate/20140927005043_enterprise_config_refactor.rb new file mode 100644 index 0000000000..636b535be3 --- /dev/null +++ b/db/migrate/20140927005043_enterprise_config_refactor.rb @@ -0,0 +1,23 @@ +class EnterpriseConfigRefactor < ActiveRecord::Migration + def up + add_column :enterprises, :sells, :string, null: false, default: 'none' + + Enterprise.all do |enterprise| + enterprise.sells = sells_what?(enterprise) + enterprise.save! + end + + remove_column :enterprises, :type + remove_column :enterprises, :is_distributor + end + + def down + end + + #TODO make this work + def sells_what?(enterprise) + return "none" if !enterprise.is_distributor || enterprise.type == "profile" + return "own" if enterprise.type == "single" || enterprise.suppliers == [enterprise] + return "any" + end +end diff --git a/db/schema.rb b/db/schema.rb index 73f10210a7..67084cc347 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140904003026) do +ActiveRecord::Schema.define(:version => 20140927005043) do create_table "adjustment_metadata", :force => true do |t| t.integer "adjustment_id" @@ -238,7 +238,6 @@ ActiveRecord::Schema.define(:version => 20140904003026) do t.string "description" t.text "long_description" t.boolean "is_primary_producer" - t.boolean "is_distributor" t.string "contact" t.string "phone" t.string "email" @@ -249,8 +248,8 @@ ActiveRecord::Schema.define(:version => 20140904003026) do t.integer "address_id" t.string "pickup_times" t.string "next_collection_at" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.text "distributor_info" t.string "logo_file_name" t.string "logo_content_type" @@ -264,8 +263,8 @@ ActiveRecord::Schema.define(:version => 20140904003026) do t.string "facebook" t.string "instagram" t.string "linkedin" - t.string "type", :default => "profile", :null => false - t.integer "owner_id", :null => false + t.integer "owner_id", :null => false + t.string "sells", :default => "none", :null => false end add_index "enterprises", ["address_id"], :name => "index_enterprises_on_address_id"