Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow flexible separator for promotion code #1951

Merged
merged 2 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Solidus 2.3.0 (master, unreleased)

- Allow custom separator between a promotion's `base_code` and `suffix` [\#1951](https://github.com/solidusio/solidus/pull/1951) ([ericgross](https://github.com/ericgross))
- Ignore `adjustment.finalized` on tax adjustments. [\#1936](https://github.com/solidusio/solidus/pull/1936) ([jordan-brough](https://github.com/jordan-brough))
- Deprecate `#simple_current_order`
[\#1915](https://github.com/solidusio/solidus/pull/1915) ([ericsaupe](https://github.com/ericsaupe))
Expand Down
5 changes: 3 additions & 2 deletions core/app/models/spree/promotion_code/batch_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ class ::Spree::PromotionCode::BatchBuilder
attr_reader :promotion_code_batch
delegate :promotion, :number_of_codes, :base_code, to: :promotion_code_batch

class_attribute :random_code_length, :batch_size, :sample_characters
class_attribute :random_code_length, :batch_size, :sample_characters, :join_characters
self.random_code_length = 6
self.batch_size = 1_000
self.sample_characters = ('a'..'z').to_a + (2..9).to_a.map(&:to_s)
self.join_characters = "_"

def initialize(promotion_code_batch)
@promotion_code_batch = promotion_code_batch
Expand Down Expand Up @@ -54,7 +55,7 @@ def generate_random_code
sample_characters.sample
end.join

"#{base_code}_#{suffix}"
"#{base_code}#{join_characters}#{suffix}"
end

def get_unique_codes(code_set)
Expand Down
28 changes: 25 additions & 3 deletions core/spec/models/spree/promotion_code/batch_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
end

subject { described_class.new promotion_code_batch }
subject { described_class.new(promotion_code_batch) }

describe "#build_promotion_codes" do
context "with a failed build" do
Expand All @@ -27,6 +27,7 @@
expect(promotion_code_batch.reload.state).to eq("failed")
end
end

context "with a successful build" do
before do
allow(Spree::PromotionCodeBatchMailer)
Expand All @@ -48,13 +49,34 @@
expect(subject.promotion.codes.map(&:value).uniq.size).to eq(10)
end

it "updates the promotion code batch state to completed" do
expect(promotion_code_batch.state).to eq("completed")
end
end
end

describe "#join_character" do
context "with the default join charachter _" do
it "builds codes with the same base prefix" do
subject.build_promotion_codes

values = subject.promotion.codes.map(&:value)
expect(values.all? { |val| val.starts_with?("#{base_code}_") }).to be true
end
end

it "updates the promotion code batch state to completed" do
expect(promotion_code_batch.state).to eq("completed")
context "with a custom join separator" do
around do |example|
Spree::PromotionCode::BatchBuilder.join_characters = "x"
example.run
Spree::PromotionCode::BatchBuilder.join_characters = "_"
end

it "builds codes with the same base prefix" do
subject.build_promotion_codes

values = subject.promotion.codes.map(&:value)
expect(values.all? { |val| val.starts_with?("#{base_code}x") }).to be true
end
end
end
Expand Down