forked from fixin.me/fixin.me
Change Unit.multiplier type from decimal to float
Remove remnants related to BigDecimal.
This commit is contained in:
@@ -17,7 +17,7 @@ class UnitsController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
@unit = current_user.units.new(unit_params)
|
||||
@unit = current_user.units.new(params.expect(Unit::ATTRIBUTES))
|
||||
if @unit.save
|
||||
@before = @unit.successive
|
||||
flash.now[:notice] = t('.success', unit: @unit)
|
||||
@@ -30,7 +30,7 @@ class UnitsController < ApplicationController
|
||||
end
|
||||
|
||||
def update
|
||||
if @unit.update(unit_params.except(:base_id))
|
||||
if @unit.update(params.except(:base_id).expect(Unit::ATTRIBUTES))
|
||||
flash.now[:notice] = t('.success', unit: @unit)
|
||||
else
|
||||
render :edit
|
||||
@@ -40,11 +40,11 @@ class UnitsController < ApplicationController
|
||||
# TODO: Avoid double table width change by first un-hiding table header,
|
||||
# then displaying index, e.g. by re-displaying header in index
|
||||
def rebase
|
||||
permitted = params.require(:unit).permit(:base_id)
|
||||
permitted.merge!(multiplier: 1) if permitted[:base_id].blank? && @unit.multiplier != 1
|
||||
unit_params = params.expect(unit: :base_id)
|
||||
unit_params.merge!(multiplier: 1.0) if unit_params[:base_id].blank?
|
||||
|
||||
@previous_base = @unit.base
|
||||
@unit.update!(permitted)
|
||||
@unit.update!(unit_params)
|
||||
|
||||
@before = @unit.successive
|
||||
if @unit.multiplier_previously_changed?
|
||||
@@ -59,10 +59,6 @@ class UnitsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def unit_params
|
||||
params.require(:unit).permit(Unit::ATTRIBUTES)
|
||||
end
|
||||
|
||||
def find_unit
|
||||
@unit = current_user.units.find_by!(id: params[:id])
|
||||
end
|
||||
|
||||
@@ -103,15 +103,19 @@ module ApplicationHelper
|
||||
def number_field(method, options = {})
|
||||
attr_type = object.type_for_attribute(method)
|
||||
case attr_type.type
|
||||
when :decimal
|
||||
when :float, :double
|
||||
options[:value] = object.public_send(method)&.to_scientific
|
||||
options[:step] ||= BigDecimal(10).power(-attr_type.scale)
|
||||
options[:max] ||= BigDecimal(10).power(attr_type.precision - attr_type.scale) -
|
||||
options[:step]
|
||||
options[:min] = options[:min] == :step ? options[:step] : options[:min]
|
||||
options[:min] ||= -options[:max]
|
||||
options[:size] ||= attr_type.precision / 2
|
||||
when :float
|
||||
options[:step] ||= :any
|
||||
options[:min] ||= Float::MIN_15
|
||||
options[:max] ||= Float::MAX_15
|
||||
# Longest possible number (written not using exponent):
|
||||
# sign (1), leading 0 (1), dot (1), exponent 0s (307), digits (15).
|
||||
# This is only upper bound, which cannot guarantee the number won't fall
|
||||
# out of range.
|
||||
# TODO: add `[pattern]` to limit precision and (possibly) replace `[maxlength]`?
|
||||
# NOTE: `[pattern]` is unavailable on `input[type=number]` and `[min]/[max]` is
|
||||
# unavailable on `input[type=text]`.
|
||||
options[:maxlength] ||= 3 + Float::MIN_10_EXP + Float::DIG
|
||||
options[:size] ||= 6
|
||||
end
|
||||
super
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Unit < ApplicationRecord
|
||||
ATTRIBUTES = [:symbol, :description, :multiplier, :base_id]
|
||||
ATTRIBUTES = {unit: [:symbol, :description, :multiplier, :base_id]}
|
||||
|
||||
belongs_to :user, optional: true
|
||||
belongs_to :base, optional: true, class_name: "Unit"
|
||||
@@ -14,8 +14,8 @@ class Unit < ApplicationRecord
|
||||
validates :symbol, presence: true, uniqueness: {scope: :user_id},
|
||||
length: {maximum: type_for_attribute(:symbol).limit}
|
||||
validates :description, length: {maximum: type_for_attribute(:description).limit}
|
||||
validates :multiplier, numericality: {equal_to: 1}, unless: :base
|
||||
validates :multiplier, numericality: {greater_than: 0, precision: true, scale: true}, if: :base
|
||||
validates :multiplier, numericality: {equal_to: 1.0}, unless: :base
|
||||
validates :multiplier, numericality: {greater_than: 0.0}, if: :base
|
||||
|
||||
scope :defaults, ->{ where(user: nil) }
|
||||
scope :defaults_diff, ->{
|
||||
@@ -102,10 +102,10 @@ class Unit < ApplicationRecord
|
||||
user_id.nil?
|
||||
end
|
||||
|
||||
# Should only by invoked on Units returned from #defaults_diff which are #portable
|
||||
# Should only by invoked on Units returned from #defaults_diff which are #portable.
|
||||
def port!(recipient)
|
||||
recipient_base = base && Unit.find_by!(symbol: base.symbol, user: recipient)
|
||||
params = slice(ATTRIBUTES - [:symbol, :base_id])
|
||||
params = slice(ATTRIBUTES[:unit] - [:symbol, :base_id])
|
||||
Unit.find_or_initialize_by(user: recipient, symbol: symbol)
|
||||
.update!(base: recipient_base, **params)
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<%= form.text_area :description, cols: 30, rows: 1, escape: false %>
|
||||
</td>
|
||||
<td>
|
||||
<%= form.number_field :multiplier, required: true, size: 10, min: :step if @unit.base_id? %>
|
||||
<%= form.number_field :multiplier, required: true if @unit.base_id? %>
|
||||
</td>
|
||||
|
||||
<td class="flex">
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
data: {turbo_stream: true} %>
|
||||
</td>
|
||||
<td><%= unit.description %></td>
|
||||
<td class="ralign"><%= unit.multiplier.to_html %></td>
|
||||
<td class="ralign"><%= unit.multiplier.to_html if unit.base_id? %></td>
|
||||
|
||||
<% if current_user.at_least(:active) %>
|
||||
<td class="flex">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<%= turbo_stream.remove @unit %>
|
||||
<%= turbo_stream.replace @previous_base if @previous_base %>
|
||||
<%= turbo_stream.replace @unit.base if @unit.base_id? && (@previous_base.id != @unit.base_id) %>
|
||||
<%= turbo_stream.replace @unit.base if @unit.base_id? && (@previous_base&.id != @unit.base_id) %>
|
||||
<%= @before ? turbo_stream.before(@before, @unit) : turbo_stream.append(:units, @unit) %>
|
||||
|
||||
Reference in New Issue
Block a user