Class: Spree::Admin::BankAccountsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/admin/bank_accounts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 15

def create
  @bank_account = @payment_method.bank_accounts.new()

  if @bank_account.save
    redirect_to_index 'Bank account created.'
  else
    render :new
  end
rescue JSON::ParserError
  @bank_account = @payment_method.bank_accounts.new(.except(:details))
  @bank_account.errors.add(:details, 'must contain valid JSON')
  render :new
end

#destroyObject



53
54
55
56
57
58
59
60
61
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 53

def destroy
  if @bank_account.synced?
    flash[:error] = 'Synced accounts cannot be deleted. Deactivate instead.'
  else
    @bank_account.destroy
  end

  redirect_to_index
end

#editObject



29
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 29

def edit; end

#indexObject



7
8
9
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 7

def index
  @bank_accounts = @payment_method.bank_accounts.order(:currency, :id)
end

#newObject



11
12
13
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 11

def new
  @bank_account = @payment_method.bank_accounts.new
end

#syncObject

Never builds and holds a plan across the request: apply! with no argument derives and validates the plan itself, including the abort guard against a provider auth failure returning [] (which plan would otherwise read as "every account disappeared" and deactivate all of them). Passing a pre-built plan here would bypass that guard entirely -- see SyncAccounts#apply! and #plan.

Rescues only EmptyResponseError -- the one operational failure SyncAccounts itself distinguishes from a programming error. A bare rescue StandardError here would also swallow a NoMethodError or similar bug and report it to the admin as an unremarkable "Sync failed", hiding it instead of surfacing it as the 500 it should be.



88
89
90
91
92
93
94
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 88

def sync
  Spree::BankPayments::SyncAccounts.new(payment_method: @payment_method).apply!
  redirect_to_index 'Accounts synced.'
rescue Spree::BankPayments::SyncAccounts::EmptyResponseError => e
  flash[:error] = "Sync failed: #{e.message}. No accounts were changed."
  redirect_to_index
end

#toggle_offeredObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 63

def toggle_offered
  Spree::BankPayments::BankAccount.transaction do
    @payment_method.bank_accounts.
      for_currency(@bank_account.currency).offered.
      where.not(id: @bank_account.id).
      update_all(offered: false)

    @bank_account.update!(offered: !@bank_account.offered?)
  end

  redirect_to_index
end

#updateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/spree/admin/bank_accounts_controller.rb', line 31

def update
  # Synced accounts are the provider's record, not ours -- editing their
  # currency or details here would silently diverge from the account
  # actually watched. Checked against the *raw*, unparsed params so a
  # malformed `details` payload can't slip past this guard by raising
  # JSON::ParserError before it runs (see the JSON.parse call in
  # #bank_account_params).
  if @bank_account.synced? && synced_field_present?
    flash[:error] = 'Synced accounts cannot be edited. Re-sync to refresh them.'
    return redirect_to_index
  end

  if @bank_account.update()
    redirect_to_index 'Bank account updated.'
  else
    render :edit
  end
rescue JSON::ParserError
  @bank_account.errors.add(:details, 'must contain valid JSON')
  render :edit
end