mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-03-06 09:39:51 +00:00
REFACTOR: Use engine to load files, add constraint (#14)
This commit is contained in:
parent
8c670328b7
commit
fb4f50478d
@ -1,4 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
require_dependency "subscriptions_user_constraint"
|
||||
|
||||
DiscourseSubscriptions::Engine.routes.draw do
|
||||
# TODO: namespace this
|
||||
@ -21,10 +22,10 @@ DiscourseSubscriptions::Engine.routes.draw do
|
||||
resources :hooks, only: [:create]
|
||||
resources :invoices, only: [:index]
|
||||
resources :payments, only: [:create]
|
||||
resources :plans, only: [:index]
|
||||
resources :plans, only: [:index], constraints: SubscriptionsUserConstraint.new
|
||||
resources :products, only: [:index, :show]
|
||||
resources :subscriptions, only: [:create]
|
||||
|
||||
get '/' => 'subscriptions#index'
|
||||
get '/:id' => 'subscriptions#index'
|
||||
get '/' => 'subscriptions#index', constraints: SubscriptionsUserConstraint.new
|
||||
get '/:id' => 'subscriptions#index', constraints: SubscriptionsUserConstraint.new
|
||||
end
|
||||
|
10
lib/subscriptions_user_constraint.rb
Normal file
10
lib/subscriptions_user_constraint.rb
Normal file
@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SubscriptionsUserConstraint
|
||||
def matches?(request)
|
||||
provider = Discourse.current_user_provider.new(request.env)
|
||||
provider.current_user
|
||||
rescue Discourse::InvalidAccess, Discourse::ReadOnly
|
||||
false
|
||||
end
|
||||
end
|
28
plugin.rb
28
plugin.rb
@ -39,6 +39,10 @@ Discourse::Application.routes.append do
|
||||
get 'u/:username/billing/:id' => 'users#show', constraints: { username: USERNAME_ROUTE_FORMAT }
|
||||
end
|
||||
|
||||
load File.expand_path('lib/discourse_subscriptions/engine.rb', __dir__)
|
||||
load File.expand_path('app/controllers/concerns/stripe.rb', __dir__)
|
||||
load File.expand_path('app/controllers/concerns/group.rb', __dir__)
|
||||
|
||||
after_initialize do
|
||||
::Stripe.api_version = "2019-12-03"
|
||||
|
||||
@ -48,30 +52,6 @@ after_initialize do
|
||||
url: 'https://github.com/discourse/discourse-subscriptions'
|
||||
)
|
||||
|
||||
[
|
||||
"../lib/discourse_subscriptions/engine",
|
||||
"../config/routes",
|
||||
"../app/controllers/concerns/group",
|
||||
"../app/controllers/concerns/stripe",
|
||||
"../app/controllers/admin_controller",
|
||||
"../app/controllers/admin/plans_controller",
|
||||
"../app/controllers/admin/products_controller",
|
||||
"../app/controllers/admin/subscriptions_controller",
|
||||
"../app/controllers/user/payments_controller",
|
||||
"../app/controllers/user/subscriptions_controller",
|
||||
"../app/controllers/customers_controller",
|
||||
"../app/controllers/hooks_controller",
|
||||
"../app/controllers/invoices_controller",
|
||||
"../app/controllers/plans_controller",
|
||||
"../app/controllers/payments_controller",
|
||||
"../app/controllers/products_controller",
|
||||
"../app/controllers/subscriptions_controller",
|
||||
"../app/models/customer",
|
||||
"../app/models/product",
|
||||
"../app/models/subscription",
|
||||
"../app/serializers/payment_serializer",
|
||||
].each { |path| require File.expand_path(path, __FILE__) }
|
||||
|
||||
Discourse::Application.routes.append do
|
||||
mount ::DiscourseSubscriptions::Engine, at: 's'
|
||||
end
|
||||
|
@ -4,6 +4,12 @@ require 'rails_helper'
|
||||
|
||||
module DiscourseSubscriptions
|
||||
RSpec.describe PlansController do
|
||||
let(:user) { Fabricate(:user) }
|
||||
|
||||
before do
|
||||
sign_in(user)
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
it "lists the active plans" do
|
||||
::Stripe::Plan.expects(:list).with(active: true)
|
||||
|
Loading…
x
Reference in New Issue
Block a user