lib/seed_dump/perform.rb in seed_dump-0.3.4 vs lib/seed_dump/perform.rb in seed_dump-0.4.0

- old
+ new

@@ -1,5 +1,7 @@ +require "true" + module SeedDump class Perform def initialize @opts = {} @@ -7,79 +9,106 @@ @indent = "" @models = [] @seed_rb = "" @id_set_string = "" @verbose = true - @model_dir = 'app/models/*.rb' + @model_dir = 'app/models/**/*.rb' end def setup(env) # config - @opts['with_id'] = !env["WITH_ID"].nil? - @opts['no-data'] = !env['NO_DATA'].nil? + @opts['debug'] = env["DEBUG"].true? + @opts['with_id'] = env["WITH_ID"].true? + @opts['no-data'] = env['NO_DATA'].true? + @opts['without_protection'] = env['WITHOUT_PROTECTION'].true? + @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'].nil? && File.exists?(@opts['file']) ) + @opts['append'] = (env['APPEND'].true? && File.exists?(@opts['file']) ) @ar_options = env['LIMIT'].to_i > 0 ? { :limit => env['LIMIT'].to_i } : {} @indent = " " * (env['INDENT'].nil? ? 2 : env['INDENT'].to_i) @opts['models'] = @opts['models'].split(',').collect {|x| x.underscore.singularize.camelize } + @opts['schema'] = env['PG_SCHEMA'] + @opts['model_dir'] = env['MODEL_DIR'] || @model_dir end def loadModels - Dir[@model_dir].sort.each do |f| - model = File.basename(f, '.*').camelize + puts "Searching in #{@opts['model_dir']} for models" if @opts['debug'] + Dir[@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("::") + 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 dumpAttribute(a_s,r,k,v) - v = attribute_for_inspect(r,k) - if k == 'id' && @opts['with_id'] - @id_set_string = "{ |c| c.#{k} = #{v} }.save" + if v.is_a?(BigDecimal) + v = v.to_s else - a_s.push("#{k.to_sym.inspect} => #{v}") unless k == 'id' && !@opts['with_id'] + 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}") + end end def dumpModel(model) @id_set_string = '' 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) } - if @id_set_string.empty? - rows.push "#{@indent}{ " << attr_s.join(', ') << " }" - else - create_hash << "\n#{model}.create" << '( ' << attr_s.join(', ') << ' )' << @id_set_string - end + rows.push "#{@indent}{ " << attr_s.join(', ') << " }" } - if @id_set_string.empty? - "\n#{model}.create([\n" << rows.join(",\n") << "\n])\n" - else - create_hash + + if @opts['without_protection'] + options = ', :without_protection => true ' end + + "\n#{model}.create([\n" << rows.join(",\n") << "\n]#{options})\n" end def dumpModels @seed_rb = "" @models.sort.each do |model| m = model.constantize if m.ancestors.include?(ActiveRecord::Base) puts "Adding #{model} seeds." if @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 + end + @seed_rb << dumpModel(m) << "\n\n" else puts "Skipping non-ActiveRecord model #{model}..." if @verbose end + puts "Protection is disabled." if @verbose && @opts['without_protection'] end end def writeFile File.open(@opts['file'], (@opts['append'] ? "a" : "w")) { |f| + f << "# encoding: utf-8\n" f << "# Autogenerated by the db:seed:dump task\n# Do not hesitate to tweak this to your needs\n" unless @opts['append'] f << "#{@seed_rb}" } end @@ -94,12 +123,19 @@ else value.inspect end end + def setSearchPath(path, append_public=true) + path_parts = [path.to_s, ('public' if append_public)].compact + ActiveRecord::Base.connection.schema_search_path = path_parts.join(',') + end + def run(env) setup env + + setSearchPath @opts['schema'] if @opts['schema'] loadModels puts "Appending seeds to #{@opts['file']}." if @opts['append'] dumpModels