オレオレRailsApplicationTemplateのススメ
RailsApplicationTemplateとは?
rails で新規にWebアプリケーションを作成する際に利用できる「ひな形」のことです。
ひな形はRubyのコードで記述でき、ファイルを編集したり、
各種RailsGeneratorを実行したりすることができます。
詳細は以下のドキュメントを参照してください。
Rails Application Templates — Ruby on Rails Guides
では作成したTemplateを紹介します。
SwanTemplate
SwanTemplateの主な特徴は、デザインフレームワークをBootstrapとMaterialDesignから選択でき、
scaffoldでそれぞれに最適なソースを自動生成してくれること。
Indexページで検索を可能にしていることです。
使い方
これだけです。
git clone https://github.com/swanmatch/SwanTemplate rails new BootstrapTest -d mysql -T -m SwanTemplate/main.rb # example rails g swaffold author name rails g swaffold genre name rails g swaffold book title author:references genre:references overview:text rails db:migrate
追加機能
Bootstrap or MaterialDesign
先述の通り、
option Please Choise use design frame work 1) Bootstrap 2) Material choose Enter your selection: _
この選択によって、それぞれに最適なソースをsimple formのconfigと、
_form.html.erbの中身が作成されます。
Scaffoldで作成されるBootstrapの画面のイメージです。
こちらはMaterialDesign。
Ripple(ボタン押した時の波のエフェクト)や、
FloatingLabel(inputにmouse overでラベルが動くエフェクト)が、
ノンデザイン、ノーコーディングで使えます。
Lavish連携機能(Bootstrap限定)
BootstrapではLavishによるデザインの自動生成が可能です。
- Visit to https://www.lavishbootstrap.com
- 適当な画像(企業ロゴなど)のURLをフォームに貼り付け、「provide Image」ボタンを押します。
- 出来上がったSASSを
.app/assets/stylesheets/_custom_variables.scss
に貼り付けます
例えば次の画像だと、、、
以下のような画面になります。
Search sub class and form in index
modelにSearchサブモデルを作ります。
SearchサブモデルはActiveModel::Baseをincludeしています。
そして検索フォームindexページに自動生成されます。
文字列(varchar)とテキスト型のカラムはLIKE検索、 数値(integer)や日付(date)は最大値、最小値で挟み込んで検索ができます。 外部キー(references)はselect2を使った複数選択が可能です。
Select2
外部キーのフォームにはSelect2が自動で適用されることで、
関連テーブルをLIKE検索で入力することができます。
前述の通り一覧ページでは複数選択でOR検索が可能です。
Bootstrap Datepicker
日付型のフォームはBootstrap Datepickerが適用されます。
Kaminari pagenation
一覧ページで結果が25件以上の場合は、
Kaminari helperでページネーションできます。
Logical Delete
migrationファイルにdeleted_atカラムを自動で追加します。
また、以下の論理削除系のメソッドを提供します。
- logical_delete!
deleated_atにTime.nowをINSERTします。 - self.active
where(deleated_at: nil) - active?
- self.deleted
where("#{self.table_name}
.deleated_at
IS NOT NULL") - deleted?
Scaffoldで作られるdeleteアクションを論理削除に置き換えています。
一覧ページは論理削除されていないもののみを表示し、
また、外部キーのフォームも同様に有効なもののみから選択するようになります。
Lock version
migrationに自動でlock_versionカラムを追加します。
編集ページにhiddenで埋め込み、
controllerのstrong parametarsに追加することで、
楽観排他制御を行っています。
ERD and annnotate
マイグレーション実行後にER図を自動生成します。
事前にgraphvizをインストールしてください。
詳細はこちら↓
Rails ERD – Installation instructions
モデルファイルの先頭にコメントでスキーマ情報を書き出します。
example
# coding: utf-8 # == Schema Information # # Table name: books # # id :integer not null, primary key # title :string(255) # author_id :integer # genre_id :integer # overview :text(65535) # lock_version :integer default(0), not null # created_by :integer # updated_by :integer # deleted_by :integer # created_at :datetime not null # updated_at :datetime not null # deleted_at :datetime
i18n genarator
マイグレーション実行後に日本語のja.ymlを自動生成します。
Suppurts
ubuntu + mysql + ruby2.4 + rails5.1でテストしています。
その他でもそれなりには動くと思う。
################################################################### ## ## ## Please enjoy your trip of the Limited Express Train on Rails! ## ## ## ################################################################### _O 〈_〉 _ ||nn ─┐ y─────────────────、,─────────────────、_______ ( ̄ ̄ ̄|囗| |日 口口口口口口口口| |口口口口口口口口口口| |ロ ロ| [三三五L_」__|__________|_|__________|_|_____凵 ∠7◎◎◎=◎~~~ ◎=◎ ◎=◎ ~ ◎=◎ ◎=◎ ~ ◎=◎ =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=