class SeedScatter::Harvester
Public Class Methods
source_root(path=nil)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 49 def self.source_root(path=nil) File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators', 'templates')) end
Public Instance Methods
class_name()
click to toggle source
# File lib/seed_scatter/harvest.rb, line 53 def class_name @model.name end
collect_data(model)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 38 def collect_data(model) model.all.each do |m| @harvest_data << ActiveSupport::JSON.decode(m.attributes.to_json, symbolize_names: true) end end
destination_path(model)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 57 def destination_path(model) "db/seeds/#{model.table_name}.rb" end
dump_model(model)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 44 def dump_model(model) collect_data(model) template "seed.erb", destination_path(model), force: true end
init(model)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 7 def init(model) models = if model == :all Dir['app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize } else [model.camelize.constantize] end models.each do |m| if m < ActiveRecord::Base @model = m puts "Harvesting seeds for #{class_name}" @harvest_data = [] dump_model(m) end end end
reseed(model)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 23 def reseed(model) @model = m = model.camelize.constantize truncate(m.table_name) f = File.expand_path(File.join("db","seeds","#{m.table_name}.rb")) puts "Reseeding #{f}" load(f) if ActiveRecord::Base.connection.adapter_name.eql?('PostgreSQL') ActiveRecord::Base.connection.reset_pk_sequence!(m.table_name) end end
truncate(table)
click to toggle source
# File lib/seed_scatter/harvest.rb, line 34 def truncate(table) ActiveRecord::Base.connection.execute("TRUNCATE #{table}") end