1
0

Changed Unit.type -> .group

Added Unit validations. Added default Units.
This commit is contained in:
cryptogopher 2019-08-16 19:29:23 +02:00
parent 8082e42225
commit fea736bd36
5 changed files with 26 additions and 7 deletions

View File

@ -1,6 +1,6 @@
module UnitsHelper module UnitsHelper
def type_options def group_options
translations = t('.types') translations = t('.groups')
Unit.types.map { |k,v| [translations[k.to_sym], k] } Unit.groups.map { |k,v| [translations[k.to_sym], k] }
end end
end end

View File

@ -1,6 +1,8 @@
class Unit < ActiveRecord::Base class Unit < ActiveRecord::Base
belongs_to :project
# https://en.wikipedia.org/wiki/International_System_of_Units # https://en.wikipedia.org/wiki/International_System_of_Units
enum type: { enum group: {
number: 0, number: 0,
share: 1, share: 1,
@ -21,4 +23,8 @@ class Unit < ActiveRecord::Base
pressure: 40 pressure: 40
} }
validates :project, associated: true
validates :name, :shortname, presence: true
validates :group, inclusion: groups.keys
end end

View File

@ -6,7 +6,7 @@
<p><%= f.text_field :shortname, required: true, size: 10 %></p> <p><%= f.text_field :shortname, required: true, size: 10 %></p>
</div> </div>
<div class="splitcontentright"> <div class="splitcontentright">
<p><%= f.select :type, type_options, required: true %></p> <p><%= f.select :group, group_options, required: true %></p>
</div> </div>
</div> </div>
<p><%= f.text_field :name, required: true, size: 40 %></p> <p><%= f.text_field :name, required: true, size: 40 %></p>

View File

@ -15,7 +15,7 @@ en:
heading: 'Units' heading: 'Units'
heading_new_unit: 'New unit' heading_new_unit: 'New unit'
form: form:
types: groups:
number: 'count' number: 'count'
share: 'share' share: 'share'
length: 'length' length: 'length'

View File

@ -4,7 +4,20 @@ class CreateUnits < ActiveRecord::Migration
t.references :project t.references :project
t.string :name t.string :name
t.string :shortname t.string :shortname
t.integer :type t.integer :group
end
reversible do |dir|
dir.up do
Unit.create project: nil, shortname: "", name: "count", group: :number
Unit.create project: nil, shortname: "%", name: "percent", group: :share
Unit.create project: nil, shortname: "g", name: "gram", group: :mass
Unit.create project: nil, shortname: "kg", name: "kilogram", group: :mass
end
dir.down do
Unit.delete_all
end
end end
end end
end end