REFACTOR: Use engine to load files, add constraint (#14)

This commit is contained in:
Justin DiRose 2020-07-06 11:11:04 -05:00 committed by GitHub
parent 8c670328b7
commit fb4f50478d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 24 additions and 27 deletions

View File

@ -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

View 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

View File

@ -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

View File

@ -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)