Class: ExtjsScaffold::Generators::ScaffoldControllerGenerator

Inherits:
Base
  • Object
show all
Includes:
Rails::Generators::ResourceHelpers
Defined in:
lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb

Instance Method Summary collapse

Methods inherited from Base

rails_app_name, source_root

Instance Method Details

#add_resource_routeObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 99

def add_resource_route
  return unless options[:routes]
  route_config =  class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ")
  route_config << "resources :#{file_name.pluralize} do \n"
  route_config << "    collection do \n"
  route_config << "      post :destroy_all \n"
  route_config << "      post :update_all \n"
  route_config << "    end\n"
  route_config << "  end"
  route_config << " end" * class_path.size
  route route_config
end

#copy_js_filesObject

copy over controller js files



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 62

def copy_js_files
  available_js.each do |name|
    filename = [name, :js].compact.join(".")
    case name
    when 'Controller'
      template "js/#{filename}", File.join("app/assets/javascripts/controller", "#{plural_table_name.capitalize}.js")  
    when 'Model'
      template "js/#{filename}", File.join("app/assets/javascripts/model", "#{singular_table_name.capitalize}.js")
    when 'Store'
      template "js/#{filename}", File.join("app/assets/javascripts/store", "#{plural_table_name.capitalize}.js")
    else
      template "js/#{filename}", File.join("app/assets/javascripts/view", singular_table_name, filename)
    end
  end
end

#copy_test_filesObject



126
127
128
129
130
131
132
133
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 126

def copy_test_files
  case options[:test_framework]
  when :rspec, 'rspec'
    template "tests/controller_spec.rb", File.join("spec/controllers", "#{controller_file_name}_controller_spec.rb")
  when :test_unit, 'test_unit'
    template "tests/controller_test.rb", File.join("test/functional", "#{controller_file_name}_controller_test.rb")
  end
end

#copy_view_filesObject

copy view templates or hook to :template_engine



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 114

def copy_view_files
  return unless options[:views]
  empty_directory File.join("app/views", controller_file_path)
  # accept haml or default to erb
  template = options[:template_engine] == 'haml' ? options[:template_engine] : 'erb'
  
  available_views.each do |view|
    filename = filename_with_extensions(view, :html, template)
    template "views/#{template}/#{filename}", File.join("app/views", controller_file_path, filename)
  end
end

#create_controller_filesObject



47
48
49
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 47

def create_controller_files
  template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end

#create_js_root_folderObject

create Extjs MVC structure



52
53
54
55
56
57
58
59
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 52

def create_js_root_folder
  empty_directory File.join("app/assets/javascripts", "controller")
  empty_directory File.join("app/assets/javascripts", "model")
  empty_directory File.join("app/assets/javascripts", "store")
  empty_directory File.join("app/assets/javascripts", "view")
  # create Extjs controller view folder
  empty_directory File.join("app/assets/javascripts/view", singular_table_name)
end

#create_model_methodsObject

add class method ‘search’ to model



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 27

def create_model_methods
  @pagination = options.pagination
  if File.exists?("#{destination_root}/app/models/#{singular_table_name}.rb")
    template "model.rb", "app/models/#{controller_file_name}_tmp.rb"
    f = File.open "#{destination_root}/app/models/#{controller_file_name}_tmp.rb", "r"
    model_methods = f.read
    # make related tables updateable - add to attr_accessible
    refs = reference_attributes.map{|a| ":#{a.name}_id, :#{a.name}_#{reference_field(a)}"}
    if refs.size > 0
      refs_attr = refs.sort.join(", ")
      insert_into_file "app/models/#{singular_table_name}.rb", ", "+refs_attr, :after => /attr_accessible.*/
    end
    # add search with pagination class method
    insert_into_file "app/models/#{singular_table_name}.rb", "\n"+model_methods, :after => /attr_accessible.*/
    remove_file "app/models/#{controller_file_name}_tmp.rb"
  end
end

#create_reference_storesObject

create stores for any reference lookup combos



79
80
81
82
83
84
85
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 79

def create_reference_stores
  attributes.select {|attr| attr.reference? }.each do |attribute|
    @reference_attribute = attribute
    filename = [reference_store, :js].compact.join(".")
    template "js/#{filename}", File.join("app/assets/javascripts/store", "#{singular_table_name.capitalize}#{attribute.name.capitalize.pluralize}.js")
  end
end

#update_application_jsObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/extjs_scaffold/scaffold_controller/scaffold_controller_generator.rb', line 87

def update_application_js
  app_init = "\n"
  app_init << "      // #{plural_table_name.capitalize}: Initialize controller and create list grid \n"
  app_init << "      if (undefined != Ext.get('#{plural_table_name}_list')) { \n"
  app_init << "        var controller = this.getController('#{plural_table_name.capitalize}');\n"
  app_init << "        controller.init();\n"
  app_init << "        Ext.create('#{app_name}.view.#{singular_table_name}.Grid',{renderTo: Ext.getBody() });\n"
  app_init << "      }\n"
  
  insert_into_file "app/assets/javascripts/#{app_file_name}", app_init, :after => "launch: function() {"
end