Class: Billingly::SpecialPlanCode

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/billingly/special_plan_code.rb

Overview

A SpecialPlanCode lets the Customer subscribe to a plan which is hidden from regular customers. Customers would visit your site and redeem the code instead of subscribing to a plan directly. The code will then subscribe them to a plan with the given special pricing.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanup_exported_filesObject



57
58
59
# File 'app/models/billingly/special_plan_code.rb', line 57

def self.cleanup_exported_files
  `rm #{File.expand_path('~/ean_13_codes_for_*.csv')}`
end

.export_codesObject

Exports all the codes for each plan. You may export only the unused ones. The codes are exported into CSV files inside the current user’s home directory. There is one file per plan. Each file contains all the available codes for a given plan.



48
49
50
51
52
53
54
55
# File 'app/models/billingly/special_plan_code.rb', line 48

def self.export_codes
  where('redeemed_on IS NULL').group_by(&:plan).each do |plan, special_codes|
    filename = "ean_13_codes_for_#{plan.description.gsub(/[^a-zA-Z0-9]/,'_')}.csv"
    File.open(File.expand_path("~/#{filename}"),'w') do |file|
      file.write(special_codes.collect(&:code).join("\n"))
    end
  end
end

.find_redeemable(code) ⇒ SpecialPlanCode?

Shorthand finder for a redeemable code. This means that the code exists and has not been redeemed yet.

Parameters:

  • code (String)

    The code to lookup.

Returns:



65
66
67
# File 'app/models/billingly/special_plan_code.rb', line 65

def self.find_redeemable(code)
  self.find_by_code_and_redeemed_on(code, nil)
end

.generate_ean_13_codes(how_many) ⇒ Array<String>

Creates a list of valid random ean-13 codes.

Parameters:

  • how_many (Integer)

    How many codes to create.

Returns:

  • (Array<String>)

    The list of generated ean-13 codes.



25
26
27
28
29
30
31
32
# File 'app/models/billingly/special_plan_code.rb', line 25

def self.generate_ean_13_codes(how_many)
  randoms = Set.new
  loop do
    ean = EAN13.complete( (100000000000 + rand(100000000000)).to_s )
    randoms << ean if ean
    return randoms.to_a if randoms.size == how_many
  end
end

.generate_for_plan(plan, how_many, bonus_amount = nil) ⇒ Object

Generates rows in the database with codes for a specific plan. It creates new rows with new codes, and it makes sure that the new codes don’t clash with pre-existing codes.

Parameters:

  • plan (Plan)

    The plan to generate the codes for.

  • how_many (Integer)

    How many new codes to issue.



39
40
41
42
43
# File 'app/models/billingly/special_plan_code.rb', line 39

def self.generate_for_plan(plan, how_many, bonus_amount = nil)
  generate_ean_13_codes(how_many).collect do |code|
    self.create!(code: code, plan: plan, bonus_amount: bonus_amount)
  end
end

Instance Method Details

#redeemed?Boolean

!@attribute [r] redeemed?

Returns:

  • (Boolean)

    Whether this code has been redeemed or not



18
19
20
# File 'app/models/billingly/special_plan_code.rb', line 18

def redeemed?
  !redeemed_on.nil?   
end