forked from fixin.me/fixin.me
Rake task to export default settings as seeds
This commit is contained in:
parent
b38d72e9b0
commit
2e4eb3d4b5
@ -118,6 +118,15 @@ Tests need to be run from within toplevel application directory:
|
||||
|
||||
bundle exec rails test test/system/users_test.rb --seed 1234
|
||||
|
||||
|
||||
### Icons
|
||||
|
||||
Pictogrammers Material Design Icons: https://pictogrammers.com/library/mdi/
|
||||
|
||||
|
||||
### Rake tasks
|
||||
|
||||
Exporting default settings defined in application to seed file (e.g. to send as
|
||||
PR or share between installations):
|
||||
|
||||
bundle exec rails db:seed:export
|
||||
|
1
config/initializers/core_ext.rb
Normal file
1
config/initializers/core_ext.rb
Normal file
@ -0,0 +1 @@
|
||||
require 'core_ext/big_decimal/formatting'
|
17
db/seeds.rb
17
db/seeds.rb
@ -20,19 +20,4 @@ end
|
||||
# Formulas will be deleted as dependent on Quantities
|
||||
#[Source, Quantity, Unit].each { |model| model.defaults.delete_all }
|
||||
|
||||
Unit.transaction do
|
||||
Unit.defaults.delete_all
|
||||
|
||||
unit_1 = Unit.create symbol: "1", name: "dimensionless, one"
|
||||
Unit.create symbol: "%", base: unit_1, multiplier: 1e-2, name: "percent"
|
||||
Unit.create symbol: "‰", base: unit_1, multiplier: 1e-3, name: "promille"
|
||||
Unit.create symbol: "‱", base: unit_1, multiplier: 1e-4, name: "basis point"
|
||||
Unit.create symbol: "ppm", base: unit_1, multiplier: 1e-6, name: "parts per million"
|
||||
|
||||
unit_g = Unit.create symbol: "g", name: "gram"
|
||||
Unit.create symbol: "ug", base: unit_g, multiplier: 1e-6, name: "microgram"
|
||||
Unit.create symbol: "mg", base: unit_g, multiplier: 1e-3, name: "milligram"
|
||||
Unit.create symbol: "kg", base: unit_g, multiplier: 1e3, name: "kilogram"
|
||||
|
||||
Unit.create symbol: "kcal", name: "kilocalorie"
|
||||
end
|
||||
require 'seeds/units.rb'
|
||||
|
9
db/seeds/templates/units.erb
Normal file
9
db/seeds/templates/units.erb
Normal file
@ -0,0 +1,9 @@
|
||||
Unit.transaction do
|
||||
Unit.defaults.delete_all
|
||||
<% Unit.defaults.ordered.each do |unit| %>
|
||||
<%= "\n" if unit.base.nil? %>
|
||||
unit_<%= unit.symbol %> =
|
||||
Unit.create symbol: "<%= unit.symbol %>",<% unless unit.base.nil? %> base: unit_<%= unit.base.symbol %>, multiplier: <%= unit.multiplier.to_scientific %>,<% end %>
|
||||
description: "<%= unit.description %>"
|
||||
<% end %>
|
||||
end
|
36
db/seeds/units.rb
Normal file
36
db/seeds/units.rb
Normal file
@ -0,0 +1,36 @@
|
||||
Unit.transaction do
|
||||
Unit.defaults.delete_all
|
||||
|
||||
unit_1 =
|
||||
Unit.create symbol: "1",
|
||||
description: "dimensionless, one"
|
||||
unit_ppm =
|
||||
Unit.create symbol: "ppm", base: unit_1, multiplier: 1e-6,
|
||||
description: "parts per million"
|
||||
unit_‱ =
|
||||
Unit.create symbol: "‱", base: unit_1, multiplier: 1e-4,
|
||||
description: "basis point"
|
||||
unit_‰ =
|
||||
Unit.create symbol: "‰", base: unit_1, multiplier: 1e-3,
|
||||
description: "promille"
|
||||
unit_% =
|
||||
Unit.create symbol: "%", base: unit_1, multiplier: 1e-2,
|
||||
description: "percent"
|
||||
|
||||
unit_g =
|
||||
Unit.create symbol: "g",
|
||||
description: "gram"
|
||||
unit_ug =
|
||||
Unit.create symbol: "ug", base: unit_g, multiplier: 1e-6,
|
||||
description: "microgram"
|
||||
unit_mg =
|
||||
Unit.create symbol: "mg", base: unit_g, multiplier: 1e-3,
|
||||
description: "milligram"
|
||||
unit_kg =
|
||||
Unit.create symbol: "kg", base: unit_g, multiplier: 1e3,
|
||||
description: "kilogram"
|
||||
|
||||
unit_kcal =
|
||||
Unit.create symbol: "kcal",
|
||||
description: "kilocalorie"
|
||||
end
|
14
lib/core_ext/big_decimal/formatting.rb
Normal file
14
lib/core_ext/big_decimal/formatting.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module FixinMe
|
||||
module BigDecimalScientificNotation
|
||||
def to_scientific
|
||||
return 'NaN' unless finite?
|
||||
|
||||
sign, coefficient, base, exponent = split
|
||||
(sign == -1 ? '-' : '') +
|
||||
(coefficient.length > 1 ? coefficient.insert(1, '.') : coefficient) +
|
||||
(exponent != 1 ? "e#{exponent-1}" : '')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
BigDecimal.prepend(FixinMe::BigDecimalScientificNotation)
|
12
lib/tasks/db.rake
Normal file
12
lib/tasks/db.rake
Normal file
@ -0,0 +1,12 @@
|
||||
namespace :db do
|
||||
namespace :seed do
|
||||
desc "Dump default settings as seed data to db/seeds/*.rb"
|
||||
task export: :environment do
|
||||
seeds_path = Pathname.new(Rails.application.paths["db"].first) / 'seeds'
|
||||
(seeds_path / 'templates').glob('*.erb').each do |template_path|
|
||||
template = ERB.new(template_path.read, trim_mode: '<>')
|
||||
(seeds_path / "#{template_path.basename('.*').to_s}.rb").write(template.result)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user