LoginSignup
6
7

More than 5 years have passed since last update.

Padrinoでプロジェクトのテンプレートを作る

Last updated at Posted at 2012-12-09

Padrinoは色んなO/Rマッパー、テンプレートエンジン、データベース、テスティングフレームワークを選べるので柔軟で便利なんだけど、プロジェクトを作成する度に「あれ?どう指定したらいいんだっけ?」となりがち。

しかも自分が普段使うテスティングフレームワークやテンプレートエンジンなんかは決まっているので別に毎回柔軟に設定できる必要もない。

「自分はいつもjquery使っててactiverecordをO/Rマッパーにしていてテンプレートエンジンはslimだ!」
なんてこともあるよね。
そんな時はこんな感じでtemplateを作成しておこう。

my_padrino_template.rb
project {
  :test => :rspec,
  :renderer => :slim,
  :script => :jquery,
  :orm => :activerecord,
  :bundle => true
}

こういうファイルを保存しておいて、プロジェクトを作成するときに

padrino g project --template /path/to/my_padrino_template.rb

と指定して実行すれば決まった構成でプロジェクトを作れる。
これでなんか指定し忘れて「うお、テンプレートエンジンがデフォルトのhamlになっとる!」なんてこともない。

「普段hamlでもslimでも色んなテンプレートエンジン使い分けてるからなー」って場合はthorのメソッドを使って対話式にも出来る。

my_padrino_template.rb
renderer = ask("Renderer(erb, slim, haml, liquid):")

choices = {
    :test => :rspec,
    :renderer => renderer,
    :script => :jquery,
    :orm => :activerecord,
    :bundle => true
}

project choices

こうしておくと実行時に選べるようになる。

$ padrino g project hello_prj --template ./my_template.rb                                     
       apply  /Path/to/my_template.rb
  Renderer(erb, slim, haml, liquid): slim #ここで聞いてくれる!
  => Executing: padrino-gen project hello_prj --bundle=true --orm=activerecord --renderer=slim --script=jquery --test=rspec -r=/Path/to

というように選択式にすることも出来る。

こんな感じで自分のtemplateを育てておくと便利。毎回作る認証部分までを含んだtemplateだって作成可能。

padrino-recipesの中のtemplatesにはpadrinoのチュートリアルで作成するblogのアプリを丸々作成出来るtenplateもある。

この辺のAPIを見ておくといいかも。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7