* Add ProMembership model * Implement ProMembershipsController#create * Implement basic ProMembershipsController#show * Add ProMembership to ledger * Populate user history after pro subscription is created * Add fields for expiration notifications * Add ProMemberships::ExpirationNotifier to notify users of expiring memberships * Add tasks for recurring jobs to notify users of expiration * Add auto_recharge column to ProMembership * Add ProMemberships::Biller (incomplete) * Fix specs * Add ProMembership to Administrate * Fix spec * Add has_enough_credits? to User and Organization * Add Payments::Customer class * Finish ProMembership::Biller functionality * Fix ProMemberships::Creator check for credits * Disable destroy actions for ProMembershipsController * Correctly authenticate ProMembershipsController actions * Make sure only pro user's history can be indexed * Add ProMembershipsController#update action for auto recharge * Use regular AR to save new credits and add touch to the purchaser * Clarify Pro membership create policy * Display information about an existing pro membership * Add UI to show page * Add system test for Pro membership creation * Implement edit membership * Make sure users with pro memberships can access history and dashboard pro * Fix padding issue * Show a different text for a user that has credits but not enough for Pro * Move Pro Membership functionality inside settings * Update Pro Membership link in email notifications * Bust all relevant caches * Add the Pro checkmark around the website * Use Users::ResaveArticlesJob instead of delay * Add/remove user from pro-members chat channel * Use the appropriate Pro checkmark * Remove unfinished pro elements * Remove checkmark JS
56 lines
1.4 KiB
Ruby
56 lines
1.4 KiB
Ruby
class ProMembership < ApplicationRecord
|
|
STATUSES = %w[active expired].freeze
|
|
MONTHLY_COST = 5
|
|
MONTHLY_COST_USD = 25
|
|
|
|
belongs_to :user
|
|
|
|
validates :user, :status, :expiration_notifications_count, presence: true
|
|
validates :user, uniqueness: true
|
|
validates :expires_at, presence: true, on: :save
|
|
validates :status, inclusion: { in: STATUSES }
|
|
|
|
scope :active, -> { where(status: :active) }
|
|
scope :expired, -> { where(status: :expired) }
|
|
|
|
before_create :set_expiration_date
|
|
after_save :resave_user_articles
|
|
|
|
def expired?
|
|
expires_at <= Time.current
|
|
end
|
|
|
|
def active?
|
|
expires_at > Time.current
|
|
end
|
|
|
|
def expire!
|
|
update!(expires_at: Time.current, status: :expired)
|
|
end
|
|
|
|
def renew!
|
|
update!(
|
|
expires_at: 1.month.from_now,
|
|
status: :active,
|
|
expiration_notification_at: nil,
|
|
expiration_notifications_count: 0,
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def set_expiration_date
|
|
self.expires_at = 1.month.from_now
|
|
end
|
|
|
|
# if the membership is new or the user flips from expired to active and viceversa,
|
|
# we need to resave all of the user's articles to make sure that the pro details
|
|
# that are cached with them are refreshed
|
|
def resave_user_articles
|
|
if saved_change_to_created_at? ||
|
|
saved_change_to_expires_at? ||
|
|
saved_change_to_status?
|
|
Users::ResaveArticlesJob.perform_later(user.id)
|
|
end
|
|
end
|
|
end
|