-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add admin UI for promotion code batches
This was somehow missed in the past. Fixes #93
- Loading branch information
Showing
9 changed files
with
172 additions
and
0 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
22 changes: 22 additions & 0 deletions
22
.../app/views/solidus_friendly_promotions/admin/promotion_code_batches/_form_fields.html.erb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="field"> | ||
<%= batch.label :base_code, class: "required" %> | ||
<%= batch.text_field :base_code, class: "fullwidth", required: true %> | ||
</div> | ||
<div class="field"> | ||
<%= 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> | ||
<% unless promotion_id %> | ||
<div class="field"> | ||
<%= f.label :per_code_usage_limit %> | ||
<%= f.text_field :per_code_usage_limit, class: "fullwidth" %> | ||
</div> | ||
<% end %> | ||
<div class="field"> | ||
<%= batch.label :email %> | ||
<%= batch.text_field :email, class: "fullwidth" %> | ||
</div> |
8 changes: 8 additions & 0 deletions
8
...ions/app/views/solidus_friendly_promotions/admin/promotion_code_batches/download.csv.ruby
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
CSV.generate do |csv| | ||
csv << ["Code"] | ||
@promotion_code_batch.promotion_codes.order(:id).pluck(:value).each do |value| | ||
csv << [value] | ||
end | ||
end |
65 changes: 65 additions & 0 deletions
65
...motions/app/views/solidus_friendly_promotions/admin/promotion_code_batches/index.html.erb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path) %> | ||
<% admin_breadcrumb(link_to @promotion.name, solidus_friendly_promotions.edit_admin_promotion_path(@promotion.id)) %> | ||
<% admin_breadcrumb(plural_resource_name(SolidusFriendlyPromotions::PromotionCodeBatch)) %> | ||
|
||
<% content_for :page_actions do %> | ||
<li> | ||
<% if can?(:create, SolidusFriendlyPromotions::PromotionCodeBatch) %> | ||
<%= link_to t('solidus_friendly_promotions.new_promotion_code_batch'), new_object_url, class: 'btn btn-primary' %> | ||
<% end %> | ||
</li> | ||
<% end %> | ||
|
||
<% if @promotion_code_batches.any? %> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th><%= SolidusFriendlyPromotions::PromotionCodeBatch.human_attribute_name(:base_code) %></th> | ||
<th><%= SolidusFriendlyPromotions::PromotionCodeBatch.human_attribute_name(:total_codes) %></th> | ||
<th><%= SolidusFriendlyPromotions::PromotionCodeBatch.human_attribute_name(:status) %></th> | ||
<th><%= SolidusFriendlyPromotions::PromotionCodeBatch.human_attribute_name(:email) %></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @promotion_code_batches.each do |promotion_code_batch| %> | ||
<tr> | ||
<td><%= promotion_code_batch.base_code %></td> | ||
<td><%= promotion_code_batch.number_of_codes %></td> | ||
<td> | ||
<% if promotion_code_batch.error.present? %> | ||
<%= t( | ||
"solidus_friendly_promotions.promotion_code_batches.errored", | ||
error: promotion_code_batch.error | ||
) %> | ||
<% elsif promotion_code_batch.finished? %> | ||
<%= t( | ||
"solidus_friendly_promotions.promotion_code_batches.finished", | ||
number_of_codes: promotion_code_batch.number_of_codes | ||
) %> | ||
<%= link_to( | ||
t('solidus_friendly_promotions.download_promotion_codes_list'), | ||
admin_promotion_promotion_code_batch_download_path( | ||
promotion_code_batch_id: promotion_code_batch.id, | ||
format: :csv | ||
) | ||
) %> | ||
<% else %> | ||
<%= t( | ||
"solidus_friendly_promotions.promotion_code_batches.processing", | ||
number_of_codes: promotion_code_batch.number_of_codes, | ||
number_of_codes_processed: promotion_code_batch.promotion_codes.count | ||
) %> | ||
<% end %> | ||
</td> | ||
<td><%= promotion_code_batch.email %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<% else %> | ||
<div class="no-objects-found"> | ||
<%= render 'spree/admin/shared/no_objects_found', | ||
resource: SolidusFriendlyPromotions::PromotionCodeBatch, | ||
new_resource_url: new_object_url %> | ||
</div> | ||
<% end %> |
8 changes: 8 additions & 0 deletions
8
...romotions/app/views/solidus_friendly_promotions/admin/promotion_code_batches/new.html.erb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<% admin_breadcrumb(link_to plural_resource_name(Spree::Promotion), spree.admin_promotions_path) %> | ||
<% admin_breadcrumb(link_to @promotion.name, spree.admin_promotion_path(@promotion.id)) %> | ||
<% admin_breadcrumb(plural_resource_name(Spree::PromotionCodeBatch)) %> | ||
<%= form_for :promotion_code_batch, url: collection_url do |batch| %> | ||
<%= batch.hidden_field :promotion_id, value: params[:promotion_id] %> | ||
<%= render partial: 'form_fields', locals: {batch: batch, promotion_id: params[:promotion_id]} %> | ||
<%= batch.submit t('spree.actions.create'), class: 'btn btn-primary' %> | ||
<% 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
46 changes: 46 additions & 0 deletions
46
...y_promotions/spec/system/solidus_friendly_promotions/admin/promotion_code_batches_spec.rb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
feature "Promotion Code Batches", partial_double_verification: false do | ||
stub_authorization! | ||
|
||
describe "create" do | ||
let(:promotion) { create :friendly_promotion } | ||
|
||
before do | ||
allow_any_instance_of(ApplicationController).to receive(:spree_current_user) { build(:user, id: 123) } | ||
visit solidus_friendly_promotions.new_admin_promotion_promotion_code_batch_path(promotion) | ||
end | ||
|
||
def create_code_batch | ||
fill_in "Base code", with: "base" | ||
fill_in "Number of codes", with: 3 | ||
click_button "Create" | ||
end | ||
|
||
it "renders partial without 'Per code usage limit' " do | ||
expect(page).to_not have_field("promotion_per_code_usage_limit") | ||
end | ||
|
||
it "creates a new promotion code batch and disables the submit button", :js do | ||
create_code_batch | ||
|
||
expect(page).to have_content "Code batch has been successfully created!" | ||
|
||
visit solidus_friendly_promotions.new_admin_promotion_promotion_code_batch_path(promotion) | ||
|
||
page.execute_script <<~JS | ||
document.querySelectorAll('form').forEach(function(element) { | ||
addEventListener('submit', function(element) { | ||
element.preventDefault(); | ||
}) | ||
}); | ||
JS | ||
|
||
create_code_batch | ||
|
||
expect(page).to have_button("Create", disabled: true) | ||
end | ||
end | ||
end |