Class: Workarea::Pricing::Discount::GeneratedPromoCode

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Instance Attribute Details

#codeString

Returns the promo code.

Returns:



13
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 13

field :code, type: String

#code_listEnumerable<CodeList>

Returns the code list that generated this code.

Returns:

  • (Enumerable<CodeList>)

    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_atTime?

Returns when the code expires.

Returns:

  • (Time, nil)

    when the code expires



18
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 18

field :expires_at, type: Time

#used_atTime?

Returns when the code was used (if at all).

Returns:

  • (Time, nil)

    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.

Parameters:

Returns:



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

Parameters:

  • optional (String)

    prefix

Returns:



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_expiredMongoid::Criteria

Get codes that have not expired

Returns:



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 }) }

.unusedMongoid::Criteria

Get unused codes

Returns:



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).

Parameters:

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


84
85
86
# File 'app/models/workarea/pricing/discount/generated_promo_code.rb', line 84

def used!
  update_attribute(:used_at, Time.current)
end