Skip to content

Commit

Permalink
Merge pull request #99 from friendlycart/code-batch-admin
Browse files Browse the repository at this point in the history
Add admin UI for promotion code batches
  • Loading branch information
mamhoff authored Jan 23, 2024
2 parents 9dd68af + 4c74f4a commit 85c1d62
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def collection_url(options = {})
def routes_proxy
solidus_friendly_promotions
end

def parent_model_name
self.class.parent_data[:model_name].gsub("solidus_friendly_promotions/", "")
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def download
def build_promotion_code_batch
@promotion_code_batch.process
end

def model_class
SolidusFriendlyPromotions::PromotionCodeBatch
end

def collection
parent.code_batches
end

def build_resource
parent.code_batches.build
end
end
end
end
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>
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
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 %>
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 %>
6 changes: 6 additions & 0 deletions friendly_promotions/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ en:
no_rules_added: No Rules Added
promotion_successfully_created: Promotion has been successfully created!
promotion_total_changed_before_complete: One or more of the promotions on your order have become ineligible and were removed. Please check the new order amounts and try again.
promotion_code_batches:
finished: Finished
errored: Errored
view_promotion_codes_list: View codes list
promotion_rules:
line_item_product:
Expand Down Expand Up @@ -190,6 +193,9 @@ en:
solidus_friendly_promotions/rules/user: User
solidus_friendly_promotions/rules/user_logged_in: User Logged In
solidus_friendly_promotions/rules/user_role: User Role(s)
solidus_friendly_promotions/promotion_code_batch:
one: Code batch
other: Code batches
attributes:
solidus_friendly_promotions/promotion:
active: Active
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it { is_expected.to respond_to(:customer_label) }
it { is_expected.to have_many :rules }
it { is_expected.to have_many(:order_promotions).dependent(:destroy) }
it { is_expected.to have_many(:code_batches).dependent(:destroy) }

describe "lane" do
it { is_expected.to respond_to(:lane) }
Expand Down
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

0 comments on commit 85c1d62

Please sign in to comment.