Class: Generators::ControllerGeneratorBase

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

Instance Method Summary collapse

Instance Method Details

#copy_stylesheetObject



86
87
88
89
90
91
92
93
# File 'lib/generators/controller_generator_base.rb', line 86

def copy_stylesheet
  if options.ext_form_submit? || options.ext_index_nav?
    source_paths << File.expand_path('../../../lib/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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/controller_generator_base.rb', line 14

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



33
34
35
36
37
38
39
# File 'lib/generators/controller_generator_base.rb', line 33

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_datepickerObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/generators/controller_generator_base.rb', line 72

def handle_datepicker
  if options.datepicker?
    inc_jquery_scripts
    inject_into_file "app/assets/javascripts/application.js",
      after: "\n//= require_tree ." do
        "\n//= require hot_date_rails"
      end
    inject_into_file "app/assets/stylesheets/application.css",
      before: "\n *= require_tree ." do
        "\n *= require hot_date_rails"
      end
  end
end

#handle_ext_formObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/controller_generator_base.rb', line 55

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



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/controller_generator_base.rb', line 41

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
    inc_jquery_scripts
  end
end

#handle_search_n_sortObject

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/generators/controller_generator_base.rb', line 96

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 #{searchable_cols_as_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


114
115
116
117
118
119
# File 'lib/generators/controller_generator_base.rb', line 114

def print_warnings
  if @unsearchable_model && behavior == :invoke && !options.quiet?
    warn("WARNING: Model #{table_name.classify} is extending SqlSearchableSortable," \
      " but doesn't have any searchable attributes at this point.") 
  end
end