Sha256: 0aac979f03db0f31e6496590a6669609069d070c522a4b2b1e9bba9877cdd437
Contents?: true
Size: 1.4 KB
Versions: 3
Compression:
Stored size: 1.4 KB
Contents
# Populates db with units of measure. class PopulateOkeiUnits < ActiveRecord::Migration # reloads units and their uuids from db/seed/units.json def up return if Rails.env.test? down populate_units_from seed end # deletes all units with their uuids. def down return if Rails.env.test? clear_uuids clear_units end private # Parses JSON from db/seed/units.json def seed @seed ||= begin root = File.dirname File.dirname(Okei::Engine.called_from) file = File.join(root, "db", "seeds", "units.json") JSON.parse(File.read file) end end # Deletes units' uuids. def clear_uuids list = Uuids::Uuid.where(record_type: "Okei::Unit") say_with_time "Removing #{ list.count } uuids" do list.delete_all left = list.count fail "#{ left } uuids left undeleted" if left > 0 end end # Deletes units objects. def clear_units list = Okei::Unit.all say_with_time "Removing #{ list.count } units" do list.delete_all left = list.count fail "#{ left } units left undeleted" if left > 0 end end # Populates units and their uuids from given JSON def populate_units_from(json) say_with_time "Adding #{ json.count } units" do json.each do |item| unit = Okei::Unit.new unit.uuids.new value: item.delete("uuid") unit.attributes = item unit.save! end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
okei-1.0.1 | db/migrate/20141019072901_populate_okei_units.rb |
okei-1.0.0 | db/migrate/20141019072901_populate_okei_units.rb |
okei-1.0.0.pre.rc | db/migrate/20141019072901_populate_okei_units.rb |