lib/seed_dump/perform.rb in seed_dump-0.6.0 vs lib/seed_dump/perform.rb in seed_dump-1.0.0

- old
+ new

@@ -20,11 +20,10 @@ @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? || (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']) ) @opts['max'] = env['MAX'] && env['MAX'].to_i > 0 ? env['MAX'].to_i : nil @@ -32,45 +31,50 @@ @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' - @opts['rails4'] = env['RAILS4'].true? - monkeypatch_AR if @opts['rails4'] end - def monkeypatch_AR - ActiveRecord::Base.instance_eval do - def attr_accessible(*opts) - nil - end - end + def log(msg) + puts msg if @opts['debug'] end def load_models - puts "Searching in #{@opts['model_dir']} for models" if @opts['debug'] - 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 - parts = $1.split("/").map {|x| x.camelize} + log("Searching in #{@opts['model_dir']} for models") + Dir[File.join(Dir.pwd, @opts['model_dir'])].sort.each do |f| + log("Processing file #{f}") + + dirname, basename = File.split(f) + + dir_array = dirname.split(File::SEPARATOR) + + # Find index of last occurence of 'models' in path + models_index = nil + dir_array.each_with_index {|x, i| models_index = i if x == 'models'} + + model_dir_array = dir_array[models_index + 1..-1] + # Initialize nested model namespaces - parts.clip.inject(Object) do |x, y| - if x.const_defined?(y) - x.const_get(y) + model_dir_array.inject(Object) do |parent, child| + child = child.camelize + + if parent.const_defined?(child) + parent.const_get(child) else - x.const_set(y, Module.new) + parent.const_set(child, Module.new) end end - model = parts.join("::") require f - puts "Detected model #{model}" if @opts['debug'] - @models.push model if @opts['models'].include?(model) || @opts['models'].empty? + model = File.join(model_dir_array + [File.basename(basename, '.rb')]).camelize + + log("Detected model #{model}") + + @models << model if @opts['models'].include?(model) || @opts['models'].empty? end end def models @models @@ -101,29 +105,26 @@ @id_set_string = '' @last_record = [] create_hash = "" options = '' rows = [] - arr = [] - arr = model.find(:all, @ar_options) unless @opts['no-data'] + arr = nil + unless @opts['no-data'] + arr = model.all + arr.limit(@ar_options[:limit]) if @ar_options[:limit] + end arr = arr.empty? ? [model.new] : arr arr.each_with_index { |r,i| attr_s = []; r.attributes.each do |k,v| - if (@opts['rails4'] || (model.attr_accessible[:default].include? k) || @opts['without_protection'] || @opts['with_id']) - pushed_key = dump_attribute(attr_s,r,k,v) - @last_record.push k if pushed_key - end + pushed_key = dump_attribute(attr_s,r,k,v) + @last_record.push k if pushed_key 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}.#{@opts['create_method']}([\n" << sr.join(",\n") << "\n]#{options})\n" @@ -180,13 +181,10 @@ 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 - - puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection'] set_search_path @opts['schema'] if @opts['schema'] load_models