Skip to content

Commit

Permalink
#1567, #1554, bug fix when approve invoice using batch action
Browse files Browse the repository at this point in the history
when Approve invoices from batch action there is loop that use method (approve)
that is not existed.
  • Loading branch information
Ivanov-Anton committed Oct 1, 2024
1 parent c8e61a0 commit 7bd62d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
11 changes: 9 additions & 2 deletions app/admin/billing/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ def scoped_collection
end

batch_action :approve, confirm: 'Are you sure?', if: proc { authorized?(:approve) } do |selection|
active_admin_config.resource_class.find(selection).each(&:approve)
redirect_to collection_path, notice: "#{active_admin_config.resource_label.pluralize} are approved!"
active_admin_config.resource_class.find(selection).each do |record|
BillingInvoice::Approve.call(invoice: record)
flash[:notice] = "#{active_admin_config.resource_label.pluralize} are approved!"
rescue BillingInvoice::Approve::Error => e
flash[:error] = e.message
break
end

redirect_to collection_path
end

member_action :approve, method: :post do
Expand Down
27 changes: 21 additions & 6 deletions spec/features/billing/invoices/approve_invoice_feature_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

RSpec.describe 'Approve invoide feature', type: :feature do
subject { click_action_item 'Approve' }

include_context :login_as_admin

let(:approved_state_id) { Billing::InvoiceState::APPROVED }
let!(:invoice) { FactoryBot.create(:invoice, :manual, :pending, :with_vendor_account) }

context 'when valid data' do
let!(:invoice) { FactoryBot.create(:invoice, :manual, :pending, :with_vendor_account) }
let(:approved_state_id) { Billing::InvoiceState::APPROVED }
subject { click_action_item 'Approve' }

before { visit invoice_path(invoice) }

Expand All @@ -19,9 +19,24 @@
end
end

context 'when invalid data', :js do
let!(:invoice) { FactoryBot.create(:invoice, :pending, :with_vendor_account) }
context 'when approve from batch action' do
subject { click_button :OK }

before do
visit invoices_path
check class: 'toggle_all'
click_batch_action 'Approve Selected'
end

it 'should approve invoice', :js do
subject

expect(page).to have_flash_message 'Invoices are approved!', type: :notice
expect(invoice.reload).to have_attributes(state_id: approved_state_id)
end
end

context 'when invalid data', :js do
before do
allow(BillingInvoice::Approve).to receive(:call).and_raise(BillingInvoice::Approve::Error, 'error')
visit invoice_path(invoice)
Expand Down

0 comments on commit 7bd62d3

Please sign in to comment.