Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ def clear_filter
end

def create
account = Current.user.accessible_accounts.find(params.dig(:entry, :account_id))
account = Current.user.accessible_accounts.find_by(id: params.dig(:entry, :account_id))

if account.nil?
@entry = Current.family.entries.new(entry_params)
@entry.valid?
set_new_transaction_form_options
render :new, status: :unprocessable_entity
return
end

return unless require_account_permission!(account)

Expand Down
21 changes: 21 additions & 0 deletions test/controllers/transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_enqueued_with(job: SyncJob)
end

test "create without an account re-renders the form instead of raising" do
assert_no_difference [ "Entry.count", "Transaction.count" ] do
post transactions_url, params: {
entry: {
account_id: "",
name: "New transaction",
date: Date.current,
currency: "USD",
amount: 100,
nature: "inflow",
entryable_type: "Transaction",
entryable_attributes: {
category_id: Category.first.id
}
}
}
end

assert_response :unprocessable_entity
end

test "updates with transaction details" do
assert_no_difference [ "Entry.count", "Transaction.count" ] do
patch transaction_url(@entry), params: {
Expand Down
Loading