2009年7月18日土曜日

[Railsのキホン][翻訳] Rails Wiki : List of Generators (1)

List of Genertors => Rails Wiki に、generator の使い方がまとまっていたので訳してみました。

controller

スタブは、新しいコントローラとそのビューを生成します。引数に、コントローラ名(CamelCaseかunder_scored形式)とビューのリストを与えます。

モジュールに含まれるコントローラを生成する場合、コントローラ名を parent_module/controller_name のようなパス形式で記述します。

このスタブは、app/controllers にコントローラクラスを、app/views/controller_name にビューテンプレートを、app/helpers にヘルパークラスを、test/functional に機能テストを、test/unit/helpers にヘルパーテストを生成します。

Example

./script/generate controller CreditCard open debit credit close

(私の環境での実行結果)

exists app/controllers/
exists app/helpers/
create app/views/credit_card
exists test/functional/
create test/unit/helpers/
create app/controllers/credit_card_controller.rb
create test/functional/credit_card_controller_test.rb
create app/helpers/credit_card_helper.rb
create test/unit/helpers/credit_card_helper_test.rb
create app/views/credit_card/open.html.erb
create app/views/credit_card/debit.html.erb
create app/views/credit_card/credit.html.erb
create app/views/credit_card/close.html.erb

コントローラ、ヘルパー、指定したビューが生成されています。
作成された雛形を見てみます。

app/controllers/credit_card_controller.rb


class CreditCardController < ApplicationController
def open
end

def debit
end

def credit
end

def close
end
end


test/functional/credit_card_controller_test.rb

require 'test_helper'

class CreditCardControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end


app/helpers/credit_card_helper.rb


module CreditCardHelper
end


test/unit/helpers/credit_card_helper_test.rb


require 'test_helper'

class CreditCardHelperTest < ActionView::TestCase
end


app/views/credit_card/open.html.erb


<h1>CreditCard#open</h1>
<p>Find me in app/views/credit_card/open.html.erb</p>
</pre>


Module Example

./script/generator controller 'admin/credit_card' suspend late_fee

Couldn't find 'admin/credit_card' generator

...作成されません。なにか間違っているのでしょうがわからない。

integration_test

スタブは、統合テストを生成します。引数に統合テスト名(CamelCaseかunder_scored形式)を与えます。test/integration/testname_test.rb に新しいテストが生成されます。

Example

./script/generate integration_test GeneralStories

(私の環境での実行結果)

exists test/integration/
create test/integration/general_stories_test.rb

作成された雛形を見てみます。

test/integration/general_stories_test.rb


require 'test_helper'

class GeneralStoriesTest < ActionController::IntegrationTest
fixtures :all

# Replace this with your real tests.
test "the truth" do
assert true
end
end


model

スタブは、新しいモデルを生成します。引数にモデル名(CamelCaseかunder_scored形式)と、(optionで)属性ペアのリストを与えます。

属性ペアは、column_name:sql_type という引数で、モデルの属性を記述するものです。タイムスタンプはデフォルトで追加されるので、created_at:timestamp および updated_at:timestamp を明示する必要はありません。

前もってすべての属性を考えておく必要はありませんが、スケッチとしていくつかを与えておくことで、そのモデルをすぐに使えるようになります。

このスタブは、app/models にモデルクラスを、test/unit にユニットテストを、text/fixtures/singular_name.yml にテストフィクスチャを、db/migrate にマイグレーションを生成します。

Example

./script/generate model post title:string body:text published:boolean

(私の環境での実行結果)

exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/post.rb
create test/unit/post_test.rb
create test/fixtures/posts.yml
create db/migrate
create db/migrate/20090718072719_create_posts.rb

作成された雛形を見てみます。

app/models/post.rb


class Post < ActiveRecord::Base
end


test/unit/post_test.rb


require 'test_helper'

class PostTest < ActiveSupport::TestCase
# Replace this with your real tests.
def test "the truth" do
assert true
end
end


test/fixtures/posts.yml

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
title: MyString
body: MyText
published: false

two:
title: MyString
body: MyText
published: false


db/migrate/20090718072719_create_posts.rb


class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.text :body
t.boolean :published

t.timestamps
end
end

def self.down
drop_table :posts
end
end


migration

スタブは、新しいデータベース・マイグレーションを生成します。引数にマイグレーション名(CamelCasedかunder_scored形式)と、(optionで)属性ペアのリストを与えます。

マイグレーションクラスは、db/migrate にタイムスタンプ(作成日時)のprefix付きで生成されます。

option引数をもとに、AddColumnsToTable もしくは RemoveColumnsFromTable のようにマイグレーション名を与えることができます。

Example

./script/generate migration AddTitleBodyToPost title:string body:text published:text

(私の環境での実行結果)
exists db/migrate
create db/migrate/20090718075112_add_title_body_to_post.rb

作成された雛形を見てみます。

db/migrate/20090718075112_add_title_body_to_post.rb


class AddTitleBodyToPost < ActiveRecord::Migration
def self.up
add_column :posts, :title, :string
add_column :posts, :body, :text
add_column :posts, :published, :boolean
end

def self.down
remove_column :posts, :published
remove_column :posts, :body
remove_column :posts, :title
end
end


続き

0 件のコメント:

コメントを投稿