spec_producer 0.6.0 → 0.7.0

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: 6e7e34624e59e659c2379b4dae35d243354ed18e
4
- data.tar.gz: 18a8a15034a4c863e8f87df51fb30e623e1b4a63
3
+ metadata.gz: 6287d80a0642fd78e395e29b0f52eca02410f7ec
4
+ data.tar.gz: d706c09fd16b42352087c136c99b66bc251241d8
5
5
  SHA512:
6
- metadata.gz: 6a2b462c791110af7912cbbf9793b347c967d09dc3a458cb38e78a0f4a9091a8ffdc9697aa91551567e269bc98a6cf1e4546b68f9b90efbec7a41c8a60145447
7
- data.tar.gz: 01e8f75d840c1c9f3928b208d3c018370f77db0cf305eee3ff76f4b9bd98c587f2e4b0d92f559817e69593f3bb8528aac149f494ac13de7e1443b9a19841b761
6
+ metadata.gz: 912f08bdf8a058aedcf38dadd43211ebd1581ccf97437f46384a68cea5b472a54d9f3bde3211fa50c7ae8d0c4e5f35e62a617b76f712375d4a759e9c3521bf75
7
+ data.tar.gz: 95d194ab23ab362feeaa2ed75c2c126aee4ac255eb24d0df14da98fac9021617efa49e23d4da796d71c591fb041b42a517f0358eafa277e8cc36f8d5f02d7cbb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.7.0 (2016-08-10)
2
+
3
+ Features:
4
+ * Producer now tries to produce all factories for the project that uses it
5
+
1
6
  ## 0.6.0 (2016-08-09)
2
7
 
3
8
  Features:
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
- when :belongs_to then " it { should belong_to(:#{key})#{produce_association_options(reflection)} }\n"
80
- when :has_one then " it { should have_one(:#{key})#{produce_association_options(reflection)} }\n"
81
- when :has_many then " it { should have_many(:#{key})#{produce_association_options(reflection)} }\n"
82
- when :has_and_belongs_to_many then " it { should have_and_belong_to_many(:#{key})#{produce_association_options(reflection)} }\n"
83
- end
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
- when :inverse_of then "inverse_of(:#{value})"
395
- when :autosave then "autosave(#{value})"
396
- when :through then "through(:#{value})"
397
- when :class_name then "class_name('#{value}')"
398
- when :foreign_key then "with_foreign_key('#{value}')"
399
- when :primary_key then "with_primary_key('#{value}')"
400
- when :source then "source(:#{value})"
401
- when :dependent then "dependent(:#{value})"
402
- end
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
@@ -1,3 +1,3 @@
1
1
  module SpecProducer
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spec_producer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasilis Kalligas