Module: SeedDump::DumpMethods
- Included in:
- SeedDump
- Defined in:
- lib/seed_dump/dump_methods.rb
Instance Method Summary collapse
-
#attribute_for_inspect(r, k) ⇒ Object
override the rails version of this function to NOT truncate strings.
- #dump_attribute(a_s, r, k, v) ⇒ Object
- #dump_model(model) ⇒ Object
- #dump_models ⇒ Object
- #initialize ⇒ Object
- #log(msg) ⇒ Object
- #models ⇒ Object
- #output ⇒ Object
- #run(env) ⇒ Object
- #setup(env) ⇒ Object
- #write_file ⇒ Object
Instance Method Details
#attribute_for_inspect(r, k) ⇒ Object
override the rails version of this function to NOT truncate strings
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/seed_dump/dump_methods.rb', line 123 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 value.inspect end end |
#dump_attribute(a_s, r, k, v) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/seed_dump/dump_methods.rb', line 42 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 |
#dump_model(model) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/seed_dump/dump_methods.rb', line 59 def dump_model(model) @id_set_string = '' create_hash = "" rows = [] model.find_each(batch_size: (@limit || 1000)) do |record| attr_s = []; record.attributes.select {|x| x.is_a?(String) }.each do |k,v| dump_attribute(attr_s, record, k, v) end rows.push "#{@indent}{ " << attr_s.join(', ') << " }" break if rows.length == @limit 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])\n" end maxsarr.join('') else "\n#{model}.#{@opts['create_method']}([\n" << rows.join(",\n") << "\n])\n" end end |
#dump_models ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/seed_dump/dump_methods.rb', line 90 def dump_models if @models.empty? Rails.application.eager_load! @seed_rb = "" @models = ActiveRecord::Base.descendants.select do |model| (model.to_s != 'ActiveRecord::SchemaMigration') && \ model.table_exists? && \ model.exists? end end @models.sort! { |a, b| a.to_s <=> b.to_s } @models.each do |model| puts "Adding #{model} seeds." if @opts['verbose'] @seed_rb << dump_model(model) << "\n\n" end @seed_rb end |
#initialize ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/seed_dump/dump_methods.rb', line 7 def initialize @opts = {} = {} @indent = "" @models = [] @seed_rb = "" @id_set_string = "" end |
#log(msg) ⇒ Object
34 35 36 |
# File 'lib/seed_dump/dump_methods.rb', line 34 def log(msg) puts msg if @opts['debug'] end |
#models ⇒ Object
38 39 40 |
# File 'lib/seed_dump/dump_methods.rb', line 38 def models @models end |
#output ⇒ Object
135 136 137 |
# File 'lib/seed_dump/dump_methods.rb', line 135 def output @seed_rb end |
#run(env) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/seed_dump/dump_methods.rb', line 139 def run(env) setup env puts "Appending seeds to #{@opts['file']}." if @opts['append'] dump_models puts "Writing #{@opts['file']}." write_file puts "Done." end |
#setup(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/seed_dump/dump_methods.rb', line 16 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['models'] = env['MODELS'] || 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 @indent = " " * (env['INDENT'].nil? ? 2 : env['INDENT'].to_i) @opts['create_method'] = env['CREATE_METHOD'] || 'create!' @limit = (env['LIMIT'].to_i > 0) ? env['LIMIT'].to_i : nil @models = @opts['models'].split(',').collect {|x| x.strip.underscore.singularize.camelize.constantize } end |
#write_file ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/seed_dump/dump_methods.rb', line 114 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}" } end |