fixturies 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/fixturies.rb +29 -22
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d0c0f13f16997f6ea9705cdcf02ecbe3df5177ca
4
- data.tar.gz: 49464f7dda3f08ee94a64d02ac151dfea707ca9b
2
+ SHA256:
3
+ metadata.gz: b6968e04db164bea8c7163b97617759f4b748e0b202d6d265f5ac4baa9ce4069
4
+ data.tar.gz: 1023e35d561e595ebcf1cb94f3da96fc82717fd83ff8a3a4b1ad6b96d0b9baed
5
5
  SHA512:
6
- metadata.gz: 6b83a2d56b74d0b5d13c1d0cc169a255173aa24a57b3540d2751ebbe27336708825c1820299e554bf657dd4fc272cd1517a855cacd344679e2cda1049c3c4c1f
7
- data.tar.gz: 61559265b9adde4649f1a24b5595f3a21c731395dfdfc738fdb548114f07dde7753072329e095e5e0821266e11f9a9ca8d1ae9dfe27702a15285d6413078a637
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(*things, &proc)
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 table(table_name)
25
- self.table_names << table_name
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
- def model(model_klass)
29
- self.table_names << model_klass.table_name
30
- end
31
-
32
- def table_names
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(Rails.root.join(self.class.fixtures_directory, "#{table_name}.yml"), 'w+') do |f|
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.2
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: 2016-03-07 00:00:00.000000000 Z
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.4.8
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