Class: FbFiller::Filler

Inherits:
Object
  • Object
show all
Defined in:
lib/fb_filler.rb

Overview

# Your code goes here…

Class Method Summary collapse

Class Method Details

.fill_factory(column, factory_name = '') ⇒ Object



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
43
44
45
# File 'lib/fb_filler.rb', line 8

def self.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

.get_attributes(ary) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/fb_filler.rb', line 47

def self.get_attributes(ary)
  stripped_ary = []
  ary.each do |char|
    stripped_ary << char.scan(/"([^"]*)"/)
  end
  stripped_ary.flatten
end