Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pascal Ehlert
committed
Dec 28, 2013
1 parent
f9e3858
commit b7b0ceb
Showing
10 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,5 @@ | |||
require "bundler/gem_tasks" | require "bundler/gem_tasks" | ||
require 'rspec/core/rake_task' | |||
|
|||
RSpec::Core::RakeTask.new(:spec) | |||
task default: :spec |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1,11 @@ | |||
require "promocode/version" | require "promocode_generator/version" | ||
require "promocode_generator/model_additions" | |||
|
|
||
module Promocode | module PromocodeGenerator | ||
# Your code goes here... | # Generates an 8 character random code of uppercase letters and numbers | ||
# It explicitly excludes the letters I, L, O and the number 0 to avoid | |||
# confusion | |||
def self.generate(length = 8) | |||
[('A'..'Z').to_a - ['I', 'L', 'O'], ('1'..'9').to_a].flatten.shuffle[0,length].join | |||
end | |||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,14 @@ | |||
module PromocodeGenerator | |||
module ModelAdditions | |||
def promocode_attribute(attribute, options = {}) | |||
options[:reject_if] ||= Proc.new { |code| self.where(attribute => code).any? } | |||
|
|||
before_save do | |||
# Make sure not to generate the same code twice! | |||
begin | |||
self.code = PromocodeGenerator.generate | |||
end while options[:reject_if].call(code) | |||
end | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
module PromocodeGenerator | |||
VERSION = "0.0.1" | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,40 @@ | |||
require 'spec_helper' | |||
|
|||
# Dummy reject condition that can be stubbed in the tests below | |||
module RejectCondition | |||
def self.check(code) | |||
false | |||
end | |||
end | |||
|
|||
class Campaign < SuperModel::Base | |||
include ActiveModel::Validations::Callbacks | |||
extend PromocodeGenerator::ModelAdditions | |||
promocode_attribute :code, :reject_if => Proc.new { |code| RejectCondition.check(code) } | |||
end | |||
|
|||
describe PromocodeGenerator::ModelAdditions do | |||
let(:campaign) { Campaign.new(:name => "foo") } | |||
|
|||
context "when the model is saved" do | |||
it "generates a code" do | |||
expect(PromocodeGenerator).to receive(:generate).once.and_return('ABCDEFG') | |||
campaign.save! | |||
end | |||
|
|||
it "assigns the code to the model" do | |||
campaign.save! | |||
expect(campaign.code).not_to be_nil | |||
end | |||
end | |||
|
|||
context "when the reject_if block returns false" do | |||
before { RejectCondition.stub(:check).and_return(true, false) } | |||
after { RejectCondition.unstub(:check) } | |||
|
|||
it "generates a new code" do | |||
expect(PromocodeGenerator).to receive(:generate).exactly(2).times | |||
campaign.save! | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
require 'spec_helper' | |||
|
|||
describe PromocodeGenerator do | |||
describe ".generate" do | |||
it "generates a code of the given length" do | |||
expect(PromocodeGenerator.generate(10).length).to eq(10) | |||
end | |||
|
|||
it "generates a code with default length" do | |||
expect(PromocodeGenerator.generate.length).to be > 0 | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,2 @@ | |||
require 'promocode' | require 'promocode_generator' | ||
require 'supermodel' |