module Cardio::Seed

methods in support of seeding

Constants

TABLES

Public Class Methods

clean() click to toggle source
# File lib/cardio/seed.rb, line 40
def clean
  Card::Act.update_all actor_id: author_id
  clean_history
  clean_time_and_user_stamps
end
default_path() click to toggle source
# File lib/cardio/seed.rb, line 8
def default_path
  db_path Cardio.config.seed_type, 0
end
dump() click to toggle source
# File lib/cardio/seed.rb, line 29
def dump
  dump_tables.each do |table|
    i = "000" # TODO: use card keys instead (this is just a label)
    write_seed_file table do
      yamlize_records table do |record, hash|
        hash["#{table}_#{i.succ!}"] = record
      end
    end
  end
end
load() click to toggle source
# File lib/cardio/seed.rb, line 20
def load
  ActiveRecord::FixtureSet.create_fixtures path, load_tables

  return unless update_seed?

  Cardio::Migration::Schema.new.assume_current
  Cardio::Migration::Transform.new.assume_current
end
path() click to toggle source
# File lib/cardio/seed.rb, line 12
def path
  if update_seed?
    db_path :real, (Rails.env.test? ? 0 : 1)
  else
    default_path
  end
end

Private Class Methods

author_id() click to toggle source
# File lib/cardio/seed.rb, line 106
def author_id
  Card::WagnBotID
end
clean_card_actions_record(record) click to toggle source
# File lib/cardio/seed.rb, line 80
def clean_card_actions_record record
  record["draft"] = false # needed?
end
clean_card_record(record) click to toggle source
# File lib/cardio/seed.rb, line 84
def clean_card_record record
  record["trash"] = false # needed?
  %w[created_at updated_at current_revision_id references_expired].each do |key|
    record.delete key
  end
end
clean_history() click to toggle source
# File lib/cardio/seed.rb, line 91
def clean_history
  puts "clean history"
  act = Card::Act.create! actor_id: author_id, card_id: author_id
  Card::Action.make_current_state_the_initial_state act
  Card::Act.where("id <> #{act.id}").delete_all
  ActiveRecord::Base.connection.execute("truncate sessions")
end
clean_time_and_user_stamps() click to toggle source
# File lib/cardio/seed.rb, line 99
def clean_time_and_user_stamps
  puts "clean time and user stamps"
  conn = ActiveRecord::Base.connection
  conn.update "UPDATE cards SET creator_id=#{author_id}, updater_id=#{author_id}"
  conn.update "UPDATE card_acts SET actor_id=#{author_id}"
end
db_path(env, index) click to toggle source
# File lib/cardio/seed.rb, line 61
def db_path env, index
  Mod.fetch(Cardio.config.seed_mods[index]).subpath "data", "fixtures", env.to_s
end
dump_tables() click to toggle source
# File lib/cardio/seed.rb, line 57
def dump_tables
  TABLES + Cardio.config.extra_seed_tables
end
load_tables() click to toggle source

TODO: make this more robust. only handles simple case of extra seed tables

# File lib/cardio/seed.rb, line 53
def load_tables
  update_seed? && !Rails.env.test? ? TABLES : dump_tables
end
update_seed?() click to toggle source
# File lib/cardio/seed.rb, line 48
def update_seed?
  ENV["CARD_UPDATE_SEED"]
end
write_seed_file(table) { || ... } click to toggle source
# File lib/cardio/seed.rb, line 65
def write_seed_file table
  filename = File.join default_path, "#{table}.yml"
  File.open(filename, "w") { |file| file.write yield }
end
yamlize_records(table) { |record, hash| ... } click to toggle source
# File lib/cardio/seed.rb, line 70
def yamlize_records table
  data = ActiveRecord::Base.connection.select_all "select * from #{table}"
  YAML.dump(
    data.each_with_object({}) do |record, hash|
      try "clean_#{table}_record", record
      yield record, hash
    end
  )
end