Skip to content

Commit

Permalink
Add Customers and Subscriptions models (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdenquin authored Mar 28, 2022
1 parent f78bc30 commit b872437
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 1 deletion.
9 changes: 9 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_litera: true

class Customer < ApplicationRecord
belongs_to :organization

has_many :subscriptions

validates :external_id, presence: true
end
2 changes: 2 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Organization < ApplicationRecord
has_many :users, through: :memberships
has_many :billable_metrics
has_many :plans
has_many :customers
has_many :subscriptions, through: :customers

before_create :generate_api_key

Expand Down
1 change: 1 addition & 0 deletions app/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Plan < ApplicationRecord

has_many :charges, dependent: :destroy
has_many :billable_metrics, through: :charges
has_many :subscriptions

FREQUENCIES = %i[
weekly
Expand Down
31 changes: 31 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class Subscription < ApplicationRecord
belongs_to :customer
belongs_to :plan
has_one :organization, through: :customer

STATUSES = [
:pending,
:active,
:terminated,
:canceled
].freeze

enum status: STATUSES

def mark_as_active!
self.started_at = Time.zone.now
self.active!
end

def mark_as_terminated!
self.terminated_at = Time.zone.now
self.terminated!
end

def mark_as_canceled!
self.canceled_at = Time.zone.now
self.canceled!
end
end
12 changes: 12 additions & 0 deletions db/migrate/20220328085925_create_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateCustomers < ActiveRecord::Migration[7.0]
def change
create_table :customers, id: :uuid do |t|
t.string :external_id, null: false, index: true
t.string :name

t.references :organization, type: :uuid, foreign_key: true, null: false, index: true

t.timestamps
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20220328090501_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateSubscriptions < ActiveRecord::Migration[7.0]
def change
create_table :subscriptions, id: :uuid do |t|
t.references :customer, type: :uuid, null: false, foreign_key: true, index: true
t.references :plan, type: :uuid, null: false, foreign_key: true, index: true

t.integer :status, null: false

t.timestamp :canceled_at
t.timestamp :terminated_at
t.timestamp :started_at

t.timestamps
end
end
end
28 changes: 27 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions spec/factories/customer_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :customer do
organization
name { Faker::TvShows::SiliconValley.character }
external_id { SecureRandom.uuid }
end
end
8 changes: 8 additions & 0 deletions spec/factories/subscription_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :subscription do
customer
plan
end
end
3 changes: 3 additions & 0 deletions spec/models/charge_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Charge, type: :model do
# Empty
end
7 changes: 7 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Customer, type: :model do
# Empty
end
7 changes: 7 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Subscription, type: :model do
# Empty
end

0 comments on commit b872437

Please sign in to comment.