ruby
40 lines · 8 steps
Modeling subscription lifecycle in Rails
A Subscription model uses an enum, composable scopes, and guarded bang methods to manage billing state transitions.
Explained by
highlit
1class Subscription < ApplicationRecord
2 belongs_to :account
3
4 enum :status, {
5 trialing: 0,
6 active: 1,
7 past_due: 2,
8 canceled: 3,
9 expired: 4
10 }, default: :trialing, validate: true
11
12 scope :billable, -> { where(status: %i[active past_due]) }
13 scope :renewable, -> { active.where(cancel_at_period_end: false) }
14 scope :ending_soon, ->(window = 3.days) { active.where(current_period_end: ..window.from_now) }
15
16 def can_reactivate?
17 canceled? || expired?
18 end
19
20 def mark_past_due!
21 return if past_due?
22
23 update!(status: :past_due, delinquent_since: Time.current)
24 BillingMailer.with(subscription: self).payment_failed.deliver_later
25 end
26
27 def cancel!(immediately: false)
28 if immediately
29 update!(status: :canceled, canceled_at: Time.current, current_period_end: Time.current)
30 else
31 update!(cancel_at_period_end: true)
32 end
33 end
34
35 def reactivate!
36 raise ActiveRecord::RecordInvalid, self unless can_reactivate?
37
38 update!(status: :active, canceled_at: nil, cancel_at_period_end: false)
39 end
40end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A validated enum turns a status column into named predicates and value scopes for free.
- 2Scopes chain onto enum-generated scopes, letting you express business rules declaratively.
- 3Guarded bang methods keep invalid state transitions from ever hitting the database.
Related explainers
ruby
class Product < ApplicationRecord scope :search, ->(query) { return all if query.blank?
Building a multi-term search scope in Rails
arel
scopes
full-text search
Advanced
7 steps
rust
use std::time::Duration; #[derive(Debug, PartialEq)] pub enum ParseDurationError {
Parsing duration strings safely in Rust
parsing
error-handling
checked-arithmetic
Intermediate
8 steps
ruby
module Rack class MaintenanceMode RETRY_AFTER = 3600
A Rack maintenance-mode middleware in Rails
middleware
rack
http-status
Intermediate
8 steps
php
<?php namespace App\Services;
How user impersonation works in Laravel
authentication
authorization
session
Intermediate
8 steps
ruby
class ImageNormalizer ORIENTATION_TRANSFORMS = { 1 => ->(img) {}, 2 => ->(img) { img.flop },
Correcting EXIF orientation in Ruby
lookup-table
lambdas
image-processing
Intermediate
7 steps
ruby
module RequestTagging class Middleware def initialize(app) @app = app
Per-request context with CurrentAttributes in Rails
middleware
thread-safety
logging
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/modeling-subscription-lifecycle-in-rails-explained-ruby-49de/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.