Skip to content

Commit

Permalink
Cleanup admin panel implementation for bulk promo codes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalcymerys committed Feb 14, 2024
1 parent 3d957d3 commit 70e43f0
Show file tree
Hide file tree
Showing 23 changed files with 523 additions and 255 deletions.
96 changes: 40 additions & 56 deletions app/controllers/spree/admin/promotion_batches_controller.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,46 @@
module Spree
module Admin
class PromotionBatchesController < ResourceController
def update
if @object.template_promotion_id
flash[:error] = Spree.t(:template_promotion_already_assigned)
respond_with(@object) do |format|
format.html { render action: :edit, status: :unprocessable_entity }
format.js { render layout: false, status: :unprocessable_entity }
end
return
end
super
end

def destroy
result = Spree::PromotionBatches::Destroy.call(promotion_batch: @promotion_batch)

if result.success?
flash[:success] = flash_message_for(@promotion_batch, :successfully_removed)
else
flash[:error] = @promotion_batch.errors.full_messages.join(', ')
end

respond_with(@promotion_batch) do |format|
format.html { redirect_to location_after_destroy }
format.js { render_js_for_destroy }
end
end

def csv_export
send_data Spree::PromotionBatches::PromotionCodesExporter.new(params).call,
filename: "promo_codes_from_batch_id_#{params[:id]}.csv",
disposition: :attachment,
type: 'text/csv'
end

def csv_import
file = params[:file]
Spree::PromotionBatches::PromotionCodesImporter.new(file: file, promotion_batch_id: params[:id]).call
redirect_back fallback_location: admin_promotions_path, notice: Spree.t('code_upload')
rescue Spree::PromotionBatches::PromotionCodesImporter::Error => e
redirect_back fallback_location: admin_promotions_path, alert: e.message
end

def populate
batch_id = params[:id]
options = {
batch_size: params[:batch_size].to_i,
affix: params.dig(:code, :affix)&.to_sym,
content: params[:affix_content],
deny_list: params[:forbidden_phrases].split,
random_part_bytes: params[:random_part_bytes].to_i
}

Spree::Promotions::PopulatePromotionBatch.new(batch_id, options).call

flash[:success] = Spree.t('promotion_batch_populated')
redirect_to spree.edit_admin_promotion_batch_url(@promotion_batch)
before_action :set_template_promotion

def index
@promotion_batches = Spree::PromotionBatch.where(template_promotion: @template_promotion)
end

def new
@promotion_batch = @template_promotion.promotion_batches.build
end

def create
Spree::PromotionBatches::CreateWithRandomCodes.new.call(template_promotion: @template_promotion, amount: params[:amount].to_i, random_characters: params[:random_characters].to_i, prefix: params[:prefix], suffix: params[:suffix])
end

def import; end

def process_import
file = params[:file].read
Spree::PromotionBatches::CreateWithCodes.new.call(template_promotion: @template_promotion, codes: file.split("\n"))
redirect_to(admin_template_promotion_promotion_batches_path(template_promotion_id: @template_promotion.id))
end

def export
@promotion_batch = @template_promotion.promotion_batches.find(params[:promotion_batch_id])
csv = Spree::PromotionBatches::Export.new.call(promotion_batch: @promotion_batch)
send_data csv, filename: "codes_#{@promotion_batch}.csv"
end

private

def collection_url
admin_template_promotion_promotion_batches_url(@template_promotion)
end

def new_object_url(options = nil)
new_admin_template_promotion_promotion_batch_url(@template_promotion)
end

def set_template_promotion
@template_promotion = Spree::Promotion.templates.find(params[:template_promotion_id])
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/spree/admin/promotions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def collection
params[:q] ||= HashWithIndifferentAccess.new
params[:q][:s] ||= 'id desc'

@collection = super
@collection = @collection.non_batched
@collection = super.where(template: false)
@search = @collection.ransack(params[:q])
@collection = @search.result(distinct: true).
includes(promotion_includes).
Expand Down
82 changes: 82 additions & 0 deletions app/controllers/spree/admin/template_promotions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Spree
module Admin
class TemplatePromotionsController < ResourceController
before_action :load_data

def create
invoke_callbacks(:create, :before)
@object.attributes = permitted_resource_params
@object.template = true
if @object.save
invoke_callbacks(:create, :after)
flash[:success] = flash_message_for(@object, :successfully_created)
respond_with(@object) do |format|
format.turbo_stream if create_turbo_stream_enabled?
format.html { redirect_to location_after_save }
format.js { render layout: false }
end
else
invoke_callbacks(:create, :fails)
respond_with(@object) do |format|
format.html { render action: :new, status: :unprocessable_entity }
format.js { render layout: false, status: :unprocessable_entity }
end
end
end

private

def new_object_url(options = {})
spree.new_admin_template_promotion_url(options)
end

def collection_url(options = {})
spree.admin_template_promotions_url(options)
end

def resource
return @resource if @resource

parent_model_name = parent_data[:model_name] if parent_data
@resource = Spree::Admin::Resource.new 'admin/spree', 'template_promotions', parent_model_name, object_name
end

def load_data
@actions = Rails.application.config.spree.promotions.actions

@calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
@promotion_categories = Spree::PromotionCategory.order(:name)
@promotion = @object
end

def collection
return @collection if defined?(@collection)

params[:q] ||= HashWithIndifferentAccess.new
params[:q][:s] ||= 'id desc'

@collection = super
@collection = @collection.templates
@search = @collection.ransack(params[:q])
@collection = @search.result(distinct: true).
includes(promotion_includes).
page(params[:page]).
per(params[:per_page] || Spree::Backend::Config[:admin_promotions_per_page])

@promotions = @collection
end

def promotion_includes
[:promotion_actions, :promotions_from_template]
end

def model_class
Spree::Promotion
end

def permitted_resource_params
params.require(:promotion).permit!
end
end
end
end
6 changes: 3 additions & 3 deletions app/helpers/spree/admin/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ def product_properties_actions
Rails.application.config.spree_backend.actions[:product_properties]
end

def promotion_batch_actions
Rails.application.config.spree_backend.actions[:promotion_batch_actions]
def template_promotion_actions
Rails.application.config.spree_backend.actions[:template_promotion]
end

def promotion_batches_actions
Rails.application.config.spree_backend.actions[:promotion_batches_actions]
Rails.application.config.spree_backend.actions[:promotion_batches]
end
# rubocop:enable Metrics/ModuleLength
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,49 @@ class PromotionBatchesDefaultActionsBuilder

def build
root = Root.new
add_new_promotion_batch_action(root)
add_view_promotions_action(root)
add_import_promotion_batch_action(root)
add_generate_promotion_batch_action(root)
root
end

private

def add_new_promotion_batch_action(root)
def add_generate_promotion_batch_action(root)
action =
ActionBuilder.new('new_promotion_batch', new_admin_promotion_batch_path).
ActionBuilder.new('generate_codes', ->(template_promotion) { new_admin_template_promotion_promotion_batch_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.generate_codes').
with_icon_key('add.svg').
with_style(::Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(::Spree::PromotionBatch).
with_style(Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_import_promotion_batch_action(root)
action =
ActionBuilder.new('import_csv', ->(template_promotion) { import_admin_template_promotion_promotion_batches_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.import_csv').
with_icon_key('file-earmark-arrow-up.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_view_promotions_action(root)
action =
ActionBuilder.new('view_promotions', ->(template_promotion) { admin_promotions_path(q: { for_template_promotion_id: template_promotion.id }) }).
with_label_translation_key('admin.promotion_batches.view_promotions').
with_icon_key('list.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_manage_ability_check(Spree::Promotion).
build

root.add(action)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Spree
module Admin
module Actions
class TemplatePromotionDefaultActionsBuilder
include Spree::Core::Engine.routes.url_helpers

def build
root = Root.new
add_view_promotion_batches_action(root)
add_import_promotion_batch_action(root)
add_generate_promotion_batch_action(root)
root
end

private

def add_generate_promotion_batch_action(root)
action =
ActionBuilder.new('generate_codes', ->(template_promotion) { new_admin_template_promotion_promotion_batch_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.generate_codes').
with_icon_key('add.svg').
with_style(Spree::Admin::Actions::ActionStyle::PRIMARY).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_import_promotion_batch_action(root)
action =
ActionBuilder.new('import_csv', ->(template_promotion) { import_admin_template_promotion_promotion_batches_path(template_promotion_id: template_promotion.id) }).
with_label_translation_key('admin.promotion_batches.import_csv').
with_icon_key('file-earmark-arrow-up.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_create_ability_check(Spree::PromotionBatch).
build

root.add(action)
end

def add_view_promotion_batches_action(root)
action =
ActionBuilder.new('view_promotion_batches', ->(template_promotion) { admin_template_promotion_promotion_batches_path(template_promotion) }).
with_label_translation_key('admin.promotion_batches.view_promotions').
with_icon_key('list.svg').
with_style(Spree::Admin::Actions::ActionStyle::LIGHT).
with_manage_ability_check(Spree::Promotion).
build

root.add(action)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def build
add_integrations_section(root)
add_oauth_section(root)
add_settings_section(root)
add_promotion_batches_section(root)
root
end

Expand Down Expand Up @@ -138,6 +137,10 @@ def add_promotions_section(root)
ItemBuilder.new('promotion_categories', admin_promotion_categories_path).
with_admin_ability_check(Spree::PromotionCategory).
with_label_translation_key('admin.tab.promotion_categories').
build,
ItemBuilder.new('bulk_promo_codes', admin_template_promotions_path).
with_admin_ability_check(Spree::Promotion, Spree::PromotionBatch).
with_label_translation_key('admin.tab.bulk_promo_codes').
build
]

Expand Down Expand Up @@ -256,13 +259,6 @@ def add_settings_section(root)
build
root.add(section)
end

def add_promotion_batches_section(root)
root.add(ItemBuilder.new('promotion_batches', admin_promotion_batches_path).
with_icon_key('stack.svg').
with_admin_ability_check(Spree::PromotionBatch).
build)
end
# rubocop:enable Metrics/AbcSize
end
# rubocop:enable Metrics/ClassLength
Expand Down
Loading

0 comments on commit 70e43f0

Please sign in to comment.