class Ghost::Seeder::Models::GenericSeed

Attributes

attributes[R]
record[R]

Public Class Methods

klass() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 39
def klass; raise NotImplementedError; end
load_fixtures(identifier) click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 21
def load_fixtures(identifier)
  YAML.load(
    ERB.new(
      File.read(
        "./config/seed/fixtures/#{identifier}.yml"
      )
    ).result(yaml_bindings.helper)
  )
end
new(attributes) click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 44
def initialize(attributes)
  @attributes = attributes
  # allow calling hash attributes as methods
  @record = OpenStruct.new(attributes)
end
perform_queries() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 10
def perform_queries
  seeds.each do |record|
    new(record).seed!
  end
end
seeds() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 31
def seeds
  load_fixtures klass.to_s.downcase.pluralize
end
wipe_records!() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 16
def wipe_records!
  Logger.warn "wiping", klass
  klass.destroy_all
end
yaml_bindings() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 35
def yaml_bindings
  YamlBindings.new
end

Public Instance Methods

seed!() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 50
def seed!
  if record_exists?
    Logger.info "#{self.class} exists:", record_slug
    raise "#{record_slug} not expected to exist!" if Runner.wipe_db?
  else
    create_record!
  end
end

Private Instance Methods

create_record!() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 78
def create_record!
  self.class.klass.create(attributes_for_create)
  Logger.success self.class, record_slug
end
first_user() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 66
def first_user
  @first_user ||= User.first
end
record_exists?() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 70
def record_exists?
  self.class.klass.where(slug: record.slug).exists?
end
record_slug() click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 74
def record_slug
  record.slug
end
yaml_bindings_helper(method) click to toggle source
# File lib/ghost/seeder/models/generic_seed.rb, line 61
def yaml_bindings_helper(method)
  @yaml_bindings ||= self.class.yaml_bindings
  @yaml_bindings.send(method)
end