add customer model spec and scope

This commit is contained in:
Rimian Perkins 2019-10-26 13:01:49 +11:00
parent dcb4b82dec
commit b7a3be9344
4 changed files with 39 additions and 4 deletions

View File

@ -13,9 +13,9 @@ module DiscoursePatrons
source: params[:source]
)
DiscoursePatrons::Customer.create(
customer_id: customer.id
user_id: current_user.id
DiscoursePatrons::Customer.create_customer(
current_user,
customer
)
render_json_dump customer

View File

@ -2,6 +2,14 @@
module DiscoursePatrons
class Customer < ActiveRecord::Base
self.table_name = "discourse_patrons_customers"
scope :find_user, ->(user) { find_by_user_id(user.id) }
class << self
table_name = "discourse_patrons_customers"
def create_customer(user, customer)
create(customer_id: customer[:id], user_id: user.id)
end
end
end
end

View File

@ -6,6 +6,7 @@ class CreateCustomers < ActiveRecord::Migration[5.2]
t.timestamps
end
add_index :discourse_patrons_customers, :user_id, unique: true
add_index :discourse_patrons_customers, :customer_id, unique: true
end
end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
require 'rails_helper'
module DiscoursePatrons
RSpec.describe Customer do
let(:user) { Fabricate(:user) }
let(:stripe_customer) { { id: 'cus_id4567' } }
it "has a table name" do
expect(described_class.table_name).to eq "discourse_patrons_customers"
end
it "creates" do
customer = described_class.create_customer(user, stripe_customer)
expect(customer.customer_id).to eq 'cus_id4567'
expect(customer.user_id).to eq user.id
end
it "has a user scope" do
described_class.create_customer(user, stripe_customer)
customer = described_class.find_user(user)
expect(customer.customer_id).to eq 'cus_id4567'
end
end
end