Rake task to export default settings as seeds

This commit is contained in:
2024-12-06 01:17:05 +01:00
parent b38d72e9b0
commit 2e4eb3d4b5
7 changed files with 82 additions and 16 deletions

View 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
View 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