From fb4f50478d2ff97a040c2ec14257a5a047aa6234 Mon Sep 17 00:00:00 2001 From: Justin DiRose Date: Mon, 6 Jul 2020 11:11:04 -0500 Subject: [PATCH] REFACTOR: Use engine to load files, add constraint (#14) --- .../admin/plans_controller.rb | 0 .../admin/products_controller.rb | 0 .../admin/subscriptions_controller.rb | 0 .../admin_controller.rb | 0 .../customers_controller.rb | 0 .../hooks_controller.rb | 0 .../invoices_controller.rb | 0 .../payments_controller.rb | 0 .../plans_controller.rb | 0 .../products_controller.rb | 0 .../subscriptions_controller.rb | 0 .../user/payments_controller.rb | 0 .../user/subscriptions_controller.rb | 0 .../{ => discourse_subscriptions}/customer.rb | 0 .../{ => discourse_subscriptions}/product.rb | 0 .../subscription.rb | 0 .../payment_serializer.rb | 0 config/routes.rb | 7 +++-- lib/subscriptions_user_constraint.rb | 10 +++++++ plugin.rb | 28 +++---------------- spec/requests/plans_controller_spec.rb | 6 ++++ 21 files changed, 24 insertions(+), 27 deletions(-) rename app/controllers/{ => discourse_subscriptions}/admin/plans_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/admin/products_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/admin/subscriptions_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/admin_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/customers_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/hooks_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/invoices_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/payments_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/plans_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/products_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/subscriptions_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/user/payments_controller.rb (100%) rename app/controllers/{ => discourse_subscriptions}/user/subscriptions_controller.rb (100%) rename app/models/{ => discourse_subscriptions}/customer.rb (100%) rename app/models/{ => discourse_subscriptions}/product.rb (100%) rename app/models/{ => discourse_subscriptions}/subscription.rb (100%) rename app/serializers/{ => discourse_subscriptions}/payment_serializer.rb (100%) create mode 100644 lib/subscriptions_user_constraint.rb diff --git a/app/controllers/admin/plans_controller.rb b/app/controllers/discourse_subscriptions/admin/plans_controller.rb similarity index 100% rename from app/controllers/admin/plans_controller.rb rename to app/controllers/discourse_subscriptions/admin/plans_controller.rb diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/discourse_subscriptions/admin/products_controller.rb similarity index 100% rename from app/controllers/admin/products_controller.rb rename to app/controllers/discourse_subscriptions/admin/products_controller.rb diff --git a/app/controllers/admin/subscriptions_controller.rb b/app/controllers/discourse_subscriptions/admin/subscriptions_controller.rb similarity index 100% rename from app/controllers/admin/subscriptions_controller.rb rename to app/controllers/discourse_subscriptions/admin/subscriptions_controller.rb diff --git a/app/controllers/admin_controller.rb b/app/controllers/discourse_subscriptions/admin_controller.rb similarity index 100% rename from app/controllers/admin_controller.rb rename to app/controllers/discourse_subscriptions/admin_controller.rb diff --git a/app/controllers/customers_controller.rb b/app/controllers/discourse_subscriptions/customers_controller.rb similarity index 100% rename from app/controllers/customers_controller.rb rename to app/controllers/discourse_subscriptions/customers_controller.rb diff --git a/app/controllers/hooks_controller.rb b/app/controllers/discourse_subscriptions/hooks_controller.rb similarity index 100% rename from app/controllers/hooks_controller.rb rename to app/controllers/discourse_subscriptions/hooks_controller.rb diff --git a/app/controllers/invoices_controller.rb b/app/controllers/discourse_subscriptions/invoices_controller.rb similarity index 100% rename from app/controllers/invoices_controller.rb rename to app/controllers/discourse_subscriptions/invoices_controller.rb diff --git a/app/controllers/payments_controller.rb b/app/controllers/discourse_subscriptions/payments_controller.rb similarity index 100% rename from app/controllers/payments_controller.rb rename to app/controllers/discourse_subscriptions/payments_controller.rb diff --git a/app/controllers/plans_controller.rb b/app/controllers/discourse_subscriptions/plans_controller.rb similarity index 100% rename from app/controllers/plans_controller.rb rename to app/controllers/discourse_subscriptions/plans_controller.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/discourse_subscriptions/products_controller.rb similarity index 100% rename from app/controllers/products_controller.rb rename to app/controllers/discourse_subscriptions/products_controller.rb diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/discourse_subscriptions/subscriptions_controller.rb similarity index 100% rename from app/controllers/subscriptions_controller.rb rename to app/controllers/discourse_subscriptions/subscriptions_controller.rb diff --git a/app/controllers/user/payments_controller.rb b/app/controllers/discourse_subscriptions/user/payments_controller.rb similarity index 100% rename from app/controllers/user/payments_controller.rb rename to app/controllers/discourse_subscriptions/user/payments_controller.rb diff --git a/app/controllers/user/subscriptions_controller.rb b/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb similarity index 100% rename from app/controllers/user/subscriptions_controller.rb rename to app/controllers/discourse_subscriptions/user/subscriptions_controller.rb diff --git a/app/models/customer.rb b/app/models/discourse_subscriptions/customer.rb similarity index 100% rename from app/models/customer.rb rename to app/models/discourse_subscriptions/customer.rb diff --git a/app/models/product.rb b/app/models/discourse_subscriptions/product.rb similarity index 100% rename from app/models/product.rb rename to app/models/discourse_subscriptions/product.rb diff --git a/app/models/subscription.rb b/app/models/discourse_subscriptions/subscription.rb similarity index 100% rename from app/models/subscription.rb rename to app/models/discourse_subscriptions/subscription.rb diff --git a/app/serializers/payment_serializer.rb b/app/serializers/discourse_subscriptions/payment_serializer.rb similarity index 100% rename from app/serializers/payment_serializer.rb rename to app/serializers/discourse_subscriptions/payment_serializer.rb diff --git a/config/routes.rb b/config/routes.rb index 06d8469..e453ea5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/lib/subscriptions_user_constraint.rb b/lib/subscriptions_user_constraint.rb new file mode 100644 index 0000000..f0e1598 --- /dev/null +++ b/lib/subscriptions_user_constraint.rb @@ -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 \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index 4d5963d..ccd8ec9 100644 --- a/plugin.rb +++ b/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 diff --git a/spec/requests/plans_controller_spec.rb b/spec/requests/plans_controller_spec.rb index 9e69aea..2a0c36d 100644 --- a/spec/requests/plans_controller_spec.rb +++ b/spec/requests/plans_controller_spec.rb @@ -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)