fixturies 0.0.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/fixturies.rb +29 -22
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6968e04db164bea8c7163b97617759f4b748e0b202d6d265f5ac4baa9ce4069
|
4
|
+
data.tar.gz: 1023e35d561e595ebcf1cb94f3da96fc82717fd83ff8a3a4b1ad6b96d0b9baed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3fac4e8eda8220a5f62c396f32faa29248c811d2f7e2ecfa05a2cf4d688b88f76406fa0b6112131df871952f418e5c7713947d5748c0fa51bfd8e02f3f7f6fd
|
7
|
+
data.tar.gz: 2bd9b0ccf7d2c725319ea4f8fe09b8a9439f702dd4fa0db241dd2f5f23c8254ef8fcc74d8ee560ee8e9538dccdaa9c24f11f5a68bde46c3acab8845babde61ad
|
data/lib/fixturies.rb
CHANGED
@@ -4,12 +4,9 @@ class Fixturies
|
|
4
4
|
|
5
5
|
attr_reader :fixtures_directory
|
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
|
|
@@ -21,19 +18,23 @@ class Fixturies
|
|
21
18
|
self.new.create_fixtures
|
22
19
|
end
|
23
20
|
|
24
|
-
def
|
25
|
-
|
21
|
+
def table_names
|
22
|
+
@all_table_names ||= ActiveRecord::Base.connection.execute("
|
23
|
+
SELECT table_name
|
24
|
+
FROM information_schema.tables
|
25
|
+
WHERE
|
26
|
+
table_schema = 'public'
|
27
|
+
AND table_type = 'BASE TABLE'
|
28
|
+
").to_a.map { |r| r['table_name'] }
|
29
|
+
|
30
|
+
@all_table_names - table_names_to_skip
|
26
31
|
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
unless defined? @table_names
|
34
|
-
@table_names = Set.new
|
35
|
-
end
|
36
|
-
@table_names
|
33
|
+
# you can add more tables to this if necessary. For example,
|
34
|
+
# we had to add spatial_ref_sys, which is created by the
|
35
|
+
# postgis extension
|
36
|
+
def table_names_to_skip
|
37
|
+
@table_names_to_skip ||= ['schema_migrations']
|
37
38
|
end
|
38
39
|
|
39
40
|
def builders
|
@@ -55,6 +56,7 @@ class Fixturies
|
|
55
56
|
clear_db
|
56
57
|
build_all_records
|
57
58
|
create_fixture_files
|
59
|
+
clear_db
|
58
60
|
end
|
59
61
|
|
60
62
|
def build_all_records
|
@@ -68,7 +70,7 @@ class Fixturies
|
|
68
70
|
raise ArgumentError.new("No id for record. Must be saved before calling identify")
|
69
71
|
end
|
70
72
|
|
71
|
-
# ensure that we are not assigning the same name to two
|
73
|
+
# ensure that we are not assigning the same name to two
|
72
74
|
# different records
|
73
75
|
@records_by_name ||= {}
|
74
76
|
@records_by_name[record.class.table_name] ||= {}
|
@@ -77,7 +79,7 @@ class Fixturies
|
|
77
79
|
raise "Cannot assign the name #{name.inspect} to two different #{record.class.table_name}"
|
78
80
|
end
|
79
81
|
@records_by_name[record.class.table_name][name] = record
|
80
|
-
|
82
|
+
|
81
83
|
record_identifiers[record_key(record)] = name
|
82
84
|
end
|
83
85
|
|
@@ -95,12 +97,16 @@ class Fixturies
|
|
95
97
|
|
96
98
|
FileUtils.mkdir_p(self.class.fixtures_directory)
|
97
99
|
|
98
|
-
self.class.table_names.each do |table_name|
|
99
|
-
|
100
|
+
self.class.table_names.sort.each do |table_name|
|
101
|
+
|
102
|
+
filename = Rails.root.join(self.class.fixtures_directory, "#{table_name}.yml")
|
103
|
+
File.delete(filename) if File.exist?(filename)
|
104
|
+
|
100
105
|
# create a simple ActiveRecord klass to connect
|
101
106
|
# to the table and handle querying for us
|
102
107
|
klass = Class.new(ActiveRecord::Base) {
|
103
108
|
self.table_name = table_name
|
109
|
+
self.inheritance_column = nil # do not blow up if the type column indicates we should be using single-table inheritance
|
104
110
|
}
|
105
111
|
|
106
112
|
hash = {}
|
@@ -108,8 +114,8 @@ class Fixturies
|
|
108
114
|
name = record_identifiers[record_key(record)] || "#{table_name.singularize}_#{i}"
|
109
115
|
hash[name] = record.attributes
|
110
116
|
end
|
111
|
-
|
112
|
-
File.open(
|
117
|
+
|
118
|
+
File.open(filename, 'w+') do |f|
|
113
119
|
f.write(hash.to_yaml)
|
114
120
|
end
|
115
121
|
|
@@ -119,11 +125,12 @@ class Fixturies
|
|
119
125
|
private
|
120
126
|
def clear_db
|
121
127
|
self.class.table_names.each do |table_name|
|
128
|
+
# Note: This does not handle foreign key constraints. See Readme for a solution
|
122
129
|
quoted_table_name = ActiveRecord::Base.connection.quote_table_name(table_name)
|
123
130
|
sql = "DELETE FROM #{quoted_table_name} "
|
124
131
|
ActiveRecord::Base.connection.delete(sql, "#{quoted_table_name} Delete all")
|
125
132
|
end
|
126
|
-
|
133
|
+
|
127
134
|
end
|
128
135
|
|
129
136
|
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: 0.0
|
4
|
+
version: 1.0.0
|
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-16 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
|
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
37
|
version: '0'
|
38
38
|
requirements: []
|
39
39
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
40
|
+
rubygems_version: 2.7.6
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
43
|
summary: The speed of fixtures meets the maintanability of factories
|