/backend/app/controllers/spree/admin/promotions_controller.rb

https://github.com/solidusio/solidus · Ruby · 67 lines · 52 code · 14 blank · 1 comment · 6 complexity · b848a27bba1801c34552192ac1af8940 MD5 · raw file

  1. # frozen_string_literal: true
  2. module Spree
  3. module Admin
  4. class PromotionsController < ResourceController
  5. before_action :load_data
  6. helper 'spree/promotion_rules'
  7. def show
  8. redirect_to action: :edit
  9. end
  10. def create
  11. @promotion = Spree::Promotion.new(permitted_resource_params)
  12. @promotion.codes.new(value: params[:single_code]) if params[:single_code].present?
  13. if params[:promotion_code_batch]
  14. @promotion_code_batch = @promotion.promotion_code_batches.new(promotion_code_batch_params)
  15. end
  16. if @promotion.save
  17. @promotion_code_batch.process if @promotion_code_batch
  18. flash[:success] = t('spree.promotion_successfully_created')
  19. redirect_to location_after_save
  20. else
  21. flash[:error] = @promotion.errors.full_messages.to_sentence
  22. render action: 'new'
  23. end
  24. end
  25. private
  26. def location_after_save
  27. spree.edit_admin_promotion_url(@promotion)
  28. end
  29. def load_data
  30. @calculators = Rails.application.config.spree.calculators.promotion_actions_create_adjustments
  31. @promotion_categories = Spree::PromotionCategory.order(:name)
  32. end
  33. def collection
  34. return @collection if @collection
  35. params[:q] ||= HashWithIndifferentAccess.new
  36. params[:q][:s] ||= 'id desc'
  37. @collection = super
  38. @search = @collection.ransack(params[:q])
  39. @collection = @search.result(distinct: true).
  40. includes(promotion_includes).
  41. page(params[:page]).
  42. per(params[:per_page] || Spree::Config[:promotions_per_page])
  43. @collection
  44. end
  45. def promotion_code_batch_params
  46. params.require(:promotion_code_batch).permit!
  47. end
  48. def promotion_includes
  49. [:promotion_actions]
  50. end
  51. end
  52. end
  53. end