Permalink
Browse files

Added new generator to install new templates

  • Loading branch information...
ashrafuzzaman committed Jun 15, 2012
1 parent 487cdf0 commit c8d4c87570f5acfd1ae278b0a6f3adf28febcfb4
View
@@ -9,4 +9,8 @@ Now it only supports erb template.
Note
===========================
Nested scaffold already exists for rails 3, still I had to build yet another because those were broken. The reason I did not forked from there because those plugin overides a lot of rails generator code. But as we know the rails code base is always changing. So the way I implement it is simple. First let rails generate what it usually does and then change some of the generated files. For me I changed the controller and the view files.
Nested scaffold already exists for rails 3, still I had to build yet another because those were broken. The reason I did not forked from there because those plugin overides a lot of rails generator code. But as we know the rails code base is always changing. So the way I implement it is simple. First let rails generate what it usually does and then change some of the generated files. For me I changed the controller and the view files.
References
===========================
* http://www.madebydna.com/all/code/2010/10/11/cooking-up-a-custom-rails3-template.html
@@ -23,7 +23,7 @@ def add_has_many
def add_in_routes
gsub_file 'config/routes.rb', Regexp.new("resources :#{plural_table_name}\n"), ''
gsub_file 'config/routes.rb', Regexp.new("resources :#{pr.plural_table_name}\n"), "resources :#{pr.plural_table_name} do\n end\n"
inject_into_file 'config/routes.rb', "\n resources :#{plural_table_name}\n", :after => "resources :#{pr.plural_table_name} do"
inject_into_file 'config/routes.rb', "\n resources :#{plural_table_name}", :after => "resources :#{pr.plural_table_name} do"
end
protected
@@ -0,0 +1,8 @@
Description:
Explain the generator
Example:
rails generate install Thing
This will create:
what/will/it/create
@@ -0,0 +1,31 @@
require 'rails/generators'
module Template
module Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def copy_view_templates
available_view_templates.each do |file|
file_with_ext = "erb/scaffold/#{file}.html.erb"
copy_file file_with_ext, "lib/templates/#{file_with_ext}"
end
end
def copy_controller_template
file_with_ext = "rails/scaffold_controller/controller.rb"
copy_file file_with_ext, "lib/templates/#{file_with_ext}"
end
def copy_helper_template
file_with_ext = "rails/helper/helper.rb"
copy_file file_with_ext, "lib/templates/#{file_with_ext}"
end
private
def available_view_templates
%w(show new edit _form index)
end
end
end
end
@@ -15,15 +15,15 @@
<legend><%%= <%= singular_table_name %>_form_title(@<%= singular_table_name %>) %></legend>
<% attributes.each do |attribute| -%>
<div class="control-group">
<%%= f.label :<%= attribute.name, class: 'control-label' %> %>
<%%= f.label :<%= attribute.name %>, class: 'control-label' %>
<div class="controls">
<%%= f.<%= attribute.field_type %> :<%= attribute.name %>, class: 'input-xlarge' %>
</div>
</div>
<% end -%>
</fieldset>
<div class="form-actions">
<%%= f.submit, class: 'btn btn-primary' %>
<%%= f.submit class: 'btn btn-primary' %>
<button class="btn">Cancel</button>
</div>
<%% end %>
@@ -7,8 +7,6 @@
<th><%= attribute.human_name %></th>
<% end -%>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
@@ -17,9 +15,11 @@
<% attributes.each do |attribute| -%>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
<td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_table_name %>, confirm: 'Are you sure?', method: :delete %></td>
<td>
<%%= link_to 'Show', <%= singular_table_name %>, class: 'btn btn-mini' %>
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class: 'btn btn-mini' %>
<%%= link_to 'Destroy', <%= singular_table_name %>, confirm: 'Are you sure?', method: :delete, class: 'btn btn-mini btn-danger' %>
</td>
<%% end %>
</tbody>
</table>
@@ -0,0 +1,17 @@
<p id="notice"><%%= notice %></p>
<table class="table table-bordered table-striped">
<tbody>
<% attributes.each do |attribute| -%>
<tr>
<td><strong><%= attribute.human_name %>:</strong></td>
<td><%%= @<%= singular_table_name %>.<%= attribute.name %> %></td>
</tr>
<% end -%>
</tbody>
</table>
<div class="form-actions">
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>), :class => 'btn btn-primary' %>
<%%= link_to 'Back', <%= index_helper %>_path, :class => 'btn' %>
</div>

This file was deleted.

Oops, something went wrong.

0 comments on commit c8d4c87

Please sign in to comment.