Padrinoはジェネレータによる素早いアプリケーション作成をサポートしています。ジェネレータはPadrinoが推奨する構成のアプリケーションの構築や、開発するにあたって必要な依存ライブラリを含むGemfileの自動生成、そして自動生成されたファイルに含まれるPadrinoの初心者向けのガイドラインといった利益をもたらします。
ジェネレータの重要な特徴のひとつは徹底して幅広いツールやライブラリ、gemをあなたのPadrinoアプリケーションに組込めるようになっているということです。
これはPadrinoのジェネレータが特定のデータベースやORM, テスト・フレームワーク、テンプレート・エンジンやJavaScriptのライブラリを使うことを強制しないということです。実際、アプリケーションを生成するときにどれを使いたいか選ぶことができるのです!
プロジェクト・ジェネレータ
プロジェクト・ジェネレータにはいくつかの設定オプションがあります。
| オプション | 初期値 | 別名 | 概要 |
|---|---|---|---|
| bundle | false | -b | bundlerによる依存ライブラリのインストールを行います |
| root | . | -r | プロジェクトのルートパスを指定します |
| dev | false | none | 開発版のPadrinoをGitリポジトリから取得してそれを使います |
| app | nil | -n | プロジェクト名とは異なるアプリケーション名を使う場合に指定します |
| tiny | false | -i | 最小限度のプロジェクト構成で始めます |
| adapter | sqlite | -a | ORMが使うDBアダプタを指定します (mysql, sqlite, postgres) |
プロジェクト・ジェネレータの使い方はとても簡単です:
$ padrino g project <the_app_name> </path/to/create/app> --<component-name> <value>ベースとなるアプリケーションを生成するコマンドをできる限り簡潔にするとこうなるでしょう:
$ padrino g project demo_projectこれは (Padrino::Applicationを継承した) DemoAppというPadrinoアプリケーションを現在のパスの ‘demo_project’ フォルダの中に作ります。アプリケーションの中には設定とデフォルトのコンポーネントのための配置がなされています。
また特定のコンポーネントを使うよう明示できます:
$ padrino g project demo_project -t rspec -e haml -m rr -s jquery -d datamapper -c sassあなたはジェネレータにあるコンポーネントをまったく使わないので (あるいはあなた自身のを使うので) 無視するように指示できます:
$ padrino g project demo_project --test none --renderer none--app オプションを使ってあなたのコア・アプリケーションの別名を指定することもできます:
$ padrino g project demo_project --app alternate_app_name # alias -n
ジェネレータはアプリケーションが新しく作られたときにすべての依存性を解決するために bundler を使います。必要なbundlerのコマンドはジェネレータが自動的に実行します:
$ padrino g project demo_project --run_bundler # alias -b
あるいはターミナルから生成されたアプリケーションのルート・ディレクトリで bundle install コマンドを手動で実行することも可能です。
もし手動でも実行されなかった場合、bundlerによる依存性の解決は最初にアプリケーションが起動するときに自動的に行われます。
このコマンドはアプリケーションが最初に生成されたときかGemfileが更新されたときにだけ実行する必要があることに注意してください。
Padrinoに同梱されたジェネレータ・フレームワークは拡張可能でコンポーネントやツールを簡単に追加できます。
これは私達のプロジェクトをフォークして lib/generators/project.rb や lib/generators/components/ の中のファイルにあるセットアップの説明を読むことで達成されるでしょう。オリジナルには含まれていなかった新たな種類のコンポーネントを含むpull requestを私達は喜んで受け入れます (といってもこれらをメンテナンスするのに助けになるものであってもありがたいです)。
有効なコンポーネントと初期値は次のようになっています:
| コンポーネント | 初期値 | 別名 | オプション |
|---|---|---|---|
| orm | none | -d | mongomapper, mongoid, activerecord, datamapper, couchrest, mongomatic, ohm |
| test | none | -t | bacon, shoulda, cucumber, testspec, riot, rspec |
| script | none | -s | prototype, rightjs, jquery, mootools, extcore, dojo |
| renderer | haml | -e | erb, haml, slim, liquid |
| stylesheet | none | -c | sass, less, scss, compass |
| mock | none | -m | rr, mocha |
いくつかの例:
プロジェクトのパスとは異なる名前のアプリケーションのプロジェクトを生成する
padrino g my_project -n blog
このコマンドは my_project/ というパスでプロジェクトを生成しますがアプリケーションの名前は Blog になります。
mongoidを使うプロジェクトを生成した後にbundlerを実行します
padrino g project your_project -d mongoid -b
テストにriotをモックにrrを使うプロジェクトを生成します
padrino g project your_project -t riot -m rr
ORMにsequelをバックエンドにmysqlを使うプロジェクトを生成します
padrino g project your_project -d sequel -a mysql
最小限度のプロジェクトのスケルトンを生成します
padrino g project your_project --tiny
プロジェクトのルートディレクトリを指定します
padrino g project your_project -r /usr/local/padrino
このコマンドは新しいPadrinoのプロジェクトを /usr/local/padrino/your_project/ に生成します。
Gitのリポジトリから取得したPadrinoを使う
padrino g project your_project [--dev] # Use padrino from a git checkout
開発版 を使うための詳しい情報は The Bleeding Edge を参照してください。
Plugin Generator
The Plugin Generator allows you to create Padrino projects based on a template file that contains all the necessary actions needed to create the project. Plugins can also be executed within an existing Padrino application. The plugin generator provides a simple DSL in addition with leveraging Thor to make generating projects a breeze!
$ padrino g project my_project --template path/to/my_template.rbThis will generate a project based on the template file provided. You can also generate a project based on a remote url such as a gist for an additional level of convenience:
$ padrino g project my_project --template http://gist.github.com/356156You can also execute template files directly from the official templates repo:
$ padrino g project my_project --template sampleblogYou can also apply templates as plugins to existing Padrino applications:
$ cd path/to/existing/padrino/app $ padrino g plugin path/to/my_plugin.rb
You can also execute plugin files directly from the official plugins repo:
$ cd path/to/existing/padrino/app $ padrino g plugin hoptoad
You can even get a list of available plugins with the following command:
$ padrino g plugin --listA simple template (plugin) file might look like this:
# my_template.rb project :test => :shoulda, :orm => :activerecord generate :model, "post title:string body:text" generate :controller, "posts get:index get:new post:new" generate :migration, "AddEmailToUser email:string" require_dependencies 'nokogiri' git :init git :add, "." git :commit, "initial commit" inject_into_file "app/models/post.rb","#Hello", :after => "end\n" rake "ar:create ar:migrate" initializer :test, "# Example" app :testapp do generate :controller, "users get:index" end git :add, "." git :commit, "second commit"
Keep in mind that the template file is pure ruby and has full access to all available thor actions.
Controller Generator
| Options | Default | Aliases | Description |
|---|---|---|---|
| app | nil | -n | specify the application |
| root | . | -r | specify the root destination |
| destroy | false | -d | removes all generated files |
Padrino provides generator support for quickly creating new controllers within your Padrino application. Note that the controller tests are generated specifically tailored towards the testing framework chosen during application generation.
Very important to note that controller generators are intended primarily to work within applications created through the Padrino application generator and that follow Padrino conventions.
Using the controller generator is as simple as:
$ padrino g controller AdminIf you want create a controller for a specified sub app you can:
$ padrino g controller Admin -a my_sub_appYou can also specify desired actions to be added to your controller:
$ padrino g controller Admin get:index get:new post:createThe controller generator will then construct the controller file within app/controllers/admin.rb and also a controller test file at test/controllers/admin_controller_test.rb according to the test framework chosen during app generation. A default route will also be generated mapping to name of the controller and the route name. For example:
$ padrino g controller User get:indexwill create a url route for :index mapping to “/user/index”
You can destroy controllers that you created via the destroy option and setting it to true. default is false.
$ padrino g controller User -dThis removes all created controller files.
Model Generator
| Options | Default | Aliases | Description |
|---|---|---|---|
| root | . | -r | specify the root destination path |
| skip_migration | false | -s | skip migration generation |
| destroy | false | -d | removes all generated files |
Padrino provides generator support for quickly creating new models within your Padrino application. Note that the models (and migrations) generated are specifically tailored towards the ORM component and testing framework chosen during application generation.
Very important to note that model generators are intended primarily to work within applications created through the Padrino application generator and that follow Padrino conventions. Using model generators within an existing application not generated by Padrino will likely not work as expected.
Using the model generator is as simple as:
$ padrino g model UserYou can also specify desired fields to be contained within your User model:
$ padrino g model User name:string age:integer email:stringThe model generator will create multiple files within your application and based on your ORM component.
Usually the model file will generate files similar to the following:
- model definition file [app/models/user.rb]
- migration declaration [db/migrate/xxx_create_users.rb]
- model unit test file [test/models/user_test.rb]
You can define as many models as you would like in a Padrino application using this generator.
You can destroy models that you created via the destroy option and setting it to true. default is false.
$ padrino g model User -dThis remove all created model files.
Migration Generator
| Options | Default | Aliases | Description |
|---|---|---|---|
| root | . | -r | specify the root destination path |
| destroy | false | -d | removes all generated files |
Padrino provides generator for quickly generating new migrations to change or manipulate the database schema.
These migrations generated will be tailored towards the ORM chosen when generating the application.
Very important to note that migration generators are intended primarily to work within applications created through the Padrino application generator and that follow Padrino conventions. Using migration generators within an existing application not generated by Padrino will likely not work as expected.
Using the migration generator is as simple as:
$ padrino g migration AddFieldsToUsers $ padrino g migration RemoveFieldsFromUsers
You can also specify desired columns to be added to the migration file:
$ padrino g migration AddFieldsToUsers last_login:datetime crypted_password:string $ padrino g migration RemoveFieldsFromUsers password:string ip_address:string
The migration generator will then construct the migration file according to your ORM component chosen
within db/migrate/xxx_add_fields_to_users.rb including the columns specified in the command.
You can destroy migrations that you created via the destroy option and setting it to true. default is false.
$ padrino g migration AddFieldsToUsers -dThis removes the migration file.
Mailer Generator
| Options | Default | Aliases | Description |
|---|---|---|---|
| app | nil | -n | specify the application |
| root | . | -r | specify the root destination path |
| destroy | false | -d | removes all generated files |
Padrino provides generator support for quickly creating new mailers within your Padrino application.
Very important to note that mailer generators are intended primarily to work within applications created through the Padrino application generator and that follow Padrino conventions.
Using the mailer generator is as simple as:
$ padrino g mailer UserNotifierIf you want create a mailer for a specified sub app you can:
$ padrino g mailer UserNotifier -a my_sub_appYou can also specify desired delivery actions to be added to the mailer:
$ padrino g mailer UserNotifier confirm_account welcome inactive_accountThe mailer generator will then construct the mailer file within app/mailers/user_notifier.rb
You can destroy mailer that you created via the destroy option and setting it to true. default is false.
$ padrino g mailer UserNotifer -dThis remove all created mailer files.
Sub App Generator
| Options | Default | Aliases | Description |
|---|---|---|---|
| tiny | false | -i | generate tiny app skeleton |
| root | . | -r | specify the root destination path |
| destroy | false | -d | removes all generated files |
Unlike other ruby frameworks Padrino is principally designed for mounting multiple apps at the same time.
First you need to create a project:
$ padrino g project demo_project $ cd demo_project
Now you are in demo_project and you can create your apps:
$ padrino g app one $ padrino g app two
By default these apps are mounted under:
- /one
- /two
but you can edit config/apps.rb and change it.
and create controllers:
your_project$ padrino g controller base --app foo # This will be created for app Foo your_project$ padrino g controller base # This will be created for Core app your_project$ padrino g controller base --app bar # This will be created for app Bar
or mailers:
your_project$ padrino g mailer registration --app foo # This will be created for app Foo your_project$ padrino g mailer registration # This will be created for Core app your_project$ padrino g mailer registration --app bar # This will be created for app Bar
Tiny Skeleton Generator
Both the Project Generator and Sub App Generator allow you to create an even smaller project skeleton. Instead of the default skeleton, the tiny option removes the need for a controllers, helpers, and mailers folder and instead generates controllers.rb, helpers.rb, and mailers.rb in its place.
To use the tiny skeleton generator for project run:
$ padrino g project tiny_app -d mongoid --tinyTo use the tiny skeleton generator for app run in your project:
my_app$ padrino g app tiny_app --tinyAdmin Generator
| Options | Default | Aliases | Description |
|---|---|---|---|
| name | nil | -a | allows you to specify the admin app’s name |
| root | . | -r | specify the root destination path |
| theme | default | none | generate admin app with theme |
| skip_migration | false | -s | skip migration generation |
| destroy | false | -d | removes all generated files |
Padrino also comes with a built-in admin dashboard. To generate the admin application in your project:
$ padrino g adminYou can specify the theme to use for your admin application using the theme flag:
$ padrino g admin --theme blueThe available themes are: amro, bec, bec-green, blue, default, djime-cerulean, kathleene, olive, orange, reidb-greenish, ruby, warehouse
This will generate the admin application and mount this at /admin. For more information, check out the Admin Guide.

