spec_producer 0.6.0 → 0.7.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/spec_producer.rb +73 -14
- data/lib/spec_producer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6287d80a0642fd78e395e29b0f52eca02410f7ec
|
4
|
+
data.tar.gz: d706c09fd16b42352087c136c99b66bc251241d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 912f08bdf8a058aedcf38dadd43211ebd1581ccf97437f46384a68cea5b472a54d9f3bde3211fa50c7ae8d0c4e5f35e62a617b76f712375d4a759e9c3521bf75
|
7
|
+
data.tar.gz: 95d194ab23ab362feeaa2ed75c2c126aee4ac255eb24d0df14da98fac9021617efa49e23d4da796d71c591fb041b42a517f0358eafa277e8cc36f8d5f02d7cbb
|
data/CHANGELOG.md
CHANGED
data/lib/spec_producer.rb
CHANGED
@@ -9,6 +9,65 @@ module SpecProducer
|
|
9
9
|
produce_specs_for_helpers
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.produce_factories
|
13
|
+
Dir.glob(Rails.root.join('app/models/*.rb')).each do |x|
|
14
|
+
require x
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Base.descendants.each do |descendant|
|
18
|
+
final_text = "FactoryGirl.define do\n"
|
19
|
+
final_text << " factory :#{descendant.name.underscore}, :class => #{descendant.name} do\n"
|
20
|
+
|
21
|
+
descendant.columns.each do |column|
|
22
|
+
value = case column.type
|
23
|
+
when :string then "'#{descendant.name.underscore.upcase}_#{column.name.underscore.upcase}'"
|
24
|
+
when :text then "'#{descendant.name.underscore.upcase}_#{column.name.underscore.upcase}'"
|
25
|
+
when :integer then "#{rand(1...10)}"
|
26
|
+
when :decimal then "#{rand(1.0...100.0)}"
|
27
|
+
when :float then "#{rand(1.0...100.0)}"
|
28
|
+
when :datetime then "'#{DateTime.now + 365}'"
|
29
|
+
when :time then "'#{Time.now + 365*24*60*60}'"
|
30
|
+
when :date then "'#{Date.today + 365}'"
|
31
|
+
when :boolean then "#{rand(2) == 1 ? true : false}"
|
32
|
+
when :binary then "#{5.times.collect { rand(0..1).to_s }.join('')}"
|
33
|
+
end
|
34
|
+
|
35
|
+
final_text << " #{column.name} #{value}\n"
|
36
|
+
end
|
37
|
+
|
38
|
+
final_text << " end\n"
|
39
|
+
final_text << "end"
|
40
|
+
|
41
|
+
if File.exists?(Rails.root.join("spec/factories/#{descendant.name.underscore}.rb"))
|
42
|
+
puts '#'*100
|
43
|
+
puts "Please, check whether the following lines are included in: spec/factories/" + descendant.name.underscore + ".rb\n"
|
44
|
+
puts '#'*100
|
45
|
+
puts "\n"
|
46
|
+
puts final_text
|
47
|
+
else
|
48
|
+
unless Dir.exists? Rails.root.join("spec")
|
49
|
+
puts "Generating spec directory"
|
50
|
+
Dir.mkdir(Rails.root.join("spec"))
|
51
|
+
end
|
52
|
+
|
53
|
+
unless Dir.exists? Rails.root.join("spec/factories")
|
54
|
+
puts "Generating spec/factories directory"
|
55
|
+
Dir.mkdir(Rails.root.join("spec/factories"))
|
56
|
+
end
|
57
|
+
|
58
|
+
path = "spec/factories/#{descendant.name.underscore}.rb"
|
59
|
+
puts "Producing factory file for: #{path}"
|
60
|
+
f = File.open("#{Rails.root.join(path)}", 'wb+')
|
61
|
+
f.write(final_text)
|
62
|
+
f.close
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
nil
|
67
|
+
rescue NameError
|
68
|
+
puts "ActiveRecord is not set for this project. Can't produce factories for this project."
|
69
|
+
end
|
70
|
+
|
12
71
|
def self.produce_specs_for_models
|
13
72
|
Dir.glob(Rails.root.join('app/models/*.rb')).each do |x|
|
14
73
|
require x
|
@@ -76,11 +135,11 @@ module SpecProducer
|
|
76
135
|
|
77
136
|
descendant.reflections.each_pair do |key, reflection|
|
78
137
|
final_text << case reflection.macro
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
138
|
+
when :belongs_to then " it { should belong_to(:#{key})#{produce_association_options(reflection)} }\n"
|
139
|
+
when :has_one then " it { should have_one(:#{key})#{produce_association_options(reflection)} }\n"
|
140
|
+
when :has_many then " it { should have_many(:#{key})#{produce_association_options(reflection)} }\n"
|
141
|
+
when :has_and_belongs_to_many then " it { should have_and_belong_to_many(:#{key})#{produce_association_options(reflection)} }\n"
|
142
|
+
end
|
84
143
|
end
|
85
144
|
|
86
145
|
final_text << "end"
|
@@ -391,15 +450,15 @@ module SpecProducer
|
|
391
450
|
|
392
451
|
reflection.options.each_pair do |key, value|
|
393
452
|
final_text << case key
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
453
|
+
when :inverse_of then "inverse_of(:#{value})"
|
454
|
+
when :autosave then "autosave(#{value})"
|
455
|
+
when :through then "through(:#{value})"
|
456
|
+
when :class_name then "class_name('#{value}')"
|
457
|
+
when :foreign_key then "with_foreign_key('#{value}')"
|
458
|
+
when :primary_key then "with_primary_key('#{value}')"
|
459
|
+
when :source then "source(:#{value})"
|
460
|
+
when :dependent then "dependent(:#{value})"
|
461
|
+
end
|
403
462
|
end
|
404
463
|
final_text.reject(&:nil?).join('.').prepend('.')
|
405
464
|
end
|