fixture_me 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b938988f5a24da9822880fff81e66acf182e2e9d
4
- data.tar.gz: 3a8ca5c16344831560dcdea3a0a3f5cc290fadbd
3
+ metadata.gz: d9810644f0c121be6dabdbe00e68646a685bc211
4
+ data.tar.gz: edaf8ae132ee5c0184a3058e3aebea5d1538a931
5
5
  SHA512:
6
- metadata.gz: 6fd4b29acf6448bddd1820af3c491857ce6176b459aaaca4be7c5679fc6a873afd5475a925bd8ff0818be7b8e849d01c172d075fac7799362463928f0044910e
7
- data.tar.gz: f8f216a50b83c4e33f95c104e5a9c559f6000be9a83a9bf6139ed8c3a8021a8ffbef1861c05f9ff07c02332351326d7b92ee74b2c53bd391cbc0d51adb387eb5
6
+ metadata.gz: 710dd27e7733e1aa35076e9870f1e8ae968a834ba48030ebe203df62592726ecda5eeb8a85d1c86f5bad2c261f801e34fc6a65ab737f2c5720c367cf22815a75
7
+ data.tar.gz: 5503d39655aabfd0a3e282f15f8d5bc69c5b3e511e3f51db7bf52daab1b3fddd4639fded7be55cd3770b0867a8ccd91386a898c1bbf4d52152feddb3228116b5
data/README.md CHANGED
@@ -28,18 +28,26 @@ Or install it yourself as:
28
28
  rails console
29
29
  ```
30
30
 
31
- Oce your are inside Rails console
31
+ Once your are inside Rails console
32
32
 
33
33
  To generate fixtures for all models.
34
34
 
35
35
  A new directory called fixtures would be created inside tmp directory (this is to make sure that this fixture generation would not override existing fixtures)
36
36
 
37
+
37
38
  ```ruby
