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



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

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



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

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)
  #binding.pry
  empty_directory base_path
  @actions = actions.nil? || actions.empty? ? %w(index new create edit update destroy) : actions
  @attr_cols = GeneratorUtils::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



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

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



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

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



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

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



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

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



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

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

end


116
117
118
119
120
121
# File 'lib/generators/controller_generator_base.rb', line 116

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