Sha256: 73fb2d24883f759ac691cec26edece81bfd44fd85084c0e090ea6c967e0256c8

Contents?: true

Size: 1.71 KB

Versions: 23

Compression:

Stored size: 1.71 KB

Contents

class Spree::Admin::PromotionRulesController < Spree::Admin::BaseController
  helper 'spree/admin/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] = Spree.t(:successfully_created, resource: Spree.t(: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] = Spree.t(:successfully_removed, resource: Spree.t(: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 = current_store.promotions.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
    unless @promotion_rule_type
      flash[:error] = Spree.t(: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

23 entries across 23 versions & 1 rubygems

Version Path
spree_backend-4.8.4 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.8.3 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.8.2 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.8.1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.3.3 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.7.2 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.6.2 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.7.1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.7.0 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.6.1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.6.0 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.5.1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.5.0 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.3.2 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.4.1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.4.0 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.4.0.rc2 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.3.1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.4.0.rc1 app/controllers/spree/admin/promotion_rules_controller.rb
spree_backend-4.3.0 app/controllers/spree/admin/promotion_rules_controller.rb