forked from fixin.me/fixin.me
Set multiplier to 1 for base units
This commit is contained in:
parent
f298acd726
commit
aa4a8193ce
@ -1,4 +1,6 @@
|
|||||||
class Unit < ApplicationRecord
|
class Unit < ApplicationRecord
|
||||||
|
attribute :multiplier, default: 1
|
||||||
|
|
||||||
belongs_to :user, optional: true
|
belongs_to :user, optional: true
|
||||||
# TODO: validate base.user == user
|
# TODO: validate base.user == user
|
||||||
belongs_to :base, optional: true, class_name: "Unit"
|
belongs_to :base, optional: true, class_name: "Unit"
|
||||||
@ -6,8 +8,8 @@ class Unit < ApplicationRecord
|
|||||||
validates :symbol, presence: true, uniqueness: {scope: :user_id},
|
validates :symbol, presence: true, uniqueness: {scope: :user_id},
|
||||||
length: {maximum: columns_hash['symbol'].limit}
|
length: {maximum: columns_hash['symbol'].limit}
|
||||||
validates :name, length: {maximum: columns_hash['name'].limit}
|
validates :name, length: {maximum: columns_hash['name'].limit}
|
||||||
validates :multiplier, absence: true, unless: :base
|
validates :multiplier, numericality: {equal_to: 1}, unless: :base
|
||||||
validates :multiplier, presence: true, numericality: {other_than: 0}, if: :base
|
validates :multiplier, numericality: {other_than: 0}, if: :base
|
||||||
validate if: ->{ base&.base.present? } do
|
validate if: ->{ base&.base.present? } do
|
||||||
errors.add(:base, :multilevel_nesting)
|
errors.add(:base, :multilevel_nesting)
|
||||||
end
|
end
|
||||||
@ -16,9 +18,9 @@ class Unit < ApplicationRecord
|
|||||||
scope :ordered, ->{
|
scope :ordered, ->{
|
||||||
parent_symbol = Arel::Nodes::NamedFunction.new(
|
parent_symbol = Arel::Nodes::NamedFunction.new(
|
||||||
'COALESCE',
|
'COALESCE',
|
||||||
[Arel::Table.new(:bases_units)[:symbol], Unit.arel_table[:symbol]]
|
[Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]]
|
||||||
)
|
)
|
||||||
left_outer_joins(:base).order(parent_symbol, :multiplier)
|
left_outer_joins(:base).order(parent_symbol, arel_table[:base_id].asc.nulls_first, :multiplier)
|
||||||
}
|
}
|
||||||
|
|
||||||
before_destroy do
|
before_destroy do
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
onclick: 'this.blur();', data: {turbo_stream: true} %>
|
onclick: 'this.blur();', data: {turbo_stream: true} %>
|
||||||
</td>
|
</td>
|
||||||
<td><%= unit.name %></td>
|
<td><%= unit.name %></td>
|
||||||
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>
|
<td class="number"><%= scientifize(unit.multiplier) if unit.base %></td>
|
||||||
|
|
||||||
<% if current_user.at_least(:active) %>
|
<% if current_user.at_least(:active) %>
|
||||||
<td class="actions">
|
<td class="actions">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user