diff --git a/config/settings.yml b/config/settings.yml index 84c592d..4fa90f0 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1,6 +1,6 @@ discourse_affiliate: affiliate_enabled: - default: true + default: false client: true affiliate_amazon_ca: default: "" diff --git a/db/migrate/20250721192553_enable_affiliate_if_already_installed.rb b/db/migrate/20250721192553_enable_affiliate_if_already_installed.rb new file mode 100644 index 0000000..bd691a6 --- /dev/null +++ b/db/migrate/20250721192553_enable_affiliate_if_already_installed.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +class EnableAffiliateIfAlreadyInstalled < ActiveRecord::Migration[7.2] + CONFIG_SETTINGS = %w[ + affiliate_amazon_ca + affiliate_amazon_cn + affiliate_amazon_co_jp + affiliate_amazon_co_uk + affiliate_amazon_com + affiliate_amazon_com_au + affiliate_amazon_com_br + affiliate_amazon_com_mx + affiliate_amazon_de + affiliate_amazon_es + affiliate_amazon_fr + affiliate_amazon_in + affiliate_amazon_it + affiliate_amazon_nl + affiliate_amazon_eu + affiliate_ldlc_com + ].freeze + + def up + is_configured = DB.query_single(<<~SQL, CONFIG_SETTINGS)&.first + SELECT 1 FROM site_settings + WHERE name IN (?) + AND value != '' + LIMIT 1 + SQL + + if is_configured + # The plugin was installed before we changed it to be disabled-by-default + # Therefore, if there is no existing database value, enable the plugin + execute <<~SQL + INSERT INTO site_settings(name, data_type, value, created_at, updated_at) + VALUES('affiliate_enabled', 5, 't', NOW(), NOW()) + ON CONFLICT (name) DO NOTHING + SQL + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/spec/db/migrate/20250721192553_enable_affiliate_if_already_installed_spec.rb b/spec/db/migrate/20250721192553_enable_affiliate_if_already_installed_spec.rb new file mode 100644 index 0000000..de28b26 --- /dev/null +++ b/spec/db/migrate/20250721192553_enable_affiliate_if_already_installed_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative "../../../db/migrate/20250721192553_enable_affiliate_if_already_installed" + +RSpec.describe EnableAffiliateIfAlreadyInstalled do + let(:connection) { ActiveRecord::Base.connection } + + before do + @provider = SiteSetting.provider + SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting) + end + + after { SiteSetting.provider = @provider } + + it "enables the affiliate plugin if it was previously installed" do + expect(SiteSetting.affiliate_enabled).to eq(false) + SiteSetting.affiliate_amazon_ca = "abc" + + EnableAffiliateIfAlreadyInstalled.new.up + + SiteSetting.refresh! + expect(SiteSetting.affiliate_enabled).to eq(true) + end + + it "leaves disabled if not configured" do + expect(SiteSetting.affiliate_enabled).to eq(false) + + EnableAffiliateIfAlreadyInstalled.new.up + + SiteSetting.refresh! + expect(SiteSetting.affiliate_enabled).to eq(false) + end +end diff --git a/spec/lib/affiliate_processor_spec.rb b/spec/lib/affiliate_processor_spec.rb index 38eac35..fb074f2 100644 --- a/spec/lib/affiliate_processor_spec.rb +++ b/spec/lib/affiliate_processor_spec.rb @@ -5,6 +5,8 @@ def r(url) AffiliateProcessor.apply(url) end + before { enable_current_plugin } + it "can apply affiliate code to ldlc" do SiteSetting.affiliate_ldlc_com = "samsshop"