Railsのscaffold generatorをカスタマイズ


**自分用メモ**


やりたいこと
----------
+ bootstrap用
+ breadcrumbsつける
+ i18nを参照してるのをgenerateしたい
+ noticeメッセージも辞書ファイル見てほしい
+ coffeescriptはgenerateしたくない



手順　
----------

### lib/templates 配下にテンプレートを作る

```
$ rake rails:templates:copy
```

### application.rb
```
config.generators.javascripts = false
```

### usual.ja.yml
```usual.ja.yml
ja:
  usual:
    index: 一覧
    show: 詳細
    new: 新規
    edit: 編集
    delete: 削除
    confirm:
      delete: 削除しますが、よろしいですか？
    back: 戻る
    create:
      success: "%{name}を作成しました"
      failed: 作成に失敗しました
    update:
      success: "%{name}を更新しました"
      failed: 更新に失敗しました
    destroy:
      success: "%{name}を削除しました"
      failed: 削除に失敗しました
    form:
      error_explanation:
        title: "%{count}件のエラーがあります"
```

### controller

```controller.rb
<% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"

<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
  before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
  before_action :set_breadcrumbs


  def index
    @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
  end


  def show
  end


  def new
    @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
  end


  def edit
  end


  def create
    @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>

    if @<%= orm_instance.save %>
      redirect_to @<%= singular_table_name %>, notice: t('usual.create.success', name: <%= "'#{human_name}'" %>)
    else
      render :new
    end
  end


  def update
    if @<%= orm_instance.update("#{singular_table_name}_params") %>
      redirect_to @<%= singular_table_name %>, notice: t('usual.update.success', name: <%= "'#{human_name}'" %>)
    else
      render :edit
    end
  end


  def destroy
    @<%= orm_instance.destroy %>
    redirect_to <%= index_helper %>_url, notice: t('usual.destroy.success', name: <%= "'#{human_name}'" %>)
  end


private

  def set_<%= singular_table_name %>
    @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
  end


  def set_breadcrumbs
    add_breadcrumb <%= class_name %>.model_name.human
  end


  def <%= "#{singular_table_name}_params" %>
    <%- if attributes_names.empty? -%>
    params[:<%= singular_table_name %>]
    <%- else -%>
    params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
    <%- end -%>
  end
end
<% end -%>

```


### index
```index.html.erb
<h1>Listing <%= plural_table_name.titleize %></h1>

<table class="table table-striped table-bordered">
  <thead>
    <tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
      <th><%%= <%= class_name %>.human_attribute_name :<%= attribute.name %> %></th>
<% end -%>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
      <tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
        <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
        <td><%%= link_to t('usual.show'), <%= singular_table_name %>, class:'btn btn-default' %></td>
        <td><%%= link_to t('usual.edit'), edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class:'btn btn-default' %></td>
        <td><%%= link_to t('usual.delete'), <%= singular_table_name %>, method: :delete, data: { confirm: t('usual.confirm.delete') }, class:'btn btn-danger' %></td>
      </tr>
    <%% end %>
  </tbody>
</table>

<br>

<%%= link_to t('usual.new'), new_<%= singular_table_name %>_path, class:'btn btn-default' %>
```

### form
```_form.html.erb
<%%= form_for(@<%= singular_table_name %>) do |f| %>
  <%% if @<%= singular_table_name %>.errors.any? %>
    <div id="error_explanation">
      <h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>

      <ul>
      <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %>
        <li><%%= message %></li>
      <%% end %>
      </ul>
    </div>
  <%% end %>

<% attributes.each do |attribute| -%>
  <div class="field form-group">
<% if attribute.password_digest? -%>
    <%%= f.label :password %><br>
    <%%= f.password_field :password, :class => "form-control" %>
  </div>
  <div class="field">
    <%%= f.label :password_confirmation %><br>
    <%%= f.password_field :password_confirmation, :class => "form-control" %>
<% else -%>
    <%%= f.label :<%= attribute.column_name %> %><br>
    <%%= f.<%= attribute.field_type %> :<%= attribute.column_name %>, :class => "form-control" %>
<% end -%>
  </div>
<% end -%>
  <div class="actions">
    <%%= f.submit :class => "btn btn-primary" %>
  </div>
<%% end %>
```


rails g scaffold user name でできたもの
------------------------------------
```users_controller.rb
class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :set_breadcrumbs


  def index
    @users = User.all
  end


  def show
  end


  def new
    @user = User.new
  end


  def edit
  end


  def create
    @user = User.new(user_params)

    if @user.save
      redirect_to @user, notice: t('usual.create.success', name: 'User')
    else
      render :new
    end
  end


  def update
    if @user.update(user_params)
      redirect_to @user, notice: t('usual.update.success', name: 'User')
    else
      render :edit
    end
  end


  def destroy
    @user.destroy
    redirect_to users_url, notice: t('usual.destroy.success', name: 'User')
  end


private

  def set_user
    @user = User.find(params[:id])
  end


  def set_breadcrumbs
    add_breadcrumb User.model_name.human
  end


  def user_params
    params.require(:user).permit(:name)
  end
end
```

```index.html.erb
<h1>Listing Users</h1>

<table class="table table-striped table-bordered">
  <thead>
    <tr>
      <th><%= User.human_attribute_name :name %></th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name %></td>
        <td><%= link_to t('usual.show'), user, class:'btn btn-default' %></td>
        <td><%= link_to t('usual.edit'), edit_user_path(user), class:'btn btn-default' %></td>
        <td><%= link_to t('usual.delete'), user, method: :delete, data: { confirm: t('usual.confirm.delete') }, class:'btn btn-danger' %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to t('usual.new'), new_user_path, class:'btn btn-default' %>
```

```_form.html.erb
<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field form-group">
    <%= f.label :name %><br>
    <%= f.text_field :name, :class => "form-control" %>
  </div>
  <div class="actions">
    <%= f.submit :class => "btn btn-primary" %>
  </div>
<% end %>
```