Skip to content

Commit

Permalink
Fix subscription active scope with ended trial. Closes pay-rails#940 C…
Browse files Browse the repository at this point in the history
…loses pay-rails#939
  • Loading branch information
excid3 committed Feb 27, 2024
1 parent 4d2791d commit 1673cfe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/pay/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Subscription < Pay::ApplicationRecord

# Scopes
scope :for_name, ->(name) { where(name: name) }
scope :on_trial, -> { where("trial_ends_at > ?", Time.current) }
scope :on_trial, -> { where(status: ["trialing", "active"]).where("trial_ends_at > ?", Time.current) }
scope :canceled, -> { where.not(ends_at: nil) }
scope :cancelled, -> { canceled }
scope :on_grace_period, -> { where("#{table_name}.ends_at IS NOT NULL AND #{table_name}.ends_at > ?", Time.current) }
scope :active, -> { where(status: ["trialing", "active"]).pause_not_started.where("#{table_name}.ends_at IS NULL OR #{table_name}.ends_at > ?", Time.current).where("trial_ends_at IS NULL OR trial_ends_at > ?", Time.current) }
scope :active, -> { where(status: "active").pause_not_started.where("#{table_name}.ends_at IS NULL OR #{table_name}.ends_at > ?", Time.current).or(on_trial) }
scope :paused, -> { where(status: "paused").or(where("pause_starts_at <= ?", Time.current)) }
scope :pause_not_started, -> { where("pause_starts_at IS NULL OR pause_starts_at > ?", Time.current) }
scope :active_or_paused, -> { active.or(paused) }
Expand Down
6 changes: 6 additions & 0 deletions test/models/pay/subscription_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ class Pay::Subscription::Test < ActiveSupport::TestCase
assert_includes subscriptions, trialing_subscription
end

test "active scope should include previously trialed subscriptions" do
trialing_subscription = create_subscription(trial_ends_at: 7.days.ago)
subscriptions = Pay::Subscription.active
assert_includes subscriptions, trialing_subscription
end

test "active scope should not include paused subscriptions" do
paused_subscription = create_subscription(status: "paused")
subscriptions = Pay::Subscription.active
Expand Down

0 comments on commit 1673cfe

Please sign in to comment.