Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[33383] Treat number fields as text fields to allow localized input

Oliver Günther47a07210d54b
diff --git a/modules/budgets/app/controllers/budgets_controller.rb b/modules/budgets/app/controllers/budgets_controller.rb
index b6f22f1fa5f..cebf7844003 100644
--- a/modules/budgets/app/controllers/budgets_controller.rb
+++ b/modules/budgets/app/controllers/budgets_controller.rb
@@ -178,7 +178,7 @@ class BudgetsController < ApplicationController
user = User.where(id: params[:user_id]).first
if user && params[:hours]
- hours = params[:hours].to_s.to_hours
+ hours = Rate.parse_number_string_to_number(params[:hours])
@costs = hours * user.rate_at(params[:fixed_date], @project).rate rescue 0.0
else
@costs = 0.0
diff --git a/modules/budgets/app/models/budget.rb b/modules/budgets/app/models/budget.rb
index cc55451cc8a..4e683f71085 100644
--- a/modules/budgets/app/models/budget.rb
+++ b/modules/budgets/app/models/budget.rb
@@ -162,13 +162,16 @@ class Budget < ApplicationRecord
def new_material_budget_item_attributes=(material_budget_item_attributes)
material_budget_item_attributes.each do |_index, attributes|
- material_budget_items.build(attributes) if attributes[:units].to_i.positive?
+ correct_material_attributes!(attributes)
+
+ material_budget_items.build(attributes) if attributes[:units].to_f.positive?
end
end
def existing_material_budget_item_attributes=(material_budget_item_attributes)
material_budget_items.reject(&:new_record?).each do |material_budget_item|
attributes = material_budget_item_attributes[material_budget_item.id.to_s]
+ correct_material_attributes!(attributes)
if User.current.allowed_to? :edit_budgets, material_budget_item.budget.project
if attributes && attributes[:units].to_i.positive?
@@ -188,8 +191,9 @@ class Budget < ApplicationRecord
def new_labor_budget_item_attributes=(labor_budget_item_attributes)
labor_budget_item_attributes.each do |_index, attributes|
- if valid_labor_budget_attributes?(attributes)
+ correct_labor_attributes!(attributes)
+ if valid_labor_budget_attributes?(attributes)
item = labor_budget_items.build(attributes)
item.budget = self # to please the labor_budget_item validation
end
@@ -199,6 +203,8 @@ class Budget < ApplicationRecord
def existing_labor_budget_item_attributes=(labor_budget_item_attributes)
labor_budget_items.reject(&:new_record?).each do |labor_budget_item|
attributes = labor_budget_item_attributes[labor_budget_item.id.to_s]
+ correct_labor_attributes!(attributes)
+
if User.current.allowed_to? :edit_budgets, labor_budget_item.budget.project
if valid_labor_budget_attributes?(attributes)
labor_budget_item.attributes = attributes.merge(amount: Rate.parse_number_string(attributes[:amount]))
@@ -215,9 +221,21 @@ class Budget < ApplicationRecord
end
end
+ def correct_labor_attributes!(attributes)
+ return unless attributes
+
+ attributes[:hours] = Rate.parse_number_string_to_number(attributes[:hours])
+ end
+
+ def correct_material_attributes!(attributes)
+ return unless attributes
+
+ attributes[:units] = Rate.parse_number_string_to_number(attributes[:units])
+ end
+
def valid_labor_budget_attributes?(attributes)
attributes &&
- attributes[:hours].to_i.positive? &&
+ attributes[:hours].to_f.positive? &&
attributes[:user_id].to_i.positive? &&
project.possible_assignees.map(&:id).include?(attributes[:user_id].to_i)
end
diff --git a/modules/budgets/app/views/budgets/items/_labor_budget_item.html.erb b/modules/budgets/app/views/budgets/items/_labor_budget_item.html.erb
index 377cbed4d02..d2c7fd7832b 100644
--- a/modules/budgets/app/views/budgets/items/_labor_budget_item.html.erb
+++ b/modules/budgets/app/views/budgets/items/_labor_budget_item.html.erb
@@ -42,26 +42,33 @@ See docs/COPYRIGHT.rdoc for more details.
error_messages = error_messages_for 'labor_budget_item'
-%>
-<% unless error_messages.blank? %><tr><td colspan="5"><%= error_messages %></td></tr><% end %>
+<% unless error_messages.blank? %>
+ <tr>
+ <td colspan="5"><%= error_messages %></td>
+ </tr>
+<% end %>
<%= fields_for prefix, labor_budget_item do |cost_form| %>
<tr class="cost_entry <%= classes %>" id="<%= id_prefix %>">
<td class="units">
<div class="form--field-container">
<div class="form--text-field-container -xslim">
<label class="hidden-for-sighted" for="<%= id_prefix %>_units"><%= LaborBudgetItem.human_attribute_name(:hours) %></label>
- <%= cost_form.number_field :hours,
- index: id_or_index,
- min: 0,
- step: 0.01,
- class: 'budget-item-value form--text-field',
- data: { :'request-key' => 'hours' } %>
+ <%= cost_form.text_field :hours,
+ index: id_or_index,
+ value: unitless_currency_number(labor_budget_item.hours),
+ # Avoid a number field due to chrome bug (OP#32232)
+ # but show the appearance of a decimal field on mobile
+ inputmode: :decimal,
+ placeholder: t(:label_example_placeholder, decimal: unitless_currency_number(1000.50)),
+ class: 'budget-item-value form--text-field',
+ data: { :'request-key' => 'hours' } %>
</div>
</div>
</td>
<td class="user">
<label class="hidden-for-sighted" for="<%= id_prefix %>_user_id"><%= t(:label_user) %></label>
<%= cost_form.select :user_id,
- @project.possible_assignees.sort.map{|u| [u.name, u.id]},
+ @project.possible_assignees.sort.map { |u| [u.name, u.id] },
{ prompt: true },
{
index: id_or_index,
@@ -74,7 +81,7 @@ See docs/COPYRIGHT.rdoc for more details.
<label class="hidden-for-sighted" for="<%= id_prefix %>_comments"><%= LaborBudgetItem.human_attribute_name(:comments) %></label>
<%= cost_form.text_field :comments, index: id_or_index, size: 40 %>
</td>
- <% if User.current.allowed_to?(:view_cost_rates, @project)%>
+ <% if User.current.allowed_to?(:view_cost_rates, @project) %>
<td class="currency budget-table--fields">
<%# Keep current budget as hidden field because otherwise they will be overridden %>
<% if templated == false && !labor_budget_item.new_record? && labor_budget_item.overridden_costs? %>
diff --git a/modules/budgets/app/views/budgets/items/_material_budget_item.html.erb b/modules/budgets/app/views/budgets/items/_material_budget_item.html.erb
index 57f9dac95a7..99293ed56c2 100644
--- a/modules/budgets/app/views/budgets/items/_material_budget_item.html.erb
+++ b/modules/budgets/app/views/budgets/items/_material_budget_item.html.erb
@@ -42,64 +42,71 @@ See docs/COPYRIGHT.rdoc for more details.
error_messages = error_messages_for 'material_budget_item'
-%>
-<% unless error_messages.blank? %><tr><td colspan="5"><%= error_messages %></td></tr><% end %>
+<% unless error_messages.blank? %>
+ <tr>
+ <td colspan="5"><%= error_messages %></td>
+ </tr>
+<% end %>
<%= fields_for prefix, material_budget_item do |cost_form| %>
-<tr class="cost_entry <%= classes %>" id="<%= id_prefix %>">
- <td class="units">
- <div class="form--field-container">
+ <tr class="cost_entry <%= classes %>" id="<%= id_prefix %>">
+ <td class="units">
+ <div class="form--field-container">
<div class="form--text-field-container -xslim">
- <label class="hidden-for-sighted" for="<%= id_prefix %>_units"><%= MaterialBudgetItem.human_attribute_name(:units) %></label>
- <%= cost_form.number_field :units,
- index: id_or_index,
- min: 0,
- step: 0.01,
- class: 'budget-item-value form--text-field',
- data: { :'request-key' => 'units' } %>
+ <label class="hidden-for-sighted" for="<%= id_prefix %>_units"><%= MaterialBudgetItem.human_attribute_name(:units) %></label>
+ <%= cost_form.text_field :units,
+ index: id_or_index,
+ value: unitless_currency_number(material_budget_item.units),
+ # Avoid a number field due to chrome bug (OP#32232)
+ # but show the appearance of a decimal field on mobile
+ inputmode: :decimal,
+ placeholder: t(:label_example_placeholder, decimal: unitless_currency_number(1000.50)),
+ class: 'budget-item-value form--text-field',
+ data: { :'request-key' => 'units' } %>
</div>
</div>
- </td>
- <td class="unit_currency budget-table--fields" id="<%= "#{id_prefix}_unit_name" %>">
- <%=h material_budget_item.cost_type.unit_plural if material_budget_item.cost_type %>
- </td>
- <td class="cost_type">
- <label class="hidden-for-sighted" for="<%= id_prefix %>_cost_type_id"><%= MaterialBudgetItem.human_attribute_name(:cost_type) %></label>
- <%= cost_form.select :cost_type_id,
- cost_types_collection_for_select_options(material_budget_item.cost_type),
- {},
- {
- index: id_or_index,
- class: 'form--select budget-item-value',
- data: { :'request-key' => 'cost_type_id' }
- } %>
- </td>
- <td class="comment">
- <label class="hidden-for-sighted" for="<%= id_prefix %>_comments"><%= MaterialBudgetItem.human_attribute_name(:comments) %></label>
- <%= cost_form.text_field :comments, index: id_or_index, size: 40 %>
- </td>
- <% if User.current.allowed_to? :view_cost_rates, @project %>
- <td class="currency budget-table--fields">
- <%# Keep current budget as hidden field because otherwise they will be overridden %>
- <% if templated == false && !material_budget_item.new_record? && material_budget_item.overridden_costs? %>
- <%= cost_form.hidden_field :amount, value: unitless_currency_number(material_budget_item.amount) %>
- <% end %>
+ </td>
+ <td class="unit_currency budget-table--fields" id="<%= "#{id_prefix}_unit_name" %>">
+ <%= h material_budget_item.cost_type.unit_plural if material_budget_item.cost_type %>
+ </td>
+ <td class="cost_type">
+ <label class="hidden-for-sighted" for="<%= id_prefix %>_cost_type_id"><%= MaterialBudgetItem.human_attribute_name(:cost_type) %></label>
+ <%= cost_form.select :cost_type_id,
+ cost_types_collection_for_select_options(material_budget_item.cost_type),
+ {},
+ {
+ index: id_or_index,
+ class: 'form--select budget-item-value',
+ data: { :'request-key' => 'cost_type_id' }
+ } %>
+ </td>
+ <td class="comment">
+ <label class="hidden-for-sighted" for="<%= id_prefix %>_comments"><%= MaterialBudgetItem.human_attribute_name(:comments) %></label>
+ <%= cost_form.text_field :comments, index: id_or_index, size: 40 %>
+ </td>
+ <% if User.current.allowed_to? :view_cost_rates, @project %>
+ <td class="currency budget-table--fields">
+ <%# Keep current budget as hidden field because otherwise they will be overridden %>
+ <% if templated == false && !material_budget_item.new_record? && material_budget_item.overridden_costs? %>
+ <%= cost_form.hidden_field :amount, value: unitless_currency_number(material_budget_item.amount) %>
+ <% end %>
- <% cost_value = material_budget_item.amount || material_budget_item.calculated_costs(@budget.fixed_date) %>
- <%= cost_form.hidden_field :currency, index: id_or_index, value: Setting.plugin_costs['costs_currency'] %>
- <%= cost_form.hidden_field :cost_value, index: id_or_index, value: unitless_currency_number(cost_value) %>
+ <% cost_value = material_budget_item.amount || material_budget_item.calculated_costs(@budget.fixed_date) %>
+ <%= cost_form.hidden_field :currency, index: id_or_index, value: Setting.plugin_costs['costs_currency'] %>
+ <%= cost_form.hidden_field :cost_value, index: id_or_index, value: unitless_currency_number(cost_value) %>
- <cost-unit-subform obj-id="<%= id_prefix %>"
- obj-name="<%= "#{name_prefix}[amount]" %>">
- <a id="<%= id_prefix %>_costs" class="costs--edit-planned-costs-btn icon-context icon-edit" role="button" title="<%= t(:help_click_to_edit) %>">
- <%= number_to_currency(cost_value) %>
- </a>
- </cost-unit-subform>
+ <cost-unit-subform obj-id="<%= id_prefix %>"
+ obj-name="<%= "#{name_prefix}[amount]" %>">
+ <a id="<%= id_prefix %>_costs" class="costs--edit-planned-costs-btn icon-context icon-edit" role="button" title="<%= t(:help_click_to_edit) %>">
+ <%= number_to_currency(cost_value) %>
+ </a>
+ </cost-unit-subform>
+ </td>
+ <% end %>
+ <td class="delete budget-table--fields buttons">
+ <a class="delete-budget-item no-decoration-on-hover" title="<%= t(:button_delete) %>">
+ <%= op_icon('icon-context icon-delete') %>
+ <span class="hidden-for-sighted"><%= t(:button_delete) %></span>
+ </a>
</td>
- <% end %>
- <td class="delete budget-table--fields buttons">
- <a class="delete-budget-item no-decoration-on-hover" title="<%= t(:button_delete) %>">
- <%= op_icon('icon-context icon-delete') %>
- <span class="hidden-for-sighted"><%= t(:button_delete) %></span>
- </a>
- </td>
-</tr>
+ </tr>
<% end %>
diff --git a/modules/budgets/config/locales/en.yml b/modules/budgets/config/locales/en.yml
index 501ef03445a..48cf76b821d 100644
--- a/modules/budgets/config/locales/en.yml
+++ b/modules/budgets/config/locales/en.yml
@@ -78,6 +78,7 @@ en:
label_budget_plural: "Budgets"
label_cost_type_specific: "Budget #%{id}: %{name}"
label_deliverable: "Budget"
+ label_example_placeholder: 'e.g., %{decimal}'
label_view_all_budgets: "View all budgets"
label_yes: "Yes"
diff --git a/modules/budgets/spec/features/budgets/add_budget_spec.rb b/modules/budgets/spec/features/budgets/add_budget_spec.rb
index 8a8a35c09f9..873c1718920 100644
--- a/modules/budgets/spec/features/budgets/add_budget_spec.rb
+++ b/modules/budgets/spec/features/budgets/add_budget_spec.rb
@@ -33,7 +33,7 @@ describe 'adding a new budget', type: :feature, js: true do
let(:user) { FactoryBot.create :admin }
before do
- allow(User).to receive(:current).and_return user
+ login_as user
end
it 'shows link to create a new budget' do
@@ -95,6 +95,7 @@ describe 'adding a new budget', type: :feature, js: true do
end
let(:new_budget_page) { Pages::NewBudget.new project.identifier }
+ let(:budget_page) { Pages::EditBudget.new Budget.last }
before do
project.add_member! user, FactoryBot.create(:role)
@@ -103,6 +104,50 @@ describe 'adding a new budget', type: :feature, js: true do
FactoryBot.create :default_hourly_rate, user: user, rate: 25.0
end
+ context 'with german locale' do
+ let(:user) { FactoryBot.create(:admin, language: :de) }
+
+ it 'creates the budget including the given cost items with german locale' do
+ I18n.locale = :de
+ new_budget_page.visit!
+
+ fill_in 'Thema', with: 'First Aid'
+
+ new_budget_page.add_unit_costs! '3,50', comment: 'RadAway'
+ new_budget_page.add_unit_costs! '1.000,50', comment: 'Rad-X'
+
+ new_budget_page.add_labor_costs! '5000,10', user_name: user.name, comment: 'treatment'
+ new_budget_page.add_labor_costs! '0,5', user_name: user.name, comment: 'attendance'
+
+ click_on 'Anlegen'
+ expect(page).to have_content('Erfolgreich angelegt')
+
+ expect(new_budget_page.unit_costs_at(1)).to have_content '175,00 EUR'
+ expect(new_budget_page.unit_costs_at(2)).to have_content '50.025,00 EUR'
+ expect(new_budget_page.overall_unit_costs).to have_content '50.200,00 EUR'
+
+ expect(new_budget_page.labor_costs_at(1)).to have_content '125.002,50 EUR'
+ expect(new_budget_page.labor_costs_at(2)).to have_content '12,50 EUR'
+ expect(new_budget_page.overall_labor_costs).to have_content '125.015,00 EUR'
+
+ click_on 'Bearbeiten'
+
+ budget_page.expect_planned_costs! type: :material, row: 1, expected: '175,00 EUR'
+ budget_page.expect_planned_costs! type: :material, row: 2, expected: '50.025,00 EUR'
+ budget_page.expect_planned_costs! type: :labor, row: 1, expected: '125.002,50 EUR'
+ budget_page.expect_planned_costs! type: :labor, row: 2, expected: '12,50 EUR'
+
+
+ fields = page
+ .all('input.budget-item-value')
+ .select { |node| node.value.present? }
+ .map(&:value)
+
+ expect(fields).to contain_exactly '3,50', '1.000,50', '5.000,10', '0,50'
+
+ end
+ end
+
it 'creates the budget including the given cost items' do
new_budget_page.visit!
diff --git a/modules/budgets/spec/support/pages/budget_form.rb b/modules/budgets/spec/support/pages/budget_form.rb
index 688b2d5c038..e811ec2befb 100644
--- a/modules/budgets/spec/support/pages/budget_form.rb
+++ b/modules/budgets/spec/support/pages/budget_form.rb
@@ -138,11 +138,11 @@ module Pages
end
def unit_costs_container
- find_container('Planned unit costs')
+ find_container(Budget.human_attribute_name(:material_budget))
end
def labor_costs_container
- find_container('Planned labor costs')
+ find_container(Budget.human_attribute_name(:labor_budget))
end
def find_container(title)
diff --git a/modules/costs/app/views/cost_types/_list.html.erb b/modules/costs/app/views/cost_types/_list.html.erb
index af4eb9ff141..21d18eefcd4 100644
--- a/modules/costs/app/views/cost_types/_list.html.erb
+++ b/modules/costs/app/views/cost_types/_list.html.erb
@@ -89,7 +89,14 @@ See docs/COPYRIGHT.rdoc for more details.
<td>
<%= form_for cost_type, :url => { :controller => '/cost_types', :action => 'set_rate', :id => cost_type }, method: :put, html: { class: 'inline-label' } do |f| %>
<label class="hidden-for-sighted" for="<%= "rate_field_#{cost_type.id}" %>"><%= t(:caption_set_rate) %></label>
- <%= content_tag :input, '', :value => "", :name => :rate, :size => 7, :id => "rate_field_#{cost_type.id}" %>
+ <%= content_tag :input,
+ '',
+ value: "",
+ name: :rate,
+ size: 7,
+ inputmode: :decimal,
+ placeholder: t(:label_example_placeholder, decimal: unitless_currency_number(1000.50)),
+ id: "rate_field_#{cost_type.id}" %>
<span class="form-label">
<%= Setting.plugin_costs['costs_currency'] %>
</span>
diff --git a/modules/costs/app/views/cost_types/_rate.html.erb b/modules/costs/app/views/cost_types/_rate.html.erb
index eca57fa07f4..e9c7bad2cab 100644
--- a/modules/costs/app/views/cost_types/_rate.html.erb
+++ b/modules/costs/app/views/cost_types/_rate.html.erb
@@ -61,6 +61,8 @@ See docs/COPYRIGHT.rdoc for more details.
<%= rate_form.text_field :rate,
size: 7,
index: id_or_index,
+ inputmode: :decimal,
+ placeholder: t(:label_example_placeholder, decimal: unitless_currency_number(1000.50)),
value: rate.rate ? rate.rate.round(2) : "",
required: templated ? false : true %>
<span class="form-label">
diff --git a/modules/costs/spec/features/cost_types/create_cost_type_spec.rb b/modules/costs/spec/features/cost_types/create_cost_type_spec.rb
index d11bc2b1ff1..9f771ddd915 100644
--- a/modules/costs/spec/features/cost_types/create_cost_type_spec.rb
+++ b/modules/costs/spec/features/cost_types/create_cost_type_spec.rb
@@ -46,7 +46,7 @@ describe 'creating a cost type', type: :feature, js: true do
fill_in 'cost_type_name', with: 'Test day rate'
fill_in 'cost_type_unit', with: 'dayUnit'
fill_in 'cost_type_unit_plural', with: 'dayUnitPlural'
- fill_in 'cost_type_new_rate_attributes_0_rate', with: '5'
+ fill_in 'cost_type_new_rate_attributes_0_rate', with: '1,000.25'
sleep 1
@@ -60,6 +60,43 @@ describe 'creating a cost type', type: :feature, js: true do
expect(cost_type_row).to have_selector('td a', text: 'Test day rate')
expect(cost_type_row).to have_selector('td', text: 'dayUnit')
expect(cost_type_row).to have_selector('td', text: 'dayUnitPlural')
- expect(cost_type_row).to have_selector('td.currency', text: '5')
+ expect(cost_type_row).to have_selector('td.currency', text: '1,000.25')
+
+ cost_type = CostType.last
+ expect(cost_type.name).to eq 'Test day rate'
+ cost_rate = cost_type.rates.last
+ expect(cost_rate.rate).to eq 1000.25
+ end
+
+ context 'with german locale' do
+ let(:user) { FactoryBot.create(:admin, language: :de) }
+
+ it 'creates the entry with german number separators' do
+ visit "/cost_types/new"
+
+ fill_in 'cost_type_name', with: 'Test day rate'
+ fill_in 'cost_type_unit', with: 'dayUnit'
+ fill_in 'cost_type_unit_plural', with: 'dayUnitPlural'
+ fill_in 'cost_type_new_rate_attributes_0_rate', with: '1.000,25'
+
+ sleep 1
+
+ scroll_to_and_click(find('button.-with-icon.icon-checkmark'))
+
+ expect_angular_frontend_initialized
+ expect(page).to have_selector '.generic-table', wait: 10
+
+ cost_type_row = find('tr', text: 'Test day rate')
+
+ expect(cost_type_row).to have_selector('td a', text: 'Test day rate')
+ expect(cost_type_row).to have_selector('td', text: 'dayUnit')
+ expect(cost_type_row).to have_selector('td', text: 'dayUnitPlural')
+ expect(cost_type_row).to have_selector('td.currency', text: '1.000,25')
+
+ cost_type = CostType.last
+ expect(cost_type.name).to eq 'Test day rate'
+ cost_rate = cost_type.rates.last
+ expect(cost_rate.rate).to eq 1000.25
+ end
end
end