fixturies 0.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/fixturies.rb +52 -22
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 95b9e96f5a11eb6e20390fe38f5a786de44adf85930aa5218840d2e559144e7d
|
4
|
+
data.tar.gz: e83b5b488b24fdbe24f464bdff10d59e916e96a18bdf4e6d319e174f44310747
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33c08b5e3804540d938d019024e001c601e8f55f6320518e2573f9500dbe0ad56fe4bc57418d96c922a95e6a70057b19d3d2304e4eb87888414eeebc6fbacdfb
|
7
|
+
data.tar.gz: 4453b116af5b4ed500b78f0dc096c645a6709da2445770e00fe6cc23dbc684336d0279af8773a316140d894ce54b0d9a1ede02e26f5490ed53c5bb6d5e1dc6cd
|
data/lib/fixturies.rb
CHANGED
@@ -2,14 +2,11 @@ class Fixturies
|
|
2
2
|
|
3
3
|
class << self
|
4
4
|
|
5
|
-
attr_reader :fixtures_directory
|
5
|
+
attr_reader :fixtures_directory, :fixture_class_names
|
6
6
|
|
7
|
-
def build(
|
7
|
+
def build(&proc)
|
8
8
|
meth_name = :"builder_#{rand}"
|
9
9
|
define_method meth_name, &proc
|
10
|
-
things.each do |thing|
|
11
|
-
thing.is_a?(String) ? table(thing) : model(thing)
|
12
|
-
end
|
13
10
|
builders << meth_name
|
14
11
|
end
|
15
12
|
|
@@ -17,23 +14,32 @@ class Fixturies
|
|
17
14
|
@fixtures_directory = dir
|
18
15
|
end
|
19
16
|
|
17
|
+
def set_fixture_class(class_names = {})
|
18
|
+
@fixture_class_names ||= {}
|
19
|
+
@fixture_class_names = fixture_class_names.merge(class_names.stringify_keys)
|
20
|
+
end
|
21
|
+
|
20
22
|
def create_fixtures
|
21
23
|
self.new.create_fixtures
|
22
24
|
end
|
23
25
|
|
24
|
-
def
|
25
|
-
|
26
|
+
def table_names
|
27
|
+
@all_table_names ||= ActiveRecord::Base.connection.execute("
|
28
|
+
SELECT table_name
|
29
|
+
FROM information_schema.tables
|
30
|
+
WHERE
|
31
|
+
table_schema = 'public'
|
32
|
+
AND table_type = 'BASE TABLE'
|
33
|
+
").to_a.map { |r| r['table_name'] }
|
34
|
+
|
35
|
+
@all_table_names - table_names_to_skip
|
26
36
|
end
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
unless defined? @table_names
|
34
|
-
@table_names = Set.new
|
35
|
-
end
|
36
|
-
@table_names
|
38
|
+
# you can add more tables to this if necessary. For example,
|
39
|
+
# we had to add spatial_ref_sys, which is created by the
|
40
|
+
# postgis extension
|
41
|
+
def table_names_to_skip
|
42
|
+
@table_names_to_skip ||= ['schema_migrations']
|
37
43
|
end
|
38
44
|
|
39
45
|
def builders
|
@@ -67,6 +73,17 @@ class Fixturies
|
|
67
73
|
if record.id.nil?
|
68
74
|
raise ArgumentError.new("No id for record. Must be saved before calling identify")
|
69
75
|
end
|
76
|
+
|
77
|
+
# ensure that we are not assigning the same name to two
|
78
|
+
# different records
|
79
|
+
@records_by_name ||= {}
|
80
|
+
@records_by_name[record.class.table_name] ||= {}
|
81
|
+
existing_entry = @records_by_name[record.class.table_name][name]
|
82
|
+
if existing_entry && existing_entry != record
|
83
|
+
raise "Cannot assign the name #{name.inspect} to two different #{record.class.table_name}"
|
84
|
+
end
|
85
|
+
@records_by_name[record.class.table_name][name] = record
|
86
|
+
|
70
87
|
record_identifiers[record_key(record)] = name
|
71
88
|
end
|
72
89
|
|
@@ -84,12 +101,16 @@ class Fixturies
|
|
84
101
|
|
85
102
|
FileUtils.mkdir_p(self.class.fixtures_directory)
|
86
103
|
|
87
|
-
self.class.table_names.each do |table_name|
|
88
|
-
|
104
|
+
self.class.table_names.sort.each do |table_name|
|
105
|
+
|
106
|
+
filename = Rails.root.join(self.class.fixtures_directory, "#{table_name}.yml")
|
107
|
+
File.delete(filename) if File.exist?(filename)
|
108
|
+
|
89
109
|
# create a simple ActiveRecord klass to connect
|
90
110
|
# to the table and handle querying for us
|
91
111
|
klass = Class.new(ActiveRecord::Base) {
|
92
112
|
self.table_name = table_name
|
113
|
+
self.inheritance_column = nil # do not blow up if the type column indicates we should be using single-table inheritance
|
93
114
|
}
|
94
115
|
|
95
116
|
hash = {}
|
@@ -97,9 +118,17 @@ class Fixturies
|
|
97
118
|
name = record_identifiers[record_key(record)] || "#{table_name.singularize}_#{i}"
|
98
119
|
hash[name] = record.attributes
|
99
120
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
121
|
+
|
122
|
+
if hash.any?
|
123
|
+
# Rails allows baking the model_class into the YAML file, which
|
124
|
+
# can be useful if the class cannot be inferred from the table_name.
|
125
|
+
# See https://github.com/rails/rails/pull/20574/files
|
126
|
+
klass = self.class.fixture_class_names&.[](table_name)
|
127
|
+
hash['_fixture'] = {'model_class' => klass.to_s} if klass.present?
|
128
|
+
|
129
|
+
File.open(filename, 'w+') do |f|
|
130
|
+
f.write(hash.to_yaml)
|
131
|
+
end
|
103
132
|
end
|
104
133
|
|
105
134
|
end
|
@@ -108,11 +137,12 @@ class Fixturies
|
|
108
137
|
private
|
109
138
|
def clear_db
|
110
139
|
self.class.table_names.each do |table_name|
|
140
|
+
# Note: This does not handle foreign key constraints. See Readme for a solution
|
111
141
|
quoted_table_name = ActiveRecord::Base.connection.quote_table_name(table_name)
|
112
142
|
sql = "DELETE FROM #{quoted_table_name} "
|
113
143
|
ActiveRecord::Base.connection.delete(sql, "#{quoted_table_name} Delete all")
|
114
144
|
end
|
115
|
-
|
145
|
+
|
116
146
|
end
|
117
147
|
|
118
148
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixturies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Brustein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: The speed of fixtures meets the maintanability of factories
|
14
14
|
email: nate@pedago.com
|
@@ -27,17 +27,16 @@ require_paths:
|
|
27
27
|
- lib
|
28
28
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '0'
|
33
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ">="
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: '0'
|
38
38
|
requirements: []
|
39
|
-
|
40
|
-
rubygems_version: 2.2.2
|
39
|
+
rubygems_version: 3.0.8
|
41
40
|
signing_key:
|
42
41
|
specification_version: 4
|
43
42
|
summary: The speed of fixtures meets the maintanability of factories
|