Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discourse_affiliate:
affiliate_enabled:
default: true
default: false
client: true
affiliate_amazon_ca:
default: ""
Expand Down
45 changes: 45 additions & 0 deletions db/migrate/20250721192553_enable_affiliate_if_already_installed.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/lib/affiliate_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down