aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/generators/erb/scaffold/scaffold_generator.rb
blob: 1841124ae466ffbad067ac06de136984aec13b1f (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'generators/erb'

module Erb
  module Generators
    class ScaffoldGenerator < Base
      include Rails::Generators::ScaffoldBase

      argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type"

      class_option :singleton, :type => :boolean, :desc => "Supply to skip index action"
      class_option :layout, :type => :boolean

      def create_root_folder
        empty_directory File.join("app/views", controller_file_path)
      end

      def copy_index_file
        return if options[:singleton]
        copy_view :index
      end

      def copy_edit_file
        copy_view :edit
      end

      def copy_show_file
        copy_view :show
      end

      def copy_new_file
        copy_view :new
      end

      def copy_layout_file
        return unless options[:layout]
        template "layout.html.erb",
                 File.join("app/views/layouts", controller_class_path, "#{controller_file_name}.html.erb")
      end

      protected

        def copy_view(view)
          template "#{view}.html.erb", File.join("app/views", controller_file_path, "#{view}.html.erb")
        end

    end
  end
end