Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +01:00
Complete updates to handle subscription product slugs
Dieser Commit ist enthalten in:
Ursprung
643c5ecff0
Commit
9eb5fc6ff6
4 geänderte Dateien mit 137 neuen und 84 gelöschten Zeilen
|
@ -1,8 +1,10 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
class CustomWizard::Subscription
|
class CustomWizard::Subscription
|
||||||
STANDARD_PRODUCT_ID = 'prod_MH11woVoZU5AWb'
|
PRODUCT_HIERARCHY = %w[
|
||||||
BUSINESS_PRODUCT_ID = 'prod_MH0wT627okh3Ef'
|
community
|
||||||
COMMUNITY_PRODUCT_ID = 'prod_MU7l9EjxhaukZ7'
|
standard
|
||||||
|
business
|
||||||
|
]
|
||||||
|
|
||||||
def self.attributes
|
def self.attributes
|
||||||
{
|
{
|
||||||
|
@ -99,8 +101,30 @@ class CustomWizard::Subscription
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_accessor :product_id,
|
||||||
|
:product_slug
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@subscription = find_subscription
|
if CustomWizard::Subscription.client_installed?
|
||||||
|
result = DiscourseSubscriptionClient.find_subscriptions("discourse-custom-wizard")
|
||||||
|
|
||||||
|
if result&.any?
|
||||||
|
slugs = result.supplier.product_slugs
|
||||||
|
|
||||||
|
if slugs.present?
|
||||||
|
ids_and_slugs = result.subscriptions.map do |subscription|
|
||||||
|
{ id: subscription.product_id, slug: slugs[subscription.product_id] }
|
||||||
|
end
|
||||||
|
|
||||||
|
id_and_slug = ids_and_slugs.sort do |a, b|
|
||||||
|
PRODUCT_HIERARCHY[a[:slug]] - PRODUCT_HIERARCHY[b[:slug]]
|
||||||
|
end.first
|
||||||
|
|
||||||
|
@product_id = id_and_slug[:id]
|
||||||
|
@product_slug = id_and_slug[:slug]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def includes?(feature, attribute, value = nil)
|
def includes?(feature, attribute, value = nil)
|
||||||
|
@ -140,36 +164,19 @@ class CustomWizard::Subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def standard?
|
def standard?
|
||||||
@subscription.product_id === STANDARD_PRODUCT_ID
|
product_slug === "standard"
|
||||||
end
|
end
|
||||||
|
|
||||||
def business?
|
def business?
|
||||||
@subscription.product_id === BUSINESS_PRODUCT_ID
|
product_slug === "business"
|
||||||
end
|
end
|
||||||
|
|
||||||
def community?
|
def community?
|
||||||
@subscription.product_id === COMMUNITY_PRODUCT_ID
|
product_slug === "community"
|
||||||
end
|
end
|
||||||
|
|
||||||
def client_installed?
|
def self.client_installed?
|
||||||
defined?(SubscriptionClient) == 'constant' && SubscriptionClient.class == Module
|
defined?(DiscourseSubscriptionClient) == 'constant' && DiscourseSubscriptionClient.class == Module
|
||||||
end
|
|
||||||
|
|
||||||
def find_subscription
|
|
||||||
subscription = nil
|
|
||||||
|
|
||||||
if client_installed?
|
|
||||||
subscription = SubscriptionClientSubscription.active
|
|
||||||
.where(product_id: [STANDARD_PRODUCT_ID, BUSINESS_PRODUCT_ID, COMMUNITY_PRODUCT_ID])
|
|
||||||
.order("product_id = '#{BUSINESS_PRODUCT_ID}' DESC")
|
|
||||||
.first
|
|
||||||
end
|
|
||||||
|
|
||||||
unless subscription
|
|
||||||
subscription = OpenStruct.new(product_id: nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
subscription
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.subscribed?
|
def self.subscribed?
|
||||||
|
@ -192,10 +199,6 @@ class CustomWizard::Subscription
|
||||||
new.type
|
new.type
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.client_installed?
|
|
||||||
new.client_installed?
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.includes?(feature, attribute, value)
|
def self.includes?(feature, attribute, value)
|
||||||
new.includes?(feature, attribute, value)
|
new.includes?(feature, attribute, value)
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,9 +2,19 @@
|
||||||
|
|
||||||
describe CustomWizard::Subscription do
|
describe CustomWizard::Subscription do
|
||||||
let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") }
|
let(:guests_permitted) { get_wizard_fixture("wizard/guests_permitted") }
|
||||||
|
let!(:business_product_id) { SecureRandom.hex(8) }
|
||||||
|
let!(:standard_product_id) { SecureRandom.hex(8) }
|
||||||
|
let!(:community_product_id) { SecureRandom.hex(8) }
|
||||||
|
let!(:product_slugs) {
|
||||||
|
{
|
||||||
|
"#{business_product_id}" => "business",
|
||||||
|
"#{standard_product_id}" => "standard",
|
||||||
|
"#{community_product_id}" => "community"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def undefine_client_classes
|
def undefine_client_classes
|
||||||
Object.send(:remove_const, :SubscriptionClient) if Object.constants.include?(:SubscriptionClient)
|
Object.send(:remove_const, :DiscourseSubscriptionClient) if Object.constants.include?(:DiscourseSubscriptionClient)
|
||||||
Object.send(:remove_const, :SubscriptionClientSubscription) if Object.constants.include?(:SubscriptionClientSubscription)
|
Object.send(:remove_const, :SubscriptionClientSubscription) if Object.constants.include?(:SubscriptionClientSubscription)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,14 +22,6 @@ describe CustomWizard::Subscription do
|
||||||
load File.expand_path("#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/subscription_client.rb", __FILE__)
|
load File.expand_path("#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/subscription_client.rb", __FILE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
def stub_client_methods
|
|
||||||
[:active, :where, :order, :first].each do |method|
|
|
||||||
SubscriptionClientSubscription.stubs(method)
|
|
||||||
.returns(SubscriptionClientSubscription)
|
|
||||||
end
|
|
||||||
SubscriptionClientSubscription.stubs(:product_id).returns(SecureRandom.hex(8))
|
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
after do
|
||||||
undefine_client_classes
|
undefine_client_classes
|
||||||
end
|
end
|
||||||
|
@ -50,7 +52,6 @@ describe CustomWizard::Subscription do
|
||||||
context "with subscription client" do
|
context "with subscription client" do
|
||||||
before do
|
before do
|
||||||
define_client_classes
|
define_client_classes
|
||||||
stub_client_methods
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "detects the subscription client" do
|
it "detects the subscription client" do
|
||||||
|
@ -58,6 +59,10 @@ describe CustomWizard::Subscription do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "without a subscription" do
|
context "without a subscription" do
|
||||||
|
before do
|
||||||
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(nil)
|
||||||
|
end
|
||||||
|
|
||||||
it "has none type" do
|
it "has none type" do
|
||||||
expect(described_class.type).to eq(:none)
|
expect(described_class.type).to eq(:none)
|
||||||
end
|
end
|
||||||
|
@ -71,19 +76,29 @@ describe CustomWizard::Subscription do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a subscription" do
|
context "with subscriptions" do
|
||||||
|
def get_subscription_result(product_id)
|
||||||
|
result = DiscourseSubscriptionClient::Subscriptions::Result.new
|
||||||
|
result.supplier = SubscriptionClientSupplier.new(product_slugs)
|
||||||
|
result.resource = SubscriptionClientResource.new
|
||||||
|
result.subscriptions = [SubscriptionClientSubscription.new(product_id)]
|
||||||
|
result
|
||||||
|
end
|
||||||
|
let!(:business_subscription_result) { get_subscription_result(business_product_id) }
|
||||||
|
let!(:standard_subscription_result) { get_subscription_result(standard_product_id) }
|
||||||
|
let!(:community_subscription_result) { get_subscription_result(community_product_id) }
|
||||||
|
|
||||||
it "handles mapped values" do
|
it "handles mapped values" do
|
||||||
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::STANDARD_PRODUCT_ID)
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(standard_subscription_result)
|
||||||
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(true)
|
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(true)
|
||||||
|
|
||||||
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::COMMUNITY_PRODUCT_ID)
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(community_subscription_result)
|
||||||
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(false)
|
expect(described_class.includes?(:wizard, :permitted, guests_permitted["permitted"])).to eq(false)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
context "with standard subscription" do
|
context "with a standard subscription" do
|
||||||
before do
|
before do
|
||||||
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::STANDARD_PRODUCT_ID)
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(standard_subscription_result)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "detects standard type" do
|
it "detects standard type" do
|
||||||
|
@ -99,9 +114,9 @@ describe CustomWizard::Subscription do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with business subscription" do
|
context "with a business subscription" do
|
||||||
before do
|
before do
|
||||||
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::BUSINESS_PRODUCT_ID)
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(business_subscription_result)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "detects business type" do
|
it "detects business type" do
|
||||||
|
@ -113,9 +128,9 @@ describe CustomWizard::Subscription do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with community subscription" do
|
context "with a community subscription" do
|
||||||
before do
|
before do
|
||||||
SubscriptionClientSubscription.stubs(:product_id).returns(CustomWizard::Subscription::COMMUNITY_PRODUCT_ID)
|
DiscourseSubscriptionClient.stubs(:find_subscriptions).returns(community_subscription_result)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "detects community type" do
|
it "detects community type" do
|
||||||
|
@ -128,3 +143,4 @@ describe CustomWizard::Subscription do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
39
spec/fixtures/subscription_client.rb
gevendort
39
spec/fixtures/subscription_client.rb
gevendort
|
@ -1,4 +1,39 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module SubscriptionClient; end
|
module DiscourseSubscriptionClient
|
||||||
class SubscriptionClientSubscription; end
|
def self.find_subscriptions(resource_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SubscriptionClientSupplier
|
||||||
|
attr_reader :product_slugs
|
||||||
|
|
||||||
|
def initialize(product_slugs)
|
||||||
|
@product_slugs = product_slugs
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SubscriptionClientResource
|
||||||
|
end
|
||||||
|
|
||||||
|
class SubscriptionClientSubscription
|
||||||
|
attr_reader :product_id
|
||||||
|
|
||||||
|
def initialize(product_id)
|
||||||
|
@product_id = product_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module DiscourseSubscriptionClient
|
||||||
|
class Subscriptions
|
||||||
|
class Result
|
||||||
|
attr_accessor :supplier,
|
||||||
|
:resource,
|
||||||
|
:subscriptions
|
||||||
|
|
||||||
|
def any?
|
||||||
|
supplier.present? && resource.present? && subscriptions.present?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -9,7 +9,6 @@ def get_wizard_fixture(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def enable_subscription(type)
|
def enable_subscription(type)
|
||||||
CustomWizard::Subscription.stubs(:client_installed?).returns(true)
|
|
||||||
CustomWizard::Subscription.stubs("#{type}?".to_sym).returns(true)
|
CustomWizard::Subscription.stubs("#{type}?".to_sym).returns(true)
|
||||||
CustomWizard::Subscription.any_instance.stubs("#{type}?".to_sym).returns(true)
|
CustomWizard::Subscription.any_instance.stubs("#{type}?".to_sym).returns(true)
|
||||||
end
|
end
|
||||||
|
|
Laden …
In neuem Issue referenzieren