Sha256: c2c06adc5c0b37c9943859425d79b3dcaa18ee422aa4f5ee9e9b117cc4c73d93

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

class Spree::Admin::PromotionRulesController < Spree::Admin::BaseController
  helper 'spree/promotion_rules'

  before_action :load_promotion, only: [:create, :destroy]
  before_action :validate_promotion_rule_type, only: :create

  def create
    @promotion_rule = @promotion_rule_type.new(promotion_rule_params)
    @promotion_rule.promotion = @promotion
    if @promotion_rule.save
      flash[:success] = t('spree.successfully_created', resource: t('spree.promotion_rule'))
    end
    respond_to do |format|
      format.html { redirect_to spree.edit_admin_promotion_path(@promotion) }
      format.js   { render layout: false }
    end
  end

  def destroy
    @promotion_rule = @promotion.promotion_rules.find(params[:id])
    if @promotion_rule.destroy
      flash[:success] = t('spree.successfully_removed', resource: t('spree.promotion_rule'))
    end
    respond_to do |format|
      format.html { redirect_to spree.edit_admin_promotion_path(@promotion) }
      format.js   { render layout: false }
    end
  end

  private

  def load_promotion
    @promotion = Spree::Promotion.find(params[:promotion_id])
  end

  def validate_promotion_rule_type
    requested_type = params[:promotion_rule].delete(:type)
    promotion_rule_types = Rails.application.config.spree.promotions.rules
    @promotion_rule_type = promotion_rule_types.detect do |klass|
      klass.name == requested_type
    end
    if !@promotion_rule_type
      flash[:error] = t('spree.invalid_promotion_rule')
      respond_to do |format|
        format.html { redirect_to spree.edit_admin_promotion_path(@promotion) }
        format.js   { render layout: false }
      end
    end
  end

  def promotion_rule_params
    params[:promotion_rule].permit!
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_backend-2.5.2 app/controllers/spree/admin/promotion_rules_controller.rb
solidus_backend-2.5.1 app/controllers/spree/admin/promotion_rules_controller.rb
solidus_backend-2.5.0 app/controllers/spree/admin/promotion_rules_controller.rb
solidus_backend-2.5.0.rc1 app/controllers/spree/admin/promotion_rules_controller.rb
solidus_backend-2.5.0.beta2 app/controllers/spree/admin/promotion_rules_controller.rb
solidus_backend-2.5.0.beta1 app/controllers/spree/admin/promotion_rules_controller.rb