Class: Subscription
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Subscription
- Defined in:
- app/models/subscription.rb
Instance Method Summary collapse
- #active? ⇒ Boolean
- #cancel ⇒ Object
- #cancel_now! ⇒ Object
- #cancelled? ⇒ Boolean
- #on_grace_period? ⇒ Boolean
- #on_trial? ⇒ Boolean
- #processor_subscription ⇒ Object
- #resume ⇒ Object
Instance Method Details
#active? ⇒ Boolean
27 28 29 |
# File 'app/models/subscription.rb', line 27 def active? ends_at.nil? || on_grace_period? || on_trial? end |
#cancel ⇒ Object
31 32 33 34 |
# File 'app/models/subscription.rb', line 31 def cancel subscription = processor_subscription.delete(at_period_end: true) update(ends_at: Time.at(subscription.current_period_end)) end |
#cancel_now! ⇒ Object
36 37 38 39 |
# File 'app/models/subscription.rb', line 36 def cancel_now! subscription = processor_subscription.delete update(ends_at: Time.at(subscription.current_period_end)) end |
#cancelled? ⇒ Boolean
19 20 21 |
# File 'app/models/subscription.rb', line 19 def cancelled? ends_at? end |
#on_grace_period? ⇒ Boolean
23 24 25 |
# File 'app/models/subscription.rb', line 23 def on_grace_period? cancelled? && Time.zone.now < ends_at end |
#on_trial? ⇒ Boolean
15 16 17 |
# File 'app/models/subscription.rb', line 15 def on_trial? trial_ends_at? && Time.zone.now < trial_ends_at end |
#processor_subscription ⇒ Object
56 57 58 |
# File 'app/models/subscription.rb', line 56 def processor_subscription owner.processor_subscription(processor_id) end |
#resume ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/subscription.rb', line 41 def resume unless on_grace_period? raise StandardError, 'You can only resume subscriptions within their grace period.' end subscription = processor_subscription subscription.plan = processor_plan subscription.trial_end = on_trial? ? trial_ends_at.to_i : 'now' subscription.save update(ends_at: nil) self end |