Permalink
Browse files

Now used regex to replace in the current files to create the nested s…

…caffold. The advantage is that if I change the scaffold template then, it sould support that.
  • Loading branch information...
ashrafuzzaman committed Jun 15, 2012
1 parent 5ee2167 commit 98d42b80dc108bf1d0b8a294ea937e0afb1cad20
@@ -8,14 +8,6 @@ def create_controller_files
template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end
def copy_view_files
template_eng = Rails.application.config.generators.options[:rails][:template_engine] || 'erb'
available_views.each do |view|
filename = "#{view}.html.erb"
template "#{template_eng}/#{filename}", File.join("app/views", plural_table_name, filename)
end
end
def add_has_many
inject_into_class "app/models/#{pr.singular_table_name}.rb", pr.class_name, " has_many :#{plural_table_name}\n"
end
@@ -26,8 +18,70 @@ def add_in_routes
inject_into_file 'config/routes.rb', "\n resources :#{plural_table_name}", :after => "resources :#{pr.plural_table_name} do"
end
def modify_view_files
template_eng = Rails.application.config.generators.options[:rails][:template_engine] || 'erb'
modify_edit_view(plural_table_name)
modify_form(plural_table_name)
modify_index_view(plural_table_name)
modify_show_view(plural_table_name)
modify_new_view(plural_table_name)
end
protected
def modify_resource_link(plural_table_name, file_name)
gsub_file File.join("app/views", plural_table_name, file_name),
Regexp.new("(<%= link_to.*,\s*)(@?#{singular_table_name})(\s*,.*%>|\s*%>)"),
"\\1[@#{pr.singular_table_name}, \\2]\\3"
end
def modify_edit_link(plural_table_name, file_name)
gsub_file File.join("app/views", plural_table_name, file_name),
Regexp.new("edit_#{singular_table_name}_path\\((@?#{singular_table_name})\\)"),
"edit_#{singular_path}(@#{pr.singular_table_name}, \\1)"
end
def modify_index_link(plural_table_name, file_name)
gsub_file File.join("app/views", plural_table_name, file_name),
Regexp.new("#{index_helper}_path"),
index_path
end
def modify_edit_view(plural_table_name)
file_name = "edit.html.erb"
modify_resource_link(plural_table_name, file_name)
modify_index_link(plural_table_name, file_name)
end
def modify_form(plural_table_name)
file_name = "_form.html.erb"
gsub_file File.join("app/views", plural_table_name, file_name),
Regexp.new("(form_for.*)@#{singular_table_name}(.*do)"),
"\\1#{resource_path}\\2"
modify_index_link(plural_table_name, file_name)
end
def modify_index_view(plural_table_name)
file_name = "index.html.erb"
modify_resource_link(plural_table_name, file_name)
modify_edit_link(plural_table_name, file_name)
gsub_file File.join("app/views", plural_table_name, file_name),
Regexp.new("new_#{singular_table_name}_path"),
"new_#{singular_path}(@#{pr.singular_table_name})"
end
def modify_show_view(plural_table_name)
file_name = "show.html.erb"
modify_edit_link(plural_table_name, file_name)
modify_index_link(plural_table_name, file_name)
end
def modify_new_view(plural_table_name)
file_name = "new.html.erb"
modify_index_link(plural_table_name, file_name)
end
def singular_path
"#{pr.singular_table_name}_#{singular_table_name}_path"
end
@@ -40,10 +94,6 @@ def resource_path
"[@#{pr.singular_table_name}, @#{singular_table_name}]"
end
def available_views
%w(index show new edit _form)
end
def parse_attributes!
parent_ar = self.attributes.to_s.scan(/(\w*):references/)[0]
if parent_ar

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -14,16 +14,18 @@
<fieldset>
<legend><%%= <%= singular_table_name %>_form_title(@<%= singular_table_name %>) %></legend>
<% attributes.each do |attribute| -%>
<% unless attribute.reference? -%>
<div class="control-group">
<%%= f.label :<%= attribute.name %>, class: 'control-label' %>
<div class="controls">
<%%= f.<%= attribute.field_type %> :<%= attribute.name %>, class: 'input-xlarge' %>
</div>
</div>
<% end -%>
<% end -%>
</fieldset>
<div class="form-actions">
<%%= f.submit class: 'btn btn-primary' %>
<button class="btn">Cancel</button>
<%%= link_to 'Back', <%= index_helper %>_path, :class => 'btn' %>
</div>
<%% end %>

0 comments on commit 98d42b8

Please sign in to comment.