Merge pull request #8420 from Matt-Yorkley/customer-names-migration

Customer names preference migration
This commit is contained in:
Matt-Yorkley
2021-11-04 14:52:57 +00:00
committed by GitHub
15 changed files with 79 additions and 21 deletions

View File

@@ -77,7 +77,7 @@ class ProducerMailer < Spree::BaseMailer
end
def set_customer_data(line_items)
return unless @coordinator.preferred_show_customer_names_to_suppliers
return unless @coordinator.show_customer_names_to_suppliers?
line_items.map do |line_item|
{

View File

@@ -15,7 +15,6 @@ class Enterprise < ApplicationRecord
preference :shopfront_taxon_order, :string, default: ""
preference :shopfront_producer_order, :string, default: ""
preference :shopfront_order_cycle_order, :string, default: "orders_close_at"
preference :show_customer_names_to_suppliers, :boolean, default: false
preference :shopfront_product_sorting_method, :string, default: "by_category"
# Allow hubs to restrict visible variants to only those in their inventory

View File

@@ -8,7 +8,7 @@ module Api
:long_description, :preferred_product_selection_from_inventory_only,
:preferred_shopfront_message, :preferred_shopfront_closed_message,
:preferred_shopfront_taxon_order, :preferred_shopfront_producer_order,
:preferred_shopfront_order_cycle_order, :preferred_show_customer_names_to_suppliers,
:preferred_shopfront_order_cycle_order, :show_customer_names_to_suppliers,
:preferred_shopfront_product_sorting_method, :owner, :contact, :users, :tag_groups,
:default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes,
:logo, :promo_image, :terms_and_conditions,

View File

@@ -15,7 +15,7 @@ class OrderDataMasker
attr_accessor :order
def customer_names_allowed?
order.distributor.preferences[:show_customer_names_to_suppliers]
order.distributor.show_customer_names_to_suppliers
end
def mask_customer_names

View File

@@ -32,7 +32,7 @@ module PermittedAttributes
:preferred_product_selection_from_inventory_only, :preferred_shopfront_message,
:preferred_shopfront_closed_message, :preferred_shopfront_taxon_order,
:preferred_shopfront_producer_order, :preferred_shopfront_order_cycle_order,
:preferred_show_customer_names_to_suppliers, :preferred_shopfront_product_sorting_method,
:show_customer_names_to_suppliers, :preferred_shopfront_product_sorting_method,
]
end
end

View File

@@ -103,8 +103,8 @@
%div{'ofn-with-tip' => t('.customer_names_tip')}
%a= t 'admin.whats_this'
.three.columns
= radio_button :enterprise, :preferred_show_customer_names_to_suppliers, true
= label :enterprise_preferred_show_customer_names_to_suppliers, t('.customer_names_true'), value: :true
= radio_button :enterprise, :show_customer_names_to_suppliers, true
= label :enterprise_show_customer_names_to_suppliers, t('.customer_names_true'), value: :true
.five.columns.omega
= radio_button :enterprise, :preferred_show_customer_names_to_suppliers, false
= label :enterprise_preferred_show_customer_names_to_suppliers, t('.customer_names_false'), value: :false
= radio_button :enterprise, :show_customer_names_to_suppliers, false
= label :enterprise_show_customer_names_to_suppliers, t('.customer_names_false'), value: :false

View File

@@ -0,0 +1,6 @@
class AddCustomerNamesToEnterprise < ActiveRecord::Migration[6.1]
def change
add_column :enterprises, :show_customer_names_to_suppliers,
:boolean, null: false, default: false
end
end

View File

@@ -0,0 +1,22 @@
class MigrateCustomerNames < ActiveRecord::Migration[6.1]
class Enterprise < ActiveRecord::Base
scope :showing_customer_names, -> do
joins(
<<-SQL
JOIN spree_preferences ON (
value LIKE '--- true\n%'
AND spree_preferences.key = CONCAT('/enterprise/show_customer_names_to_suppliers/', enterprises.id)
)
SQL
)
end
end
def up
migrate_customer_names_preferences!
end
def migrate_customer_names_preferences!
Enterprise.showing_customer_names.update_all(show_customer_names_to_suppliers: true)
end
end

View File

@@ -210,6 +210,7 @@ ActiveRecord::Schema.define(version: 2021_10_29_174211) do
t.integer "terms_and_conditions_file_size"
t.datetime "terms_and_conditions_updated_at"
t.integer "business_address_id"
t.boolean "show_customer_names_to_suppliers", default: false, 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

View File

