Method: ConvertTheme#apply_to

Defined in:
lib/convert_theme.rb

#apply_to(rails_path, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/convert_theme.rb', line 27

def apply_to(rails_path, options = {})
  @rails_path = rails_path
  @template_type = (options[:template_type] || detect_template).to_s

  File.open(File.join(template_temp_path, 'app/views/layouts/application.html.erb'), "w") do |file|
    index_file = File.read(File.join(template_root, index_path)).gsub(/\r/, '')
    doc = Hpricot(index_file)
    doc.search("##{content_id}").each do |div|
      div.inner_html = "<%= yield %>"
    end
    contents = doc.to_html
    contents.gsub!(%r{(["'])/?#{image_dir}}, '\1/images')
    contents.gsub!(%r{(["'])/?#{stylesheet_dir}}, '\1/stylesheets')
    contents.gsub!(%r{(["'])/?#{javascript_dir}}, '\1/javascripts')
    file << contents
  end
  
  template_stylesheets.each do |file|
    File.open(File.join(template_temp_path, 'public/stylesheets', File.basename(file)), "w") do |f|
      contents = File.read(file)
      contents.gsub!(%r{url\((["']?)[./]*#{image_dir}}, 'url(\1/images')
      f << contents
    end
  end
  template_javascripts.each do |file|
    FileUtils.cp_r(file, File.join(template_temp_path, 'public/javascripts'))
  end
  template_images.each do |file|
    FileUtils.cp_r(file, File.join(template_temp_path, 'public/images'))
  end
  
  # now use rubigen to install the files into the rails app
  # so users can get conflict resolution options from command line
  RubiGen::Base.reset_sources
  RubiGen::Base.prepend_sources(RubiGen::PathSource.new(:internal, File.dirname(__FILE__)))
  generator_options = options[:generator] || {}
  generator_options.merge!(:stdout => @stdout, :no_exit => true,
    :source => template_temp_path, :destination => rails_path)
  RubiGen::Scripts::Generate.new.run(["convert_theme"], generator_options)
end