Class: Haml::Generators::ControllerScaffoldingGenerator

Inherits:
Erb::Generators::ControllerGenerator
  • Object
show all
Defined in:
lib/generators/haml/controller/controller_scaffolding_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_stylesheetObject



82
83
84
85
86
87
88
89
# File 'lib/generators/haml/controller/controller_scaffolding_generator.rb', line 82

def copy_stylesheet
  if options.ext_form_submit? || options.ext_index_nav?
    source_paths << File.expand_path('../../../../generators/assets/stylesheets', __FILE__)
    base_path = "app/assets/stylesheets"
    path = File.join(base_path, 'controller_scaffolding.css.scss')
    copy_file('controller_scaffolding.css.scss', path) if file_action(path)
  end
end

#copy_view_filesObject

This method seems to always get run first



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/haml/controller/controller_scaffolding_generator.rb', line 21

def copy_view_files #do NOT change the name of this method 
                    # it must be overriding an existing one in a parent class
  base_path = File.join("app/views", class_path, file_name)
  empty_directory base_path
  @actions = actions.nil? || actions.empty? ? %w(index new create edit update destroy) : actions
  @attr_cols = ::Rails::Generators::attr_cols(table_name)
  @col_count = @attr_cols.count
  @col_count += 1 if @actions.include?("edit")
  @col_count += 1 if @actions.include?("destroy")
  @search_sort = options.search_sort?
  (@actions - %w(create update destroy)).each do |action|
    @action = action
    formats.each do |format|
      @path = File.join(base_path, filename_with_extensions(action, format))
      set_template(@action, @path)
    end
  end
end

#gen_form_partialObject



40
41
42
43
44
45
46
# File 'lib/generators/haml/controller/controller_scaffolding_generator.rb', line 40

def gen_form_partial
  base_path = File.join("app/views", class_path, file_name)
  unless (@actions & %w(edit new)).empty? #Remember that "&" is Array#intersect
    @path = File.join(base_path, filename_with_extensions("_form", format))
    set_template("_form", @path) 
  end
end

#handle_ext_formObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/haml/controller/controller_scaffolding_generator.rb', line 65

def handle_ext_form
  if options.ext_form_submit?
    copy_controller_concern("ext_form_submit.rb")

    inject_into_file "app/controllers/application_controller.rb", 
          after: "class ApplicationController < ActionController::Base\n" do
            "\n\tinclude ExtFormSubmit\n"
          end
    copy_partial("_flash_messages")
    inject_into_file "app/views/layouts/application.html.erb", 
          before: "<%= yield %>\n" do
            "\n<%= render 'flash_messages' %>\n"
          end
    copy_partial("_validation_errors")
  end
end

#handle_ext_indexObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/haml/controller/controller_scaffolding_generator.rb', line 48

def handle_ext_index
  if options.ext_index_nav?
    copy_controller_concern("ext_index_nav.rb")
    inject_into_file "app/controllers/application_controller.rb", 
          after: "class ApplicationController < ActionController::Base\n" do
            "\n\tinclude ExtIndexNav\n"
          end
    copy_partial("_pagination")
    add_pagination_to_locale_file
    copy_ext_index_js
    inject_into_file "app/assets/javascripts/application.js",
      before: "\n//= require_tree ." do
        "\n//= require jquery\n//= require jquery_ujs"
      end
  end
end

#handle_search_n_sortObject

This is the code that add SnS functionality to the model specified in the controller generator



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/haml/controller/controller_scaffolding_generator.rb', line 92

def handle_search_n_sort
  if @search_sort
    inject_into_file "app/models/#{table_name.singularize}.rb",
      before: /^end/ do
        "\n\textend SqlSearchableSortable\n"
      end
    inject_into_file "app/models/#{table_name.singularize}.rb",
      before: /^end/ do
        "\n\tsql_searchable #{cols_to_symbols}\n" 
      end
    inject_into_file "app/models/#{table_name.singularize}.rb",
      before: /^end/ do
        "\n\tsql_sortable #{cols_to_symbols}\n"
      end
  end

end