Class: SeedDump::Perform
Instance Method Summary collapse
-
#attribute_for_inspect(r, k) ⇒ Object
override the rails version of this function to NOT truncate strings.
- #dumpAttribute(a_s, r, k, v) ⇒ Object
- #dumpModel(model) ⇒ Object
- #dumpModels ⇒ Object
-
#initialize ⇒ Perform
constructor
A new instance of Perform.
- #last_record ⇒ Object
- #loadModels ⇒ Object
- #models ⇒ Object
- #run(env) ⇒ Object
- #setSearchPath(path, append_public = true) ⇒ Object
- #setup(env) ⇒ Object
- #writeFile ⇒ Object
Constructor Details
#initialize ⇒ Perform
Returns a new instance of Perform.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/seed_dump/perform.rb', line 7 def initialize @opts = {} @ar_options = {} @indent = "" @models = [] @last_record = [] @seed_rb = "" @id_set_string = "" @model_dir = 'app/models/**/*.rb' end |
Instance Method Details
#attribute_for_inspect(r, k) ⇒ Object
override the rails version of this function to NOT truncate strings
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/seed_dump/perform.rb', line 153 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 |
#dumpAttribute(a_s, r, k, v) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/seed_dump/perform.rb', line 72 def dumpAttribute(a_s,r,k,v) 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}") end end end |
#dumpModel(model) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/seed_dump/perform.rb', line 86 def dumpModel(model) @id_set_string = '' @last_record = [] create_hash = "" = '' 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 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 end end rows.push "#{@indent}{ " << attr_s.join(', ') << " }" } if @opts['without_protection'] = ', :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]#{})\n" end maxsarr.join('') else "\n#{model}.create([\n" << rows.join(",\n") << "\n]#{})\n" end end |
#dumpModels ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/seed_dump/perform.rb', line 124 def dumpModels @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'] if @opts['skip_callbacks'] @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" else puts "Skipping non-ActiveRecord model #{model}..." if @opts['verbose'] end end end |
#last_record ⇒ Object
68 69 70 |
# File 'lib/seed_dump/perform.rb', line 68 def last_record @last_record end |
#loadModels ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/seed_dump/perform.rb', line 38 def loadModels 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} # Initialize nested model namespaces parts.clip.inject(Object) do |x, y| if x.const_defined?(y) x.const_get(y) else x.const_set(y, 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? end end |
#models ⇒ Object
64 65 66 |
# File 'lib/seed_dump/perform.rb', line 64 def models @models end |
#run(env) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/seed_dump/perform.rb', line 170 def run(env) setup env puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection'] setSearchPath @opts['schema'] if @opts['schema'] loadModels puts "Appending seeds to #{@opts['file']}." if @opts['append'] dumpModels puts "Writing #{@opts['file']}." writeFile puts "Done." end |
#setSearchPath(path, append_public = true) ⇒ Object
165 166 167 168 |
# File 'lib/seed_dump/perform.rb', line 165 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 |
#setup(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/seed_dump/perform.rb', line 18 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['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 @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 |
#writeFile ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/seed_dump/perform.rb', line 144 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 |