-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup admin panel implementation for bulk promo codes
- Loading branch information
1 parent
d4e771d
commit 2984a67
Showing
23 changed files
with
523 additions
and
255 deletions.
There are no files selected for viewing
96 changes: 40 additions & 56 deletions
96
app/controllers/spree/admin/promotion_batches_controller.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
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
82 changes: 82 additions & 0 deletions
82
app/controllers/spree/admin/template_promotions_controller.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,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 |
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
27 changes: 0 additions & 27 deletions
27
app/models/spree/admin/actions/promotion_batch_default_actions_builder.rb
This file was deleted.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
app/models/spree/admin/actions/template_promotion_default_actions_builder.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,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 |
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
Oops, something went wrong.