-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Customers and Subscriptions models (#33)
- Loading branch information
Showing
12 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |