Class: Effective::FormUpgrader

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/form_upgrader.rb

Instance Method Summary collapse

Constructor Details

#initialize(folder: 'app/views/') ⇒ FormUpgrader

Returns a new instance of FormUpgrader.



5
6
7
# File 'app/models/effective/form_upgrader.rb', line 5

def initialize(folder: 'app/views/')
  @folders = Array(folder)
end

Instance Method Details

#upgrade!Object



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
# File 'app/models/effective/form_upgrader.rb', line 9

def upgrade!
  @folders.each do |folder|
    Dir.glob(folder + '**/*').each do |path|
      next if File.directory?(path)
      next unless path.include?('.html')

      writer = Effective::CodeWriter.new(path)

      name = path.split('/')[0...-1] - ['app', 'views']
      resource = Effective::Resource.new(name)

      if writer.find { |line| line.include?('simple_form_for') }
        upgrade_simple_form(writer, resource)
      elsif writer.find { |line| line.include?('semantic_form_for') }
        upgrade_formtastic(writer, resource)
      elsif writer.find { |line| line.include?('form_for') }
        upgrade_form_for(writer, resource)
      else
        next # Nothing to do
      end

      writer.write!
    end
  end

  puts 'All Done. Have a great day.'
  true
end