Class: AdminTheme::ResourceGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/admin_theme/resource/resource_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, *options) ⇒ ResourceGenerator

Returns a new instance of ResourceGenerator.



14
15
16
17
# File 'lib/generators/admin_theme/resource/resource_generator.rb', line 14

def initialize(args, *options)
  super(args, *options)
  initialize_views_variables
end

Instance Method Details

#generate_controllerObject



19
20
21
22
# File 'lib/generators/admin_theme/resource/resource_generator.rb', line 19

def generate_controller
  template "../controllers/admin_resource_controller.erb", "app/controllers/admin/#{plural_resource_name}_controller.rb"
  inject_into_file "config/routes.rb", "    resources :#{plural_resource_name}\n", :after => "namespace :admin do\n"
end

#generate_viewsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/admin_theme/resource/resource_generator.rb', line 24

def generate_views
  template "#{options.engine}/view_form.html.#{options.engine}",   "app/views/admin/#{plural_resource_name}/_form.html.#{options.engine}"
  template "#{options.engine}/view_edit.html.#{options.engine}",   "app/views/admin/#{plural_resource_name}/edit.html.#{options.engine}"
  template "#{options.engine}/view_index.html.#{options.engine}",  "app/views/admin/#{plural_resource_name}/index.html.#{options.engine}"
  template "#{options.engine}/view_new.html.#{options.engine}",    "app/views/admin/#{plural_resource_name}/new.html.#{options.engine}"
  template "#{options.engine}/view_show.html.#{options.engine}",   "app/views/admin/#{plural_resource_name}/show.html.#{options.engine}"

  # Add link to resource in admin layout
  gsub_file("app/views/layouts/admin.html.erb", /\<div\s+id=\"main-navigation\">.*\<\/ul\>/mi) do |match|
    match.gsub!(/\<\/ul\>/, "")
    %|#{match}  <li class="<%= controller_name == '#{plural_resource_name}' ? 'active' : '' %>">
      <%= link_to "#{plural_model_name}", #{controller_routing_path}_path %>
    </li>
  </ul>|
  end
end