superseeder 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +13 -8
- data/lib/superseeder/adapter.rb +30 -0
- data/lib/superseeder/adapters/active_record.rb +31 -0
- data/lib/superseeder/adapters/mongoid.rb +31 -0
- data/lib/superseeder/formats/roo.rb +14 -14
- data/lib/superseeder/seedable.rb +11 -5
- data/lib/superseeder/version.rb +1 -1
- data/test/adapters/active_record.rb +26 -0
- data/test/adapters/all.rb +31 -0
- data/test/adapters/mongoid.rb +25 -0
- data/test/adapters/mongoid.yml +6 -0
- data/test/adapters/schema.rb +12 -0
- data/test/dummy/config/application.rb +16 -2
- data/test/dummy/config/environments/test.rb +3 -1
- data/test/dummy/db/seeds/data/cars_with_parkings.csv +4 -0
- data/test/dummy/db/seeds/data/cars_without_parkings.csv +4 -0
- data/test/dummy/db/seeds/data/multiple_2.wxwxwx +0 -0
- data/test/dummy/db/seeds/data/noformat.tttt +0 -0
- data/test/dummy/db/seeds/data/parkings_with_cars.csv +5 -0
- data/test/dummy/db/seeds/data/parkings_without_cars.csv +5 -0
- data/test/dummy/db/seeds/data/whatevers.wxwxwx +0 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +5476 -0
- data/test/superseeder/formats/roo_test.rb +56 -0
- data/test/superseeder/seedable_test.rb +54 -0
- data/test/test_helper.rb +25 -8
- metadata +81 -6
- data/test/superseeder_test.rb +0 -7
- /data/test/dummy/{public/favicon.ico → db/seeds/data/multiple_1.wxwxwx} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ac1535106365fed57579024bed2fffd848749ce
|
4
|
+
data.tar.gz: ce9deb018e56f473cc7ddfbb1c84194064cb05c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2474335519980b1a92432b879876e62b34f29b58670758943e12535bbf2e0863c2fbdbc485170fff26e6e5989f11471d07ffbcc2fc2ef627eaee529027de6101
|
7
|
+
data.tar.gz: 941980a27de9ee42dd0d6e0c4a36f9a3a6eaab69b447e25334c66314180d6e1fa18e1867593f403b5e61612ba391554714fcfef8104b8397baf34ef2bd7fb396
|
data/Rakefile
CHANGED
@@ -14,19 +14,24 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
14
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
17
|
Bundler::GemHelper.install_tasks
|
21
18
|
|
22
19
|
require 'rake/testtask'
|
23
20
|
|
24
21
|
Rake::TestTask.new(:test) do |t|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
if ENV['ADAPTER'].nil?
|
23
|
+
require 'superseeder/adapter'
|
24
|
+
::Superseeder::Adapter.adapters.each do |adapter|
|
25
|
+
ENV['ADAPTER'] = adapter
|
26
|
+
Rake::Task['test'].execute
|
27
|
+
end
|
28
|
+
else
|
29
|
+
puts "Testing with #{ENV['ADAPTER']} adapter..."
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
29
35
|
end
|
30
36
|
|
31
|
-
|
32
37
|
task default: :test
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Superseeder
|
2
|
+
class Adapter
|
3
|
+
|
4
|
+
|
5
|
+
def self.adapters
|
6
|
+
%w(active_record mongoid)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(instance)
|
10
|
+
@instance = instance
|
11
|
+
if defined? ActiveRecord::Base
|
12
|
+
if instance.kind_of? ActiveRecord::Base
|
13
|
+
require 'superseeder/adapters/active_record'
|
14
|
+
self.singleton_class.include ::Superseeder::Adapters::ActiveRecord
|
15
|
+
end
|
16
|
+
end
|
17
|
+
if defined? Mongoid::Document
|
18
|
+
if instance.kind_of? Mongoid::Document
|
19
|
+
require 'superseeder/adapters/mongoid'
|
20
|
+
self.singleton_class.include ::Superseeder::Adapters::Mongoid
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
attr_reader :instance
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Superseeder
|
2
|
+
module Adapters
|
3
|
+
module ActiveRecord
|
4
|
+
|
5
|
+
def each_relation
|
6
|
+
self.instance.singleton_class.reflections.each do |key, val|
|
7
|
+
yield key, self.is_array_relation?(val), val.class_name.constantize
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def each_field(row)
|
12
|
+
self.instance.attributes.each do |field, val|
|
13
|
+
next unless row.key? field
|
14
|
+
yield field, false #self.is_array_field?(options) TODO inquiry
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def is_array_field?(options)
|
21
|
+
options.type == Array
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_array_relation?(reflection)
|
25
|
+
[::ActiveRecord::Reflection::HasManyReflection,
|
26
|
+
::ActiveRecord::Reflection::HasAndBelongsToManyReflection].any?{ |c| reflection.kind_of? c }
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Superseeder
|
2
|
+
module Adapters
|
3
|
+
module Mongoid
|
4
|
+
|
5
|
+
def each_relation
|
6
|
+
self.instance.relations.each do |key, val|
|
7
|
+
yield key, self.is_array_relation?(val), val.class_name.constantize
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def each_field(row)
|
12
|
+
self.instance.fields.select{ |f, o| row.key? o.options[:as] || f }.each do |field, options|
|
13
|
+
yield options.options[:as] || field, self.is_array_field?(options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def is_array_field?(options)
|
20
|
+
options.type == Array
|
21
|
+
end
|
22
|
+
|
23
|
+
def is_array_relation?(options)
|
24
|
+
[::Mongoid::Relations::Referenced::Many,
|
25
|
+
::Mongoid::Relations::Embedded::Many,
|
26
|
+
::Mongoid::Relations::Referenced::ManyToMany].include? options[:relation]
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -33,29 +33,29 @@ module Superseeder
|
|
33
33
|
row['_type'].constantize
|
34
34
|
end.new
|
35
35
|
|
36
|
+
require 'superseeder/adapter'
|
37
|
+
adapter = ::Superseeder::Adapter.new instance
|
38
|
+
|
36
39
|
# Set relations
|
37
|
-
|
38
|
-
attrs = row.select{ |k, _| k =~ /\A#{
|
40
|
+
adapter.each_relation do |name, is_array, class_name|
|
41
|
+
attrs = row.select{ |k, _| k =~ /\A#{name}_/ }
|
39
42
|
next if attrs.empty?
|
40
|
-
attrs = attrs.inject({}){ |h, (k, v)| h[k.sub /\A#{
|
41
|
-
if
|
42
|
-
Mongoid::Relations::Embedded::Many,
|
43
|
-
Mongoid::Relations::Referenced::ManyToMany].include? val[:relation]
|
43
|
+
attrs = attrs.inject({}){ |h, (k, v)| h[k.sub /\A#{name}_/, ''] = v; h }
|
44
|
+
if is_array
|
44
45
|
vals = attrs.map do |k, v|
|
45
|
-
v.split(many_sep).map{
|
46
|
+
v.split(many_sep).map{ |u| class_name.find_by k => u } unless v.nil?
|
46
47
|
end
|
47
48
|
vals.flatten!
|
48
49
|
vals.compact!
|
49
|
-
instance.send "#{
|
50
|
+
instance.send "#{name}=", vals
|
50
51
|
else
|
51
|
-
instance.send "#{
|
52
|
+
instance.send "#{name}=", class_name.find_by(attrs)
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
55
|
-
# Set
|
56
|
-
|
57
|
-
|
58
|
-
val = if o.type == Array
|
56
|
+
# Set fields
|
57
|
+
adapter.each_field(row) do |name, is_array|
|
58
|
+
val = if is_array
|
59
59
|
row[name].try(:split, many_sep)
|
60
60
|
else
|
61
61
|
row[name]
|
@@ -66,7 +66,7 @@ module Superseeder
|
|
66
66
|
if instance.valid?
|
67
67
|
instance.save
|
68
68
|
else
|
69
|
-
logger.debug "Skipped #{row} : #{instance.errors.full_messages}"
|
69
|
+
Rails.logger.debug "Skipped #{row} : #{instance.errors.full_messages}"
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
data/lib/superseeder/seedable.rb
CHANGED
@@ -2,8 +2,12 @@ module Superseeder
|
|
2
2
|
module Seedable
|
3
3
|
|
4
4
|
def seed(*args, &block)
|
5
|
-
opts
|
6
|
-
path =
|
5
|
+
opts = args.extract_options!
|
6
|
+
path = if opts[:path].blank?
|
7
|
+
Rails.root.join('db', 'seeds', 'data')
|
8
|
+
else
|
9
|
+
Pathname(opts.delete :path)
|
10
|
+
end
|
7
11
|
filename = opts.delete :filename
|
8
12
|
path = if filename.blank?
|
9
13
|
tmp = path.join "#{self.name.underscore.pluralize}*"
|
@@ -12,18 +16,20 @@ module Superseeder
|
|
12
16
|
raise ArgumentError.new "No candidate files for #{self.name}. Either create a #{tmp} file or specify the filename you want to use with the :filename option." if candidates.length == 0
|
13
17
|
candidates.first
|
14
18
|
else
|
15
|
-
path.join filename
|
19
|
+
tmp = path.join filename
|
20
|
+
raise ArgumentError.new "#{filename} does not exist." unless File.exist? tmp
|
21
|
+
tmp
|
16
22
|
end
|
17
23
|
ext = File.extname path
|
18
24
|
Dir[Pathname(Gem::Specification.find_by_name('superseeder').gem_dir).join('lib','superseeder','formats','*.rb')].each{ |f| require(f) }
|
19
25
|
modules = Superseeder::Formats.constants.select{ |c| Module === Superseeder::Formats.const_get(c) }.map{ |c| Superseeder::Formats.const_get(c) }
|
20
26
|
mod = modules.find{ |m| m.send(:extensions).include? ext }
|
21
27
|
raise ArgumentError.new "No registered module to support #{ext} extension." if mod.nil?
|
22
|
-
logger.debug "Seeding #{self.name.downcase.pluralize.humanize} from #{path}..."
|
28
|
+
Rails.logger.debug "Seeding #{self.name.downcase.pluralize.humanize} from #{path}..."
|
23
29
|
count = self.count
|
24
30
|
self.extend mod
|
25
31
|
self.__process path, opts, &block
|
26
|
-
logger.debug "Done. (#{self.count - count} entries added)"
|
32
|
+
Rails.logger.debug "Done. (#{self.count - count} entries added)"
|
27
33
|
end
|
28
34
|
end
|
29
35
|
|
data/lib/superseeder/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
5
|
+
|
6
|
+
class Car < ActiveRecord::Base
|
7
|
+
|
8
|
+
belongs_to :parking
|
9
|
+
|
10
|
+
validates :name, :presence => true
|
11
|
+
end
|
12
|
+
|
13
|
+
class Parking < ActiveRecord::Base
|
14
|
+
|
15
|
+
has_many :cars
|
16
|
+
|
17
|
+
validates :name, :presence => true, :uniqueness => true
|
18
|
+
end
|
19
|
+
|
20
|
+
load File.dirname(__FILE__) + '/schema.rb'
|
21
|
+
#ActiveRecord::Tasks::DatabaseTasks.migrate
|
22
|
+
#Rake.load_rakefile 'active_record/railties/databases.rake'
|
23
|
+
#require 'active_record/railties/databases.rake'
|
24
|
+
#Rake::Task['db:migrate'].invoke
|
25
|
+
|
26
|
+
require_relative 'all'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Whatever
|
2
|
+
|
3
|
+
def self.count
|
4
|
+
0
|
5
|
+
end
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
class Multiple
|
10
|
+
|
11
|
+
def self.count
|
12
|
+
0
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class Nofile
|
18
|
+
|
19
|
+
def self.count
|
20
|
+
0
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Noformat
|
26
|
+
|
27
|
+
def self.count
|
28
|
+
0
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
Mongoid.load! 'test/adapters/mongoid.yml', :test
|
4
|
+
Mongoid.raise_not_found_error = false
|
5
|
+
|
6
|
+
class Car
|
7
|
+
include Mongoid::Document
|
8
|
+
|
9
|
+
field :name, :type => String
|
10
|
+
|
11
|
+
belongs_to :parking
|
12
|
+
|
13
|
+
validates :name, :presence => true
|
14
|
+
end
|
15
|
+
|
16
|
+
class Parking
|
17
|
+
include Mongoid::Document
|
18
|
+
|
19
|
+
field :name, :type => String
|
20
|
+
has_many :cars
|
21
|
+
|
22
|
+
validates :name, :presence => true, :uniqueness => true
|
23
|
+
end
|
24
|
+
|
25
|
+
require_relative 'all'
|
@@ -1,9 +1,23 @@
|
|
1
1
|
require File.expand_path('../boot', __FILE__)
|
2
2
|
|
3
|
-
require 'rails
|
3
|
+
require 'rails'
|
4
|
+
|
5
|
+
%w(
|
6
|
+
action_controller
|
7
|
+
action_view
|
8
|
+
action_mailer
|
9
|
+
active_job
|
10
|
+
rails/test_unit
|
11
|
+
sprockets
|
12
|
+
).each do |framework|
|
13
|
+
begin
|
14
|
+
require "#{framework}/railtie"
|
15
|
+
rescue LoadError
|
16
|
+
end
|
17
|
+
end
|
4
18
|
|
5
19
|
Bundler.require(*Rails.groups)
|
6
|
-
require
|
20
|
+
require 'superseeder'
|
7
21
|
|
8
22
|
module Dummy
|
9
23
|
class Application < Rails::Application
|
@@ -13,7 +13,7 @@ Rails.application.configure do
|
|
13
13
|
config.eager_load = false
|
14
14
|
|
15
15
|
# Configure static asset server for tests with Cache-Control for performance.
|
16
|
-
config.
|
16
|
+
config.serve_static_files = true
|
17
17
|
config.static_cache_control = 'public, max-age=3600'
|
18
18
|
|
19
19
|
# Show full error reports and disable caching.
|
@@ -34,6 +34,8 @@ Rails.application.configure do
|
|
34
34
|
# Print deprecation notices to the stderr.
|
35
35
|
config.active_support.deprecation = :stderr
|
36
36
|
|
37
|
+
config.active_support.test_order = :random
|
38
|
+
|
37
39
|
# Raises error for missing translations
|
38
40
|
# config.action_view.raise_on_missing_translations = true
|
39
41
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|