Class: Workarea::Pricing::Discount::CodeList

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/pricing/discount/code_list.rb

Instance Attribute 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

#countArray

Returns the number of codes to generate.

Returns:

  • (Array)

    the number of codes to generate



25
# File 'app/models/workarea/pricing/discount/code_list.rb', line 25

field :count, type: Integer

#expires_atArray

Returns when these codes expire.

Returns:

  • (Array)

    when these codes expire



20
# File 'app/models/workarea/pricing/discount/code_list.rb', line 20

field :expires_at, type: Time

#generation_completed_atArray

Returns the number of codes to generate.

Returns:

  • (Array)

    the number of codes to generate



30
# File 'app/models/workarea/pricing/discount/code_list.rb', line 30

field :generation_completed_at, type: Time

#nameString

Returns name.

Returns:



10
# File 'app/models/workarea/pricing/discount/code_list.rb', line 10

field :name, type: String

#prefixString

Returns string to prefix the generated codes.

Returns:

  • (String)

    string to prefix the generated codes



15
# File 'app/models/workarea/pricing/discount/code_list.rb', line 15

field :prefix, type: String

#promo_codesEnumerable<GeneratedPromoCode>

Returns the list of promo codes that have been generated.

Returns:



42
43
44
# File 'app/models/workarea/pricing/discount/code_list.rb', line 42

has_many :promo_codes,
class_name: 'Workarea::Pricing::Discount::GeneratedPromoCode',
dependent: :delete

Instance Method Details

#generate_promo_codes!Integer

This method creates a set of promo codes for this discount.

Returns:

  • (Integer)

    the number of generated codes



52
53
54
55
# File 'app/models/workarea/pricing/discount/code_list.rb', line 52

def generate_promo_codes!
  count.times { generate_code }
  update_attributes!(generation_completed_at: Time.current)
end

#generation_complete?Boolean

Whether the list finished generating its codes

Returns:

  • (Boolean)


61
62
63
# File 'app/models/workarea/pricing/discount/code_list.rb', line 61

def generation_complete?
  generation_completed_at.present?
end

#valid_codes?(test_codes) ⇒ Boolean

Whether any of the strings passed is a qualifying code for this discount. Used when checking discount conditions.

Parameters:

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'app/models/workarea/pricing/discount/code_list.rb', line 71

def valid_codes?(test_codes)
  promo_codes
    .not_expired
    .unused
    .where(:code.in => test_codes.map(&:to_s).map(&:downcase))
    .exists?
end