Class: Workarea::Pricing::Discount::GeneratedPromoCode
- Inherits:
-
Object
- Object
- Workarea::Pricing::Discount::GeneratedPromoCode
- Includes:
- ApplicationDocument
- Defined in:
- app/models/workarea/pricing/discount/generated_promo_code.rb
Overview
This class represents one instance of a promo code generated by a CodeList.
Instance Attribute Summary collapse
-
#code ⇒ String
The promo code.
-
#code_list ⇒ Enumerable<CodeList>
The code list that generated this code.
-
#expires_at ⇒ Time?
When the code expires.
-
#used_at ⇒ Time?
When the code was used (if at all).
Class Method Summary collapse
-
.find_by_code(code) ⇒ GeneratedPromoCode?
Find an instance based on its code.
-
.generate_code(prefix = nil) ⇒ String
Generate a new, unique code.
-
.not_expired ⇒ Mongoid::Criteria
Get codes that have not expired.
-
.unused ⇒ Mongoid::Criteria
Get unused codes.
-
.valid_code?(code) ⇒ Boolean
Whether the code passed is unused and not expired (i.e. valid for customer use).
Instance Method Summary collapse
-
#used! ⇒ Boolean
Mark this code as being used, so it can’t be used again.
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
Methods included from Mongoid::Document
Instance Attribute Details
#code ⇒ String
Returns the promo code.
13 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 13 field :code, type: String |
#code_list ⇒ Enumerable<CodeList>
Returns the code list that generated this code.
29 30 31 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 29 belongs_to :code_list, class_name: 'Workarea::Pricing::Discount::CodeList', index: true |
#expires_at ⇒ Time?
Returns when the code expires.
18 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 18 field :expires_at, type: Time |
#used_at ⇒ Time?
Returns when the code was used (if at all).
23 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 23 field :used_at, type: Time |
Class Method Details
.find_by_code(code) ⇒ GeneratedPromoCode?
Find an instance based on its code. Case-insensitive.
66 67 68 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 66 def self.find_by_code(code) find_by(code: code.strip.downcase) rescue nil end |
.generate_code(prefix = nil) ⇒ String
Generate a new, unique code
57 58 59 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 57 def self.generate_code(prefix = nil) "#{prefix}#{SecureRandom.hex(3)}".downcase end |
.not_expired ⇒ Mongoid::Criteria
Get codes that have not expired
45 46 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 45 scope :not_expired, -> { any_of({ expires_at: nil }, { :expires_at.gt => Time.current }) } |
.unused ⇒ Mongoid::Criteria
Get unused codes
38 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 38 scope :unused, -> { where(used_at: nil) } |
.valid_code?(code) ⇒ Boolean
Whether the code passed is unused and not expired (i.e. valid for customer use).
76 77 78 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 76 def self.valid_code?(code) unused.not_expired.where(code: code.strip.downcase).exists? end |
Instance Method Details
#used! ⇒ Boolean
Mark this code as being used, so it can’t be used again.
84 85 86 |
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 84 def used! update_attribute(:used_at, Time.current) end |