4768aca484
* Add Conversation factory with dependent factories * Include FactoryBot methods in rspec config * Add unit tests for public methods of Conversation model * Move Current model into a separate file in lib folder * Disable Metrics/BlockLength rule for db/migrate and spec folders * Get rid of global $dispatcher variable * Create Message#unread_since scope * Refactor callback methods in Conversation model * Create Conversations::EventDataPresenter * Add translation keys for activity messages * Add pry-rails gem * Refactor Conversation#notify_status_change * Add mock_redis for test env
41 lines
842 B
Ruby
41 lines
842 B
Ruby
class Subscription < ApplicationRecord
|
|
include Events::Types
|
|
|
|
belongs_to :account
|
|
before_create :set_default_billing_params
|
|
after_create :notify_creation
|
|
|
|
enum state: [:trial, :active, :cancelled]
|
|
|
|
def payment_source_added!
|
|
self.payment_source_added = true
|
|
self.save
|
|
end
|
|
|
|
def trial_expired?
|
|
(trial? && expiry < Date.current) ||
|
|
(cancelled? && !payment_source_added)
|
|
end
|
|
|
|
def suspended?
|
|
cancelled? && payment_source_added
|
|
end
|
|
|
|
def summary
|
|
{
|
|
state: state,
|
|
expiry: expiry.to_i
|
|
}
|
|
end
|
|
|
|
private
|
|
|
|
def set_default_billing_params
|
|
self.expiry = Time.now + Plan.default_trial_period
|
|
self.pricing_version = Plan.default_pricing_version
|
|
end
|
|
|
|
def notify_creation
|
|
Rails.configuration.dispatcher.dispatch(SUBSCRIPTION_CREATED, Time.zone.now, subscription: self)
|
|
end
|
|
end
|