module PromocodeGenerator::ModelAdditions

Public Instance Methods

promocode_attribute(attribute, options = {}) click to toggle source
# File lib/promocode_generator/model_additions.rb, line 3
def promocode_attribute(attribute, options = {})
  options[:reject_if] ||= Proc.new { |code| self.where(attribute => code).any? }

  before_create do
    # Make sure not to generate the same code twice!
    begin
      self.send("#{attribute}=", PromocodeGenerator.generate)
    end while options[:reject_if].call(self.send(attribute))
  end
end