lib/seed_dump/perform.rb in seed_dump-0.4.0 vs lib/seed_dump/perform.rb in seed_dump-0.4.1
- old
+ new
@@ -1,27 +1,30 @@
require "true"
+require "clip"
module SeedDump
class Perform
def initialize
@opts = {}
@ar_options = {}
@indent = ""
@models = []
+ @last_record = []
@seed_rb = ""
@id_set_string = ""
- @verbose = true
@model_dir = 'app/models/**/*.rb'
end
def setup(env)
# config
+ @opts['verbose'] = env["VERBOSE"].true? || env['VERBOSE'].nil?
@opts['debug'] = env["DEBUG"].true?
@opts['with_id'] = env["WITH_ID"].true?
+ @opts['timestamps'] = env["TIMESTAMPS"].true? || env["TIMESTAMPS"].nil?
@opts['no-data'] = env['NO_DATA'].true?
- @opts['without_protection'] = env['WITHOUT_PROTECTION'].true?
+ @opts['without_protection'] = env['WITHOUT_PROTECTION'].true? || (env['WITHOUT_PROTECTION'].nil? && @opts['timestamps'])
@opts['skip_callbacks'] = env['SKIP_CALLBACKS'].true?
@opts['models'] = env['MODELS'] || (env['MODEL'] ? env['MODEL'] : "")
@opts['file'] = env['FILE'] || "#{Rails.root}/db/seeds.rb"
@opts['append'] = (env['APPEND'].true? && File.exists?(@opts['file']) )
@ar_options = env['LIMIT'].to_i > 0 ? { :limit => env['LIMIT'].to_i } : {}
@@ -31,50 +34,66 @@
@opts['model_dir'] = env['MODEL_DIR'] || @model_dir
end
def loadModels
puts "Searching in #{@opts['model_dir']} for models" if @opts['debug']
- Dir[@opts['model_dir']].sort.each do |f|
+ Dir[Dir.pwd + '/' + @opts['model_dir']].sort.each do |f|
puts "Processing file #{f}" if @opts['debug']
# parse file name and path leading up to file name and assume the path is a module
f =~ /models\/(.*).rb/
# split path by /, camelize the constituents, and then reform as a formal class name
- model = $1.split("/").map {|x| x.camelize}.join("::")
+ parts = $1.split("/").map {|x| x.camelize}
+ # Initialize nested model namespaces
+ parts.clip.inject(Object) { |x, y| x.const_set(y, Module.new) }
+ model = parts.join("::")
+ require f
+
puts "Detected model #{model}" if @opts['debug']
@models.push model if @opts['models'].include?(model) || @opts['models'].empty?
end
end
def models
@models
end
+ def last_record
+ @last_record
+ end
+
def dumpAttribute(a_s,r,k,v)
if v.is_a?(BigDecimal)
v = v.to_s
else
v = attribute_for_inspect(r,k)
end
unless k == 'id' && !@opts['with_id']
- @opts['without_protection'] = true if ["id", "created_at", "updated_at"].include?(k)
- a_s.push("#{k.to_sym.inspect} => #{v}")
+ if (!(k == 'created_at' || k == 'updated_at') || @opts['timestamps'])
+ a_s.push("#{k.to_sym.inspect} => #{v}")
+ end
end
end
def dumpModel(model)
@id_set_string = ''
+ @last_record = []
create_hash = ""
options = ''
rows = []
arr = []
arr = model.find(:all, @ar_options) unless @opts['no-data']
arr = arr.empty? ? [model.new] : arr
arr.each_with_index { |r,i|
attr_s = [];
- r.attributes.each { |k,v| dumpAttribute(attr_s,r,k,v) }
+ r.attributes.each do |k,v|
+ if ((model.attr_accessible[:default].include? k) || @opts['without_protection'] || @opts['with_id'])
+ dumpAttribute(attr_s,r,k,v)
+ @last_record.push k
+ end
+ end
rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
}
if @opts['without_protection']
options = ', :without_protection => true '
@@ -86,23 +105,22 @@
def dumpModels
@seed_rb = ""
@models.sort.each do |model|
m = model.constantize
if m.ancestors.include?(ActiveRecord::Base)
- puts "Adding #{model} seeds." if @verbose
+ puts "Adding #{model} seeds." if @opts['verbose']
if @opts['skip_callbacks']
@seed_rb << "#{model}.reset_callbacks :save\n"
@seed_rb << "#{model}.reset_callbacks :create\n"
- puts "Callbacks are disabled." if @verbose
+ puts "Callbacks are disabled." if @opts['verbose']
end
@seed_rb << dumpModel(m) << "\n\n"
else
- puts "Skipping non-ActiveRecord model #{model}..." if @verbose
+ puts "Skipping non-ActiveRecord model #{model}..." if @opts['verbose']
end
- puts "Protection is disabled." if @verbose && @opts['without_protection']
end
end
def writeFile
File.open(@opts['file'], (@opts['append'] ? "a" : "w")) { |f|
@@ -131,9 +149,11 @@
end
def run(env)
setup env
+
+ puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection']
setSearchPath @opts['schema'] if @opts['schema']
loadModels