lib/seed_dump/perform.rb in seed_dump-0.5.3 vs lib/seed_dump/perform.rb in seed_dump-0.6.0
- old
+ new
@@ -32,13 +32,23 @@
@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 loadModels
+ def monkeypatch_AR
+ ActiveRecord::Base.instance_eval do
+ def attr_accessible(*opts)
+ nil
+ end
+ end
+ 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/
@@ -68,25 +78,28 @@
def last_record
@last_record
end
- def dumpAttribute(a_s,r,k,v)
+ def dump_attribute(a_s, r, k, v)
+ pushed = false
if v.is_a?(BigDecimal)
v = v.to_s
else
v = attribute_for_inspect(r,k)
end
unless k == 'id' && !@opts['with_id']
if (!(k == 'created_at' || k == 'updated_at') || @opts['timestamps'])
a_s.push("#{k.to_sym.inspect} => #{v}")
+ pushed = true
end
end
+ pushed
end
- def dumpModel(model)
+ def dump_model(model)
@id_set_string = ''
@last_record = []
create_hash = ""
options = ''
rows = []
@@ -95,13 +108,13 @@
arr = arr.empty? ? [model.new] : arr
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
+ 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
end
rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
}
@@ -120,11 +133,11 @@
"\n#{model}.#{@opts['create_method']}([\n" << rows.join(",\n") << "\n]#{options})\n"
end
end
- def dumpModels
+ def dump_models
@seed_rb = ""
@models.sort.each do |model|
m = model.constantize
if m.ancestors.include?(ActiveRecord::Base) && !m.abstract_class
puts "Adding #{model} seeds." if @opts['verbose']
@@ -133,18 +146,18 @@
@seed_rb << "#{model}.reset_callbacks :save\n"
@seed_rb << "#{model}.reset_callbacks :create\n"
puts "Callbacks are disabled." if @opts['verbose']
end
- @seed_rb << dumpModel(m) << "\n\n"
+ @seed_rb << dump_model(m) << "\n\n"
else
puts "Skipping non-ActiveRecord model #{model}..." if @opts['verbose']
end
end
end
- def writeFile
+ def write_file
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}"
}
@@ -161,29 +174,29 @@
else
value.inspect
end
end
- def setSearchPath(path, append_public=true)
+ def set_search_path(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
puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection']
- setSearchPath @opts['schema'] if @opts['schema']
+ set_search_path @opts['schema'] if @opts['schema']
- loadModels
+ load_models
puts "Appending seeds to #{@opts['file']}." if @opts['append']
- dumpModels
+ dump_models
puts "Writing #{@opts['file']}."
- writeFile
+ write_file
puts "Done."
end
end
end