From 898c470c00d8017f5319efd6ac63902b0553aa2b Mon Sep 17 00:00:00 2001 From: GeorgeThoppil Date: Tue, 12 Apr 2022 21:33:36 -0400 Subject: [PATCH] Change enterprise visibility scope --- app/models/enterprise.rb | 9 +++++++-- .../api/admin/enterprise_serializer.rb | 4 ++-- .../form/_primary_details.html.haml | 13 +++++++----- .../single_enterprise_dashboard.html.haml | 2 +- config/locales/en.yml | 7 ++++--- ...hange_visible_data_type_for_enterprises.rb | 20 +++++++++++++++++++ db/schema.rb | 4 ++-- .../enterprise_issue_validator.rb | 2 +- spec/factories/enterprise_factory.rb | 6 ++++++ .../enterprise_issue_validator_spec.rb | 2 +- .../enterprise_shopfront_serializer_spec.rb | 11 +++++++++- spec/system/admin/enterprises_spec.rb | 2 +- spec/system/admin/overview_spec.rb | 2 +- spec/system/consumer/groups_spec.rb | 18 +++++++++++++---- 14 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 db/migrate/20220410162955_change_visible_data_type_for_enterprises.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index d27d605d1d..1e73ae448f 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -120,7 +120,8 @@ class Enterprise < ApplicationRecord after_rollback :restore_permalink scope :by_name, -> { order('name') } - scope :visible, -> { where(visible: true) } + scope :visible, -> { where(visible: "public") } + scope :not_hidden, -> { where.not(visible: "hidden") } scope :activated, -> { where("sells != 'unspecified'") } scope :ready_for_checkout, lambda { joins(:shipping_methods). @@ -267,7 +268,7 @@ class Enterprise < ApplicationRecord def plus_relatives_and_oc_producers(order_cycles) oc_producer_ids = Exchange.in_order_cycle(order_cycles).incoming.pluck :sender_id - Enterprise.is_primary_producer.relatives_of_one_union_others(id, oc_producer_ids | [id]) + Enterprise.not_hidden.is_primary_producer.relatives_of_one_union_others(id, oc_producer_ids | [id]) end def relatives_including_self @@ -401,6 +402,10 @@ class Enterprise < ApplicationRecord abn.present? end + def public? + visible == "public" + end + protected def devise_mailer diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index 2cef7c454c..019250f634 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -13,8 +13,8 @@ module Api :default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes, :logo, :promo_image, :terms_and_conditions, :terms_and_conditions_file_name, :terms_and_conditions_updated_at, - :preferred_invoice_order_by_supplier, - :preferred_product_low_stock_display + :preferred_invoice_order_by_supplier, :preferred_product_low_stock_display, + :visible has_one :owner, serializer: Api::Admin::UserSerializer has_many :users, serializer: Api::Admin::UserSerializer diff --git a/app/views/admin/enterprises/form/_primary_details.html.haml b/app/views/admin/enterprises/form/_primary_details.html.haml index 28507311ea..ac7f37b584 100644 --- a/app/views/admin/enterprises/form/_primary_details.html.haml +++ b/app/views/admin/enterprises/form/_primary_details.html.haml @@ -42,11 +42,14 @@ %div{'ofn-with-tip' => t('.visible_in_search_tip')} %a= t('admin.whats_this') .two.columns - = f.radio_button :visible, true - = f.label :visible, t('.visible'), value: 'true' - .five.columns.omega - = f.radio_button :visible, false - = f.label :visible, t('.not_visible'), value: 'false' + = f.radio_button :visible, "public", 'ng-model' => 'Enterprise.visible' + = f.label :visible, t('.visible'), value: 'public' + .two.columns + = f.radio_button :visible, "only_through_links", 'ng-model' => 'Enterprise.visible' + = f.label :visible, t('.not_visible'), value: 'only_through_links' + .four.columns.omega + = f.radio_button :visible, "hidden", 'ng-model' => 'Enterprise.visible' + = f.label :visible, t('.hidden'), value: 'hidden' .permalink{ ng: { controller: "permalinkCtrl" } } .row{ ng: { show: "Enterprise.sells == 'own' || Enterprise.sells == 'any'" } } .three.columns.alpha diff --git a/app/views/spree/admin/overview/single_enterprise_dashboard.html.haml b/app/views/spree/admin/overview/single_enterprise_dashboard.html.haml index 3c9bb75b8a..ed0e976821 100644 --- a/app/views/spree/admin/overview/single_enterprise_dashboard.html.haml +++ b/app/views/spree/admin/overview/single_enterprise_dashboard.html.haml @@ -26,7 +26,7 @@ #package_selection{ hidden: true } = render partial: "/admin/enterprises/change_type_form" -- if !@enterprise.visible +- unless @enterprise.public? .alert-box %strong = t "spree_admin_single_enterprise_hint" diff --git a/config/locales/en.yml b/config/locales/en.yml index 8cd8a7db00..78a009b389 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -922,9 +922,10 @@ en: sells: Sells sells_tip: "None - enterprise does not sell to customers directly.
Own - Enterprise sells own products to customers.
Any - Enterprise can sell own or other enterprises products.
" visible_in_search: Visible in search? - visible_in_search_tip: Determines whether this enterprise will be visible to customers when searching the site. - visible: Visible - not_visible: Not visible + visible_in_search_tip: "Shops can be
1. publicly visible, appearing on the OFN map and listings.
2. Hidden on maps and listings but referenced by other shops and linked in their profile.
3. Completely hidden." + visible: Public + not_visible: Hidden + hidden: Hide all references permalink: Permalink (no spaces) permalink_tip: "This permalink is used to create the url to your shop: %{link}your-shop-name/shop" link_to_front: Link to shop front diff --git a/db/migrate/20220410162955_change_visible_data_type_for_enterprises.rb b/db/migrate/20220410162955_change_visible_data_type_for_enterprises.rb new file mode 100644 index 0000000000..f28c048c91 --- /dev/null +++ b/db/migrate/20220410162955_change_visible_data_type_for_enterprises.rb @@ -0,0 +1,20 @@ +class ChangeVisibleDataTypeForEnterprises < ActiveRecord::Migration[6.1] + class Enterprise < ActiveRecord::Base; end + def up + add_column :enterprises, :visible_tmp, :string, limit: 255, default: "public", null: false + Enterprise.reset_column_information + + Enterprise.where(:visible => 0).update_all(visible_tmp: "only_through_links") + remove_column :enterprises, :visible + rename_column :enterprises, :visible_tmp, :visible + end + + def down + add_column :enterprises, :visible_tmp, :boolean, default: true, null: false + Enterprise.reset_column_information + + Enterprise.where("visible != 'public'").update_all(visible_tmp: false) + remove_column :enterprises, :visible + rename_column :enterprises, :visible_tmp, :visible + end +end diff --git a/db/schema.rb b/db/schema.rb index dbd9b1b3cf..6ce6c4dc61 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_07_051248) do +ActiveRecord::Schema.define(version: 2022_04_10_162955) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,7 +191,6 @@ ActiveRecord::Schema.define(version: 2022_04_07_051248) do t.string "promo_image_content_type", limit: 255 t.integer "promo_image_file_size" t.datetime "promo_image_updated_at" - t.boolean "visible", default: true t.string "facebook", limit: 255 t.string "instagram", limit: 255 t.string "linkedin", limit: 255 @@ -213,6 +212,7 @@ ActiveRecord::Schema.define(version: 2022_04_07_051248) do t.datetime "terms_and_conditions_updated_at" t.integer "business_address_id" t.boolean "show_customer_names_to_suppliers", default: false, null: false + t.string "visible", limit: 255, default: "public", null: false t.index ["address_id"], name: "index_enterprises_on_address_id" t.index ["is_primary_producer", "sells"], name: "index_enterprises_on_is_primary_producer_and_sells" t.index ["name"], name: "index_enterprises_on_name", unique: true diff --git a/lib/open_food_network/enterprise_issue_validator.rb b/lib/open_food_network/enterprise_issue_validator.rb index b69a7e6f36..538a0d5205 100644 --- a/lib/open_food_network/enterprise_issue_validator.rb +++ b/lib/open_food_network/enterprise_issue_validator.rb @@ -43,7 +43,7 @@ module OpenFoodNetwork def warnings warnings = [] - unless @enterprise.visible + unless @enterprise.public? warnings << { description: I18n.t('admin.enterprise_issues.not_visible', enterprise: @enterprise.name), link: "#{I18n.t(:edit)}" diff --git a/spec/factories/enterprise_factory.rb b/spec/factories/enterprise_factory.rb index 48d87ccdcd..736899e565 100644 --- a/spec/factories/enterprise_factory.rb +++ b/spec/factories/enterprise_factory.rb @@ -28,6 +28,12 @@ FactoryBot.define do sells { "none" } end + factory :supplier_enterprise_hidden, parent: :enterprise do + is_primary_producer { true } + sells { "none" } + visible { "hidden" } + end + factory :distributor_enterprise, parent: :enterprise do is_primary_producer { false } sells { "any" } diff --git a/spec/lib/open_food_network/enterprise_issue_validator_spec.rb b/spec/lib/open_food_network/enterprise_issue_validator_spec.rb index 617b9ec73c..8630dfcd36 100644 --- a/spec/lib/open_food_network/enterprise_issue_validator_spec.rb +++ b/spec/lib/open_food_network/enterprise_issue_validator_spec.rb @@ -6,7 +6,7 @@ require 'open_food_network/enterprise_issue_validator' module OpenFoodNetwork describe EnterpriseIssueValidator do describe "warnings" do - let(:enterprise_invisible) { create(:enterprise, visible: false) } + let(:enterprise_invisible) { create(:enterprise, visible: "only_through_links") } let(:warnings) { EnterpriseIssueValidator.new(enterprise_invisible).warnings } it "reports invisible enterprises" do diff --git a/spec/serializers/api/enterprise_shopfront_serializer_spec.rb b/spec/serializers/api/enterprise_shopfront_serializer_spec.rb index ca7d8ea013..1c2ce03ea0 100644 --- a/spec/serializers/api/enterprise_shopfront_serializer_spec.rb +++ b/spec/serializers/api/enterprise_shopfront_serializer_spec.rb @@ -5,13 +5,18 @@ require 'spec_helper' describe Api::EnterpriseShopfrontSerializer do let!(:hub) { create(:distributor_enterprise, with_payment_and_shipping: true) } let!(:producer) { create(:supplier_enterprise) } + let!(:producer_hidden) { create(:supplier_enterprise_hidden) } let!(:relationship) { create(:enterprise_relationship, parent: hub, child: producer) } + let!(:relationship2) { create(:enterprise_relationship, parent: hub, child: producer_hidden) } let!(:taxon1) { create(:taxon, name: 'Meat') } let!(:taxon2) { create(:taxon, name: 'Veg') } let!(:product) { create(:product, supplier: producer, primary_taxon: taxon1, taxons: [taxon1, taxon2] ) } + let!(:product2) { + create(:product, supplier: producer_hidden, primary_taxon: taxon1, taxons: [taxon1, taxon2] ) + } let(:close_time) { 2.days.from_now } let!(:oc) { create(:simple_order_cycle, orders_close_at: close_time, distributors: [hub]) } @@ -20,6 +25,10 @@ describe Api::EnterpriseShopfrontSerializer do create(:exchange, order_cycle: oc, incoming: false, sender: producer, receiver: hub) } + let!(:ex2) { + create(:exchange, order_cycle: oc, incoming: false, + sender: producer_hidden, receiver: hub) + } let(:serializer) { Api::EnterpriseShopfrontSerializer.new hub } @@ -41,7 +50,7 @@ describe Api::EnterpriseShopfrontSerializer do expect(serializer.serializable_hash[:hubs].to_json).to match hub.name end - it "serializes an array of producers" do + it "serializes an array of producers that are public or linked by links" do expect(serializer.serializable_hash[:producers]).to be_a ActiveModel::ArraySerializer expect(serializer.serializable_hash[:producers].to_json).to match producer.name end diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index ba0d3e31d4..00c244e7b2 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -201,7 +201,7 @@ describe ' expect(page).to have_field 'enterprise_name', with: 'Eaterprises' @enterprise.reload expect(@enterprise.owner).to eq user - expect(page).to have_checked_field "enterprise_visible_true" + expect(page).to have_checked_field "enterprise_visible_public" click_link "Business Details" expect(page).to have_checked_field "enterprise_charges_sales_tax_true" diff --git a/spec/system/admin/overview_spec.rb b/spec/system/admin/overview_spec.rb index f734344736..8d6b748cf2 100644 --- a/spec/system/admin/overview_spec.rb +++ b/spec/system/admin/overview_spec.rb @@ -32,7 +32,7 @@ describe ' context "when visibilty is set to false" do before do - d1.visible = false + d1.visible = "only_through_links" d1.save! end diff --git a/spec/system/consumer/groups_spec.rb b/spec/system/consumer/groups_spec.rb index 210849da60..b9dd459c40 100644 --- a/spec/system/consumer/groups_spec.rb +++ b/spec/system/consumer/groups_spec.rb @@ -71,10 +71,20 @@ describe 'Groups', js: true do coordinator: create(:distributor_enterprise)) } let(:producer) { create(:supplier_enterprise) } - let(:d1) { create(:distributor_enterprise, with_payment_and_shipping: true, visible: true) } - let(:d2) { create(:distributor_enterprise, with_payment_and_shipping: true, visible: true) } - let(:d3) { create(:distributor_enterprise, with_payment_and_shipping: true, visible: false) } - let(:d4) { create(:distributor_enterprise, with_payment_and_shipping: true, visible: true) } + let(:d1) { + create(:distributor_enterprise, with_payment_and_shipping: true, visible: "public") + } + let(:d2) { + create(:distributor_enterprise, with_payment_and_shipping: true, visible: "public") + } + let(:d3) { + create(:distributor_enterprise, + with_payment_and_shipping: true, + visible: "only_through_links") + } + let(:d4) { + create(:distributor_enterprise, with_payment_and_shipping: true, visible: "public") + } let(:p1) { create(:simple_product, supplier: producer) } let(:p2) { create(:simple_product, supplier: create(:supplier_enterprise)) } let(:p3) { create(:simple_product, supplier: create(:supplier_enterprise)) }