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 2, 2024
1 parent c8e61a0 commit 3a6a0f5
Show file tree
Hide file tree
Showing 2 changed files with 32 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] = "##{record.id}, #{e.message}"
break
end

redirect_to collection_path
end

member_action :approve, method: :post do
Expand Down
29 changes: 23 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' }

RSpec.describe 'Approve invoice feature', type: :feature do
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,8 +19,25 @@
end
end

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
let!(:invoice) { FactoryBot.create(:invoice, :pending, :with_vendor_account) }
subject { click_action_item 'Approve' }

before do
allow(BillingInvoice::Approve).to receive(:call).and_raise(BillingInvoice::Approve::Error, 'error')
Expand Down

0 comments on commit 3a6a0f5

Please sign in to comment.