Permalink
Browse files

Don't regenerate code on update

  • Loading branch information...
1 parent b7b0ceb commit a903f686263a7fa1580a45adf9646060c8e7d64c Pascal Ehlert committed Dec 28, 2013
View
@@ -24,7 +24,7 @@ Without Rails:
With Rails:
- In your model, use the promocode_attribute method to automatically generate a promotional code of length 8 in the before_save hook:
+ In your model, use the promocode_attribute method to automatically generate a promotional code of length 8 in the before_create hook:
```ruby
class Campaign < ActiveRecord::Base
@@ -3,7 +3,7 @@ module ModelAdditions
def promocode_attribute(attribute, options = {})
options[:reject_if] ||= Proc.new { |code| self.where(attribute => code).any? }
- before_save do
+ before_create do
# Make sure not to generate the same code twice!
begin
self.code = PromocodeGenerator.generate
@@ -1,3 +1,3 @@
module PromocodeGenerator
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
@@ -16,7 +16,7 @@ class Campaign < SuperModel::Base
describe PromocodeGenerator::ModelAdditions do
let(:campaign) { Campaign.new(:name => "foo") }
- context "when the model is saved" do
+ context "when a new model is saved" do
it "generates a code" do
expect(PromocodeGenerator).to receive(:generate).once.and_return('ABCDEFG')
campaign.save!
@@ -28,6 +28,16 @@ class Campaign < SuperModel::Base
end
end
+ context "when an existing model is saved" do
+ it "does not touch the code" do
+ campaign.save!
+ code = campaign.code
+
+ campaign.name = "blubb"
+ expect { campaign.save! }.not_to change { campaign.code }
+ end
+ end
+
context "when the reject_if block returns false" do
before { RejectCondition.stub(:check).and_return(true, false) }
after { RejectCondition.unstub(:check) }

0 comments on commit a903f68

Please sign in to comment.