lib/seed_dump/perform.rb in seed_dump-0.4.3 vs lib/seed_dump/perform.rb in seed_dump-0.5.0

- old
+ new

@@ -4,15 +4,15 @@ module SeedDump class Perform def initialize @opts = {} - @ar_options = {} + @ar_options = {} @indent = "" @models = [] @last_record = [] - @seed_rb = "" + @seed_rb = "" @id_set_string = "" @model_dir = 'app/models/**/*.rb' end def setup(env) @@ -31,10 +31,11 @@ @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 + @opts['create_method'] = env['CREATE_METHOD'] || 'create' end def loadModels puts "Searching in #{@opts['model_dir']} for models" if @opts['debug'] Dir[Dir.pwd + '/' + @opts['model_dir']].sort.each do |f| @@ -55,11 +56,11 @@ model = parts.join("::") require f puts "Detected model #{model}" if @opts['debug'] - @models.push model if @opts['models'].include?(model) || @opts['models'].empty? + @models.push model if @opts['models'].include?(model) || @opts['models'].empty? end end def models @models @@ -72,55 +73,55 @@ def dumpAttribute(a_s,r,k,v) if v.is_a?(BigDecimal) v = v.to_s else v = attribute_for_inspect(r,k) - end + end unless k == 'id' && !@opts['with_id'] if (!(k == 'created_at' || k == 'updated_at') || @opts['timestamps']) a_s.push("#{k.to_sym.inspect} => #{v}") end - 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 = arr.empty? ? [model.new] : arr - arr.each_with_index { |r,i| + arr.each_with_index { |r,i| attr_s = []; 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 + @last_record.push k end end rows.push "#{@indent}{ " << attr_s.join(', ') << " }" - } + } if @opts['without_protection'] options = ', :without_protection => true ' end - + if @opts['max'] splited_rows = rows.each_slice(@opts['max']).to_a maxsarr = [] splited_rows.each do |sr| - maxsarr << "\n#{model}.create([\n" << sr.join(",\n") << "\n]#{options})\n" - end + maxsarr << "\n#{model}.#{@opts['create_method']}([\n" << sr.join(",\n") << "\n]#{options})\n" + end maxsarr.join('') else - "\n#{model}.create([\n" << rows.join(",\n") << "\n]#{options})\n" + "\n#{model}.#{@opts['create_method']}([\n" << rows.join(",\n") << "\n]#{options})\n" end - + end def dumpModels @seed_rb = "" @models.sort.each do |model| @@ -150,10 +151,10 @@ end #override the rails version of this function to NOT truncate strings def attribute_for_inspect(r,k) value = r.attributes[k] - + if value.is_a?(String) && value.length > 50 "#{value}".inspect elsif value.is_a?(Date) || value.is_a?(Time) %("#{value.to_s(:db)}") else