File: application_layout_generator.rb

package info (click to toggle)
ruby-haml-rails 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 228 kB
  • sloc: ruby: 281; makefile: 4
file content (32 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download
1
2
3
4
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
require 'rails'
require 'shellwords'

module Haml
  module Generators
    class ApplicationLayoutGenerator < ::Rails::Generators::Base
      HTML_LAYOUT_PATH = 'app/views/layouts/application.html.erb'
      HAML_LAYOUT_PATH = 'app/views/layouts/application.html.haml'

      # Converts existing application.html.erb to haml format,
      # and creates app/views/layouts/application.html.haml
      # with some error checking.
      def convert
        app_layout_from = ::Rails.root.join(HTML_LAYOUT_PATH).to_s
        app_layout_to   = ::Rails.root.join(HAML_LAYOUT_PATH).to_s

        if File.exist?(app_layout_from)

          if !File.exist?(app_layout_to)
            `html2haml #{app_layout_from.shellescape} #{app_layout_to.shellescape}`
            puts "Success! app/views/layouts/application.html.haml is created.\n" \
                 "Please remove the erb file: app/views/layouts/application.html.erb"
          else
            puts "Error! There is a file named app/views/layouts/application.html.haml already."
          end
        else
          puts "Error! There is no file named app/views/layouts/application.html.erb."
        end
      end
    end
  end
end