@@ -165,11 +165,11 @@ describe Admin::EnterprisesController, type: :controller do
it "updates enterprise preferences" do
allow(controller).to receive_messages spree_current_user: distributor_manager
update_params = { id: distributor,
enterprise: { preferred_show_customer_names_to_suppliers: "1" } }
enterprise: { show_customer_names_to_suppliers: "1" } }
spree_post :update, update_params
distributor.reload
expect(distributor.preferences[:show_customer_names_to_suppliers]).to eq true
expect(distributor.show_customer_names_to_suppliers).to eq true
end
describe "enterprise properties" do

View File

@@ -82,7 +82,7 @@ describe OpenFoodNetwork::OrdersAndFulfillmentsReport do
context "where the distributor allows suppliers to see customer names" do
before do
distributor.preferred_show_customer_names_to_suppliers = true
distributor.update_columns show_customer_names_to_suppliers: true
end
it "shows line items supplied by my producers, with names shown" do
@@ -117,7 +117,7 @@ describe OpenFoodNetwork::OrdersAndFulfillmentsReport do
context "where the distributor allows suppliers to see customer names" do
before do
distributor.preferred_show_customer_names_to_suppliers = true
distributor.show_customer_names_to_suppliers = true
end
it "does not show line items supplied by my producers" do

View File

@@ -66,7 +66,7 @@ module OpenFoodNetwork
context "where the distributor allows suppliers to see customer names" do
before do
distributor.preferred_show_customer_names_to_suppliers = true
distributor.update_columns show_customer_names_to_suppliers: true
end
it "shows line items supplied by my producers, with names shown" do
@@ -96,7 +96,7 @@ module OpenFoodNetwork
context "where the distributor allows suppliers to see customer names" do
before do
distributor.preferred_show_customer_names_to_suppliers = true
distributor.show_customer_names_to_suppliers = true
end
it "does not show line items supplied by my producers" do

View File

@@ -128,9 +128,9 @@ describe ProducerMailer, type: :mailer do
expect(mail.body.encoded).to include(p1.name)
end
context 'when flag preferred_show_customer_names_to_suppliers is true' do
context 'when flag show_customer_names_to_suppliers is true' do
before do
order_cycle.coordinator.set_preference(:show_customer_names_to_suppliers, true)
order_cycle.coordinator.show_customer_names_to_suppliers = true
end
it "adds customer names table" do
@@ -160,9 +160,9 @@ describe ProducerMailer, type: :mailer do
end
end
context 'when flag preferred_show_customer_names_to_suppliers is false' do
context 'when flag show_customer_names_to_suppliers is false' do
before do
order_cycle.coordinator.set_preference(:show_customer_names_to_suppliers, false)
order_cycle.coordinator.show_customer_names_to_suppliers = false
end
it "does not add customer names table" do

View File

@@ -0,0 +1,30 @@
# frozen_string_literal: true
require 'spec_helper'
require_relative '../../db/migrate/20211027140313_migrate_customer_names'
describe MigrateCustomerNames do
subject { MigrateCustomerNames.new }
let!(:enterprise1) { create(:enterprise) }
let!(:enterprise2) { create(:enterprise) }
let!(:enterprise3) { create(:enterprise) }
let!(:enterprise4) { create(:enterprise) }
before do
Spree::Preference.create(value: true, value_type: "boolean", key: "/enterprise/show_customer_names_to_suppliers/#{enterprise1.id}")
Spree::Preference.create(value: false, value_type: "boolean", key: "/enterprise/show_customer_names_to_suppliers/#{enterprise2.id}")
Spree::Preference.create(value: true, value_type: "boolean", key: "/enterprise/show_customer_names_to_suppliers/#{enterprise4.id}")
end
describe '#migrate_customer_names_preferences!' do
it "migrates the preference to the enterprise" do
subject.migrate_customer_names_preferences!
expect(enterprise1.reload.show_customer_names_to_suppliers?).to be true
expect(enterprise2.reload.show_customer_names_to_suppliers?).to be false
expect(enterprise3.reload.show_customer_names_to_suppliers?).to be false # was nil
expect(enterprise4.reload.show_customer_names_to_suppliers?).to be true
end
end
end

View File

@@ -8,7 +8,7 @@ describe OrderDataMasker do
let(:order) { create(:order, distributor: distributor, ship_address: create(:address)) }
context 'when displaying customer names is allowed' do
before { distributor.preferences[:show_customer_names_to_suppliers] = true }
before { distributor.show_customer_names_to_suppliers = true }
it 'masks personal addresses and email' do
described_class.new(order).call
@@ -49,7 +49,7 @@ describe OrderDataMasker do
end
context 'when displaying customer names is not allowed' do
before { distributor.preferences[:show_customer_names_to_suppliers] = false }
before { distributor.show_customer_names_to_suppliers = false }
it 'masks personal addresses and email' do
described_class.new(order).call