๐Ÿ“ฆ obafemitayor / newsletter-subscription-application

๐Ÿ“„ subscription.rb ยท 27 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27class Subscription < ApplicationRecord
  # Soft delete
  scope :active, -> { where(deleted_at: nil) }

  # Relationships
  belongs_to :customer
  belongs_to :category

  # Validations
  validates :customer_id, presence: true
  validates :category_id, presence: true
  validates :customer_id, uniqueness: { scope: :category_id, message: 'already subscribed to this category' }

  # Instance methods
  def soft_delete
    update(deleted_at: Time.current)
  end

  def restore
    update(deleted_at: nil)
  end

  def deleted?
    deleted_at.present?
  end
end