Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep the currency picked for a trade's price (#5842)

Jang Dong Ik6d68f9c157b8
diff --git a/app/views/trades/_form.html.erb b/app/views/trades/_form.html.erb
index b74846a5fa..cde4f593f8 100644
--- a/app/views/trades/_form.html.erb
+++ b/app/views/trades/_form.html.erb
@@ -61,7 +61,11 @@
<% if %w[buy sell].include?(type) %>
<%= form.number_field :qty, label: t(".qty"), placeholder: "10", min: 0.000000000000000001, step: "any", required: true %>
<%= form.money_field :price, label: t(".price"), step: "any", precision: 10, required: true, default_currency: account&.currency %>
- <%= form.money_field :fee, label: t(".fee"), step: "any", min: 0, default_currency: account&.currency %>
+ <%# A trade has one currency. A second picker here would submit its own
+ model[currency] after the price's and override it, so the fee
+ follows the price's instead. %>
+ <%= form.money_field :fee, label: t(".fee"), step: "any", min: 0, default_currency: account&.currency,
+ hide_currency: true, currency_source: "select[name='model[currency]']" %>
<% end %>
</div>
diff --git a/test/controllers/trades_controller_test.rb b/test/controllers/trades_controller_test.rb
index ab4609f275..6cf1937064 100644
--- a/test/controllers/trades_controller_test.rb
+++ b/test/controllers/trades_controller_test.rb
@@ -183,6 +183,16 @@ class TradesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to @investment
end
+ # The price and the fee each had a model[currency] picker, and the fee's,
+ # submitted last, overrode the currency picked for the price.
+ test "a buy form submits one currency, the price's, which the fee follows" do
+ get new_trade_url(account_id: @entry.account_id, type: "buy")
+
+ assert_response :success
+ assert_select "select[name='model[currency]']", count: 1
+ assert_select "[data-money-field-currency-source-value] input[name='model[fee]']"
+ end
+
test "the transfer account picker only offers accounts the member may write to" do
sign_in_as_member_with_writable_investment
diff --git a/test/system/trades_test.rb b/test/system/trades_test.rb
index f8da094275..4d19cafbda 100644
--- a/test/system/trades_test.rb
+++ b/test/system/trades_test.rb
@@ -38,6 +38,23 @@ class TradesTest < ApplicationSystemTestCase
end
end
+ # The fee's currency picker used to be submitted after the price's and
+ # override it, saving a 305.6 EUR price as 305.6 in the account's currency.
+ test "keeps the currency picked for the price" do
+ open_new_trade_modal
+
+ fill_in "Ticker symbol", with: "AAPL"
+ fill_in "Date", with: Date.current
+ fill_in "Quantity", with: 10
+ fill_in "model[price]", with: 305.6
+ first("select[name='model[currency]']").select("EUR")
+
+ click_button "Add transaction"
+
+ assert_text "Entry created"
+ assert_equal "EUR", @account.entries.order(:created_at).last.currency
+ end
+
test "can create sell transaction" do
qty = 10
aapl = @account.holdings.find { |h| h.security.ticker == "AAPL" }