forked from fixin.me/fixin.me
Validate Unit string lengths
This commit is contained in:
parent
d28ef26dc5
commit
98c8fb9346
@ -4,14 +4,17 @@ class Unit < ApplicationRecord
|
|||||||
belongs_to :user, optional: true
|
belongs_to :user, optional: true
|
||||||
belongs_to :base, optional: true, class_name: "Unit"
|
belongs_to :base, optional: true, class_name: "Unit"
|
||||||
|
|
||||||
validates :symbol, presence: true, uniqueness: {scope: :user_id}
|
validates :symbol, presence: true, uniqueness: {scope: :user_id},
|
||||||
|
length: {maximum: columns_hash['symbol'].limit}
|
||||||
|
validates :name, length: {maximum: columns_hash['name'].limit}
|
||||||
validates :multiplier, numericality: {equal_to: 1}, unless: :base
|
validates :multiplier, numericality: {equal_to: 1}, unless: :base
|
||||||
validates :multiplier, numericality: {other_than: 1}, if: :base
|
validates :multiplier, numericality: {other_than: 1}, if: :base
|
||||||
validate if: -> { base.present? } do
|
validate if: -> { base.present? } do
|
||||||
errors.add(:base, :only_top_level_base_units) unless base.base.nil?
|
errors.add(:base, :only_top_level_base_units) unless base.base.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
acts_as_nested_set parent_column: :base_id, scope: :user, dependent: :destroy, order_column: :multiplier
|
acts_as_nested_set parent_column: :base_id, scope: :user, dependent: :destroy,
|
||||||
|
order_column: :multiplier
|
||||||
|
|
||||||
scope :defaults, -> { where(user: nil) }
|
scope :defaults, -> { where(user: nil) }
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
<%= fields_for unit do |form| %>
|
<%= fields_for unit do |form| %>
|
||||||
<td>
|
<td>
|
||||||
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 10,
|
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 10,
|
||||||
autocomplete: "off" %>
|
maxlength: unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= form.text_field :name, form: :unit_form, size: 25, autocomplete: "off" %>
|
<%= form.text_field :name, form: :unit_form, size: 25,
|
||||||
|
maxlength: unit.class.columns_hash['name'].limit, autocomplete: "off" %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<% unless unit.base.nil? %>
|
<% unless unit.base.nil? %>
|
||||||
|
@ -18,8 +18,8 @@ class UnitsTest < ApplicationSystemTestCase
|
|||||||
|
|
||||||
within first('tbody > tr') do
|
within first('tbody > tr') do
|
||||||
assert_selector ':focus'
|
assert_selector ':focus'
|
||||||
fill_in 'unit[symbol]', with: SecureRandom.random_symbol(rand(1..10))
|
fill_in 'unit[symbol]', with: SecureRandom.random_symbol(rand(1..16))
|
||||||
fill_in 'unit[name]', with: [nil, SecureRandom.alphanumeric(rand(1..500))].sample
|
fill_in 'unit[name]', with: [nil, SecureRandom.alphanumeric(rand(1..255))].sample
|
||||||
assert_difference ->{ Unit.count }, 1 do
|
assert_difference ->{ Unit.count }, 1 do
|
||||||
click_on t(:add)
|
click_on t(:add)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user