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

Promo code batch join chars #2662

Merged
merged 3 commits into from
Apr 5, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<%= f.text_field :base_code, class: "fullwidth" %>
<% end %>

<%= f.field_container :join_characters do %>
<%= f.label :join_characters %>
<%= f.text_field :join_characters, class: "fullwidth" %>
<% end %>

<%= f.field_container :number_of_codes do %>
<%= f.label :number_of_codes %>
<%= f.text_field :number_of_codes, class: "fullwidth" %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<%= batch.label :number_of_codes, class: "required" %>
<%= batch.number_field :number_of_codes, class: "fullwidth", min: 1, required: true %>
</div>
<div class="field">
<%= batch.label :join_characters %>
<%= batch.text_field :join_characters, class: "fullwidth" %>
</div>
<div class="field">
<%= f.label :per_code_usage_limit %>
<%= f.text_field :per_code_usage_limit, class: "fullwidth" %>
Expand Down
7 changes: 3 additions & 4 deletions core/app/models/spree/promotion_code/batch_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ class ::Spree::PromotionCode::BatchBuilder
DEFAULT_OPTIONS = {
random_code_length: 6,
batch_size: 1000,
sample_characters: ('a'..'z').to_a + (2..9).to_a.map(&:to_s),
join_characters: "_"
sample_characters: ('a'..'z').to_a + (2..9).to_a.map(&:to_s)
}

[:random_code_length, :batch_size, :sample_characters, :join_characters].each do |attr|
[:random_code_length, :batch_size, :sample_characters].each do |attr|
define_singleton_method(attr) do
Spree::Deprecation.warn "#{name}.#{attr} is deprecated. Use #{name}::DEFAULT_OPTIONS[:#{attr}] instead"
DEFAULT_OPTIONS[attr]
Expand Down Expand Up @@ -70,7 +69,7 @@ def generate_random_code
@options[:sample_characters].sample
end.join

"#{base_code}#{@options[:join_characters]}#{suffix}"
"#{base_code}#{@promotion_code_batch.join_characters}#{suffix}"
end

def get_unique_codes(code_set)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class AddJoinCharactersToPromotionCodeBatch < ActiveRecord::Migration[5.1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.

def change
add_column(:spree_promotion_code_batches,
:join_characters,
:string,
null: false,
default: '_')
end
end
31 changes: 30 additions & 1 deletion core/spec/jobs/promotion_code_batch_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,42 @@
email: email
)
end

context "with a successful build" do
before do
allow(Spree::PromotionCodeBatchMailer)
.to receive(:promotion_code_batch_finished)
.and_call_original
end

def codes
Spree::PromotionCode.pluck(:value)
end

context 'with the default join character' do
it 'uses the default join characters', :aggregate_failures do
subject.perform(promotion_code_batch)
codes.each do |code|
expect(code).to match(/^test_/)
end
end
end
context 'with a custom join character' do
let(:promotion_code_batch) do
Spree::PromotionCodeBatch.create!(
promotion_id: create(:promotion).id,
base_code: "test",
number_of_codes: 10,
email: email,
join_characters: '-'
)
end
it 'uses the custom join characters', :aggregate_failures do
subject.perform(promotion_code_batch)
codes.each do |code|
expect(code).to match(/^test-/)
end
end
end
context "with an email address" do
it "sends an email" do
subject.perform(promotion_code_batch)
Expand Down
10 changes: 9 additions & 1 deletion core/spec/models/spree/promotion_code/batch_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@
end

context "with a custom join separator" do
let(:options) { { join_characters: "x" } }
let(:promotion_code_batch) do
Spree::PromotionCodeBatch.create!(
promotion_id: promotion.id,
base_code: base_code,
number_of_codes: number_of_codes,
email: "test@email.com",
join_characters: "x"
)
end

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