38
- FixtureMe::AddFixtures.create_all_fixtures
39
+ fixme = FixtureMe::AddFixtures.new
40
+ fixme.create_all_fixtures
39
41
  ```
40
42
 
43
+ to exclude created_at and updated_at columns
41
44
 
45
+ ```ruby
46
+ fixme = FixtureMe::AddFixtures.new
47
+ fixme.create_all_fixtures_no_timestamps
48
+ ```
42
49
 
50
+
43
51
  To generate fixtures one by one
44
52
 
45
53
  a mymodel.yml file would be generated inside that file does not exist in the test/fixtures directory
Binary file
@@ -1,3 +1,3 @@
1
1
  module FixtureMe
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/fixture_me.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "fixture_me/version"
2
-
2
+ require 'fileutils'
3
3
  module FixtureMe
4
4
 
5
5
  def add_fixture
@@ -19,18 +19,88 @@ module FixtureMe
19
19
  end
20
20
 
21
21
 
22
+ def add_fixture_no_id_timestamps
23
+ hash = self.attributes.delete_if { |k, v| ["created_at", "updated_at", "id"].include? k }
24
+ md5 = Digest::MD5.hexdigest(hash.to_s)
25
+ string = "#{md5}:\n"
26
+ hash.each do |key,value|
27
+ value.gsub!("\n","\n"+" "*4) if value.class == String
28
+ string += " "*2 + "#{key.to_s}: #{value}\n" unless value.nil?
29
+ end
30
+ string += "\n"
31
+ table_name = self.class.table_name
32
+ file_name = table_name + ".yml"
33
+ File.open("#{Rails.root}/test/fixtures/#{file_name}", 'a') do |out|
34
+ out.write(string)
35
+ end
36
+ end
37
+
38
+
22
39
 
23
40
  class AddFixtures
24
41
 
25
42
 
26
- def self.create_all_fixtures
43
+ def initialize
44
+ @fixtures_dir = FileUtils.mkdir_p( "#{Rails.root}/tmp/fixtures/").first
45
+ end
46
+
47
+ def fixtures_dir
48
+ @fixtures_dir
49
+ end
50
+
51
+
52
+ def all_models
53
+ # must eager load all the classes...
54
+ Dir.glob("#{RAILS_ROOT}/app/models/**/*.rb") do |model_path|
55
+ begin
56
+ require model_path
57
+ rescue
58
+ # ignore
59
+ end
60
+ end
61
+ # simply return them
62
+ ActiveRecord::Base.send(:subclasses)
63
+ #Dir.glob("#{Rails.root}/app/models/*.rb").map{|x| x.split("/").last.split(".").first.camelize}
64
+ end
65
+
66
+
67
+
68
+ def get_list_of_unique_models_with_db_table
69
+
70
+
71
+ table_names = ActiveRecord::Base.connection.tables #.map{|a| a.capitalize.singularize}
72
+ model_names = Dir["#{Rails.root}/app/models/**/*.rb"].map {|f| File.basename(f, '.*').pluralize}
73
+
74
+ #Rails.application.eager_load! unless Rails.configuration.cache_classes
75
+ #ActiveRecord::Base.descendants
76
+ modelswithtables = table_names & model_names
77
+
78
+ end
79
+
80
+
81
+ def create_fixture(table_name, sql, model)
82
+
83
+ File.open("#{fixtures_dir}#{table_name}.yml", "w") do |file|
84
+ objects = ActiveRecord::Base.connection.select_all(sql)
85
+ objects.each_with_index do |obj, i|
86
+ model.columns.each do |col|
87
+ if !col.null && obj[col.name].nil?
88
+ obj[col.name] = ''
89
+ end
90
+ end
91
+ file.write({"#{table_name}#{i}" => obj}.to_yaml.sub('---', ''))
92
+ file.write "\n"
93
+ end
94
+ end
95
+
96
+
97
+ end
27
98
 
28
- fixtures_dir = "#{Rails.root}/tmp/fixtures/"
29
- FileUtils.mkdir_p(fixtures_dir)
30
99
 
31
- table_names = ActiveRecord::Base.connection.tables #.map{|a| a.capitalize.singularize}
32
- model_names = Dir["#{Rails.root}/app/models/**/*.rb"].map {|f| File.basename(f, '.*').pluralize}
33
- modelswithtables = table_names & model_names
100
+ def create_all_fixtures
101
+
102
+ modelswithtables = get_list_of_unique_models_with_db_table
103
+
34
104
  modelswithtables.each do |table_name|
35
105
  model = table_name.classify.constantize
36
106
 
@@ -40,23 +110,45 @@ module FixtureMe
40
110
  else
41
111
  sql = "SELECT * FROM #{table_name}"
42
112
  end
43
- File.open("#{fixtures_dir}#{table_name}.yml", "w") do |file|
44
- objects = ActiveRecord::Base.connection.select_all(sql)
45
- objects.each_with_index do |obj, i|
46
- model.columns.each do |col|
47
- if !col.null && obj[col.name].nil?
48
- obj[col.name] = ''
49
- end
50
- end
51
- file.write({"#{table_name}#{i}" => obj}.to_yaml.sub('---', ''))
52
- file.write "\n"
53
- end
113
+
114
+ self.create_fixture(table_name, sql, model)
115
+
116
+ puts "extracted #{table_name}"
117
+ end
118
+
119
+ end
120
+
121
+
122
+
123
+
124
+ def create_all_fixtures_no_timestamps
125
+
126
+ modelswithtables = get_list_of_unique_models_with_db_table
127
+ exclude_columns = ['created_at', 'updated_at']
128
+
129
+
130
+ modelswithtables.each do |table_name|
131
+ model = table_name.classify.constantize
132
+ columns = model.attribute_names - exclude_columns
133
+
134
+ if model.columns.any?{|c| c.name == 'created_at'}
135
+ #sql = "SELECT * FROM #{table_name} ORDER BY created_at DESC"
136
+ sql = model.select(columns).order("created_at DESC").to_sql
137
+ else
138
+ #sql = "SELECT * FROM #{table_name}"
139
+ sql = model.select(columns).to_sql
54
140
  end
141
+
142
+ self.create_fixture(table_name, sql, model)
143
+
55
144
  puts "extracted #{table_name}"
56
145
  end
57
146
 
58
147
  end
59
148
 
149
+
150
+
151
+
60
152
  #FixtureMe::AddFixtures.create_all_fixtures
61
153
  end
62
154
 
@@ -64,3 +156,6 @@ module FixtureMe
64
156
 
65
157
  end
66
158
  ActiveRecord::Base.send(:include, FixtureMe)
159
+
160
+ #c = FixtureMe::AddFixtures.new
161
+ #c.create_all_fixtures
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixture_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - anthony de silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-20 00:00:00.000000000 Z
11
+ date: 2014-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,7 @@ files:
52
52
  - LICENSE.txt
53
53
  - README.md
54
54
  - Rakefile
55
+ - fixture_me-0.0.1.gem
55
56
  - fixture_me.gemspec
56
57
  - lib/.DS_Store
57
58
  - lib/fixture_me.rb