Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit ddf63a16d86186efb198e63de308011a9325e753 0 parents
@huacnlee authored
3  README.textilte
@@ -0,0 +1,3 @@
+这个是我对 Rails 默认模板的定制,模板乃是个人喜好,不一定适合你,仅供查考。
+
+
29 rails/generators/erb/scaffold/scaffold_generator.rb
@@ -0,0 +1,29 @@
+require 'rails/generators/erb'
+require 'rails/generators/resource_helpers'
+
+module Erb
+ module Generators
+ class ScaffoldGenerator < Base
+ include Rails::Generators::ResourceHelpers
+
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
+ def create_root_folder
+ empty_directory File.join("app/views", controller_file_path)
+ end
+
+ def copy_view_files
+ available_views.each do |view|
+ filename = filename_with_extensions(view)
+ template filename, File.join("app/views", controller_file_path, filename)
+ end
+ end
+
+ protected
+
+ def available_views
+ %w(index edit show new _form _base)
+ end
+ end
+ end
+end
5 templates/erb/scaffold/_base.html.erb
@@ -0,0 +1,5 @@
+<%%= content_for :base_sitemap do %>
+<a href="<%%= <%= plural_table_name %>_path %>"><%= plural_table_name %></a> »
+<%% end %>
+<%%= content_for :styles do %>
+<%% end %>
14 templates/erb/scaffold/_form.html.erb
@@ -0,0 +1,14 @@
+<div id="<%= singular_table_name %>" class="form">
+<%%= simple_form_for(@<%= file_name %>,
+ :url => (@<%= file_name %>.id.blank? ? <%= index_helper %>_path : <%= singular_table_name %>_path(@<%= file_name %>)) ) do |f| %>
+
+ <div class="inputs">
+<% for attribute in attributes -%>
+ <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
+<% end -%>
+ </div>
+ <div class="actions">
+ <%%= f.submit "提交", :class => "button" %> 或者 <%%= link_to '返回', <%= index_helper %>_path %>
+ </div>
+<%% end %>
+</div>
6 templates/erb/scaffold/edit.html.erb
@@ -0,0 +1,6 @@
+<%%= content_for :sitemap do %>
+<span class="current">修改</span>
+<%% end %>
+<%%= render 'base' %>
+<%%= render 'form' %>
+
24 templates/erb/scaffold/index.html.erb
@@ -0,0 +1,24 @@
+<%%= content_for :sitemap do %>
+<span class="current">列表</span>
+<%% end %>
+<%%= render 'base' %>
+<div class="tools">
+ <a href="<%%= <%= plural_table_name %>_path %>" class="button">查看所有</a>
+ <a href="<%%= new_<%= singular_table_name %>_path %>" class="button">新建</a>
+</div>
+
+<div id="<%= plural_table_name %>">
+ <%%= grid(@<%= plural_file_name %>, :show_filters => :always) do |g|
+ g.column :column_name => '', :attribute_name => 'id', :no_filter => true
+<% for attribute in attributes -%>
+ g.column :column_name => '<%= attribute.name %>', :attribute_name => '<%= attribute.name %>'
+<% end -%>
+ g.column :column_name => '创建时间', :attribute_name => 'created_at'
+ g.column :column_name => '操作' do |c|
+ raw "#{link_to '修改', edit_<%= singular_table_name %>_path(c.id)}
+ #{link_to '删除', <%= singular_table_name %>_path(c.id), :method => :delete, :confirm => '确定要删除吗?'}"
+ end
+ end %>
+</div>
+
+
5 templates/erb/scaffold/new.html.erb
@@ -0,0 +1,5 @@
+<%%= content_for :sitemap do %>
+<span class="current">新建</span>
+<%% end %>
+<%%= render 'base' %>
+<%%= render 'form' %>
16 templates/erb/scaffold/show.html.erb
@@ -0,0 +1,16 @@
+<%%= content_for :sitemap do %>
+<span class="current">查看</span>
+<%% end %>
+<%%= render 'base' %>
+<div class="tools">
+ <%%= link_to '修改', edit_<%= singular_table_name %>_path(@<%= file_name %>) %> |
+ <%%= link_to '返回', <%= index_helper %>_path %>
+</div>
+<div id="<%= singular_table_name %>">
+ <% for attribute in attributes -%>
+ <p>
+ <b><%= attribute.human_name %>:</b>
+ <%%= @<%= file_name %>.<%= attribute.name %> %>
+ </p>
+ <% end -%>
+</div>
74 templates/rails/scaffold_controller/controller.rb
@@ -0,0 +1,74 @@
+# coding: UTF-8
+class <%= controller_class_name %>Controller < ApplicationController
+
+ def index
+ @<%= plural_file_name %> = initialize_grid(<%= file_name.camelize %>,
+ :order => 'id',
+ :order_direction => 'desc')
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json
+ end
+ end
+
+ def show
+ @<%= file_name %> = <%= orm_class.find(file_name.camelize, "params[:id]") %>
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json
+ end
+ end
+
+ def new
+ @<%= file_name %> = <%= orm_class.build(file_name.camelize) %>
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json
+ end
+ end
+
+ def edit
+ @<%= file_name %> = <%= orm_class.find(file_name.camelize, "params[:id]") %>
+ end
+
+ def create
+ @<%= file_name %> = <%= orm_class.build(file_name.camelize, "params[:#{file_name}]") %>
+
+ respond_to do |format|
+ if @<%= file_name %>.save
+ format.html { redirect_to(<%= index_helper %>_path, :notice => '<%= human_name %> 创建成功。') }
+ format.json
+ else
+ format.html { render :action => "new" }
+ format.json
+ end
+ end
+ end
+
+ def update
+ @<%= file_name %> = <%= orm_class.find(file_name.camelize, "params[:id]") %>
+
+ respond_to do |format|
+ if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
+ format.html { redirect_to(<%= index_helper %>_path, :notice => '<%= human_name %> 更新成功。') }
+ format.json
+ else
+ format.html { render :action => "edit" }
+ format.json
+ end
+ end
+ end
+
+ def destroy
+ @<%= file_name %> = <%= orm_class.find(file_name.camelize, "params[:id]") %>
+ @<%= file_name %>.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(<%= index_helper %>_path,:notice => "删除成功。") }
+ format.json
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.