5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/spec_filler.rb', line 5
def fill_factory(column, factory_name='')
class_name = column.split('_').map(&:capitalize).join
line_array = []
next_line = false
File.foreach('db/schema.rb') do |line|
if line.chomp.empty? && next_line == true
break
end
if next_line
line_array << line
end
if line.include?(column)
line_array << line unless line.include?('add_index')
next_line = true
end
end
File.open("spec/factories/#{column}.rb", 'w') do |file|
unless factory_name.empty?
file.print("FactoryGirl.define do\n\tfactory :#{factory_name}, class: #{class_name} do\n")
else
file.print("FactoryGirl.define do\n\tfactory :#{column} do\n")
end
if line_array[0].include?('primary_key')
file.print("\t\t# It looks like your primary key isn't the record ID.\n")
file.print("\t\t# Don't forget to pass in your primary key when you're creating\n")
file.print("\t\t# the test object (e.g., create(:object, primary_key: column_name)).\n")
end
line_array.shift
get_attributes(line_array).each do |attr|
file.print "\t\t#{attr}\n"
end
file.print("\tend\n")
file.print('end')
end
end
|