Class: Spree::Admin::PaymentMethodsController

Inherits:
ResourceController show all
Defined in:
app/controllers/spree/admin/payment_methods_controller.rb

Instance Method Summary collapse

Methods inherited from ResourceController

belongs_to, #destroy, #edit, #new, #update_positions

Instance Method Details

#create ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/spree/admin/payment_methods_controller.rb', line 10

def create
  @payment_method = params[:payment_method].delete(:type).constantize.new(payment_method_params)
  @object = @payment_method
  set_current_store
  invoke_callbacks(:create, :before)
  if @payment_method.save
    invoke_callbacks(:create, :after)
    flash[:success] = Spree.t(:successfully_created, resource: Spree.t(:payment_method))
    redirect_to location_after_create
  else
    invoke_callbacks(:create, :fails)
    respond_with(@payment_method, status: :unprocessable_entity)
  end
end

#update ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/spree/admin/payment_methods_controller.rb', line 25

def update
  invoke_callbacks(:update, :before)
  payment_method_type = params[:payment_method].delete(:type)
  if @payment_method['type'].to_s != payment_method_type
    @payment_method.update_columns(
      type: payment_method_type,
      updated_at: Time.current
    )
    @payment_method = scope.find(params[:id])
  end

  attributes = payment_method_params.merge(preferences_params)
  attributes.each do |k, _v|
    attributes.delete(k) if k.include?('password') && attributes[k].blank?
  end

  if @payment_method.update(attributes)
    set_current_store
    invoke_callbacks(:update, :after)
    flash[:success] = Spree.t(:successfully_updated, resource: Spree.t(:payment_method))
    redirect_to spree.edit_admin_payment_method_path(@payment_method)
  else
    invoke_callbacks(:update, :fails)
    respond_with(@payment_method) do |format|
      format.html { render action: :edit, status: :unprocessable_entity }
      format.js { render layout: false, status: :unprocessable_entity }
    end
  end
end