product controller

This commit is contained in:
Rimian Perkins 2019-10-25 08:18:16 +11:00
parent 5f71def8a4
commit 6fbcea2cf3
8 changed files with 55 additions and 3 deletions

View File

@ -10,6 +10,7 @@ module DiscoursePatrons
begin begin
plans = ::Stripe::Plan.list(active: true) plans = ::Stripe::Plan.list(active: true)
# TODO: Serialize. Remove some attributes like meta.group_name
render_json_dump plans.data render_json_dump plans.data
rescue ::Stripe::InvalidRequestError => e rescue ::Stripe::InvalidRequestError => e

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
module DiscoursePatrons
class ProductsController < ::ApplicationController
include DiscoursePatrons::Stripe
before_action :set_api_key
def index
begin
products = ::Stripe::Product.list(active: true)
# TODO: Serialize. Remove some attributes like metadata
render_json_dump products.data
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
end
end

View File

@ -0,0 +1,13 @@
import { ajax } from "discourse/lib/ajax";
const Product = Discourse.Model.extend({});
Product.reopenClass({
findAll() {
return ajax("/patrons/products", { method: "get" }).then(result =>
result.map(product => Product.create(product))
);
}
});
export default Product;

View File

@ -1,6 +1,8 @@
export default function() { export default function() {
this.route("patrons", function() { this.route("patrons", function() {
this.route("subscribe"); this.route("subscribe", function() {
this.route("product", { path: ":product_id" })
});
this.route("show", { path: ":pid" }); this.route("show", { path: ":pid" });
}); });
} }

View File

@ -10,10 +10,9 @@ export default Discourse.Route.extend({
return `$${toCurrency(plan.amount)} ${plan.currency.toUpperCase()} / ${plan.interval}`; return `$${toCurrency(plan.amount)} ${plan.currency.toUpperCase()} / ${plan.interval}`;
}; };
const group = Group.find();
const plans = Plan.findAll().then(results => results.map(p => planSelectText(p))); const plans = Plan.findAll().then(results => results.map(p => planSelectText(p)));
const subscription = Subscription.create(); const subscription = Subscription.create();
return Ember.RSVP.hash({ group, plans, subscription }); return Ember.RSVP.hash({ plans, subscription });
} }
}); });

View File

@ -15,6 +15,7 @@ DiscoursePatrons::Engine.routes.draw do
resources :customers, only: [:create] resources :customers, only: [:create]
resources :subscriptions, only: [:create] resources :subscriptions, only: [:create]
resources :plans, only: [:index] resources :plans, only: [:index]
resources :products, only: [:index]
resources :patrons, only: [:index, :create] resources :patrons, only: [:index, :create]
get '/' => 'patrons#index' get '/' => 'patrons#index'

View File

@ -50,6 +50,7 @@ after_initialize do
"../app/controllers/customers_controller", "../app/controllers/customers_controller",
"../app/controllers/patrons_controller", "../app/controllers/patrons_controller",
"../app/controllers/plans_controller", "../app/controllers/plans_controller",
"../app/controllers/products_controller",
"../app/controllers/subscriptions_controller", "../app/controllers/subscriptions_controller",
"../app/models/payment", "../app/models/payment",
"../app/serializers/payment_serializer", "../app/serializers/payment_serializer",

View File

@ -0,0 +1,14 @@
# frozen_string_literal: true
require 'rails_helper'
module DiscoursePatrons
RSpec.describe ProductsController do
describe "index" do
it "lists the active products" do
::Stripe::Product.expects(:list).with(active: true)
get "/patrons/products.json"
end
end
end
end