LoginSignup
2
0

More than 5 years have passed since last update.

Rails開発 (私用)

Last updated at Posted at 2018-04-04

前提

Rails開発の際に確認する用の投稿。

アプリケーションの初期化

  • アプリケーションの命名規則

    • ローカル環境はスネーク記法
    • Githubや本番環境などではパスカルケース
  • ~/ <development environment dir> / <application> にアプリケーションを ~/ <development environment dir> / <document> に開発に関わる資料を保存する

    • ~/ <development environment dir> / <document> へ保存するもの
      • アプリ概要
      • サイトマップ
      • モデル関係図 (カラム・型一覧)
      • 参考にするアプリケーションのスクリーンショット (任意)
      • 参考にするアプリケーションの機能一覧 (任意)
  • gemの追加(それに伴う設定)。bundle install・rails db:create/migrate、モデル・コントローラ・ビューの生成、モデルの関連付けが完了後以下を実行

    • git init
    • git add -A
    • git commit -m '[initialize] レポジトリの初期化'
    • git remote add origin https://github.com/<username>/<appname>.git
    • git push -u origin <branchname>

Gem

Bootstrap

Gemfileでbootstrapを指定してインストールする。
BootstrapはjQueryに依存するため、(デフォルトでjQueryがインストールされない)Rails5.1以上ではjquery-railsもGemfileに追記する。

Gemfile
gem 'bootstrap', '~> 4.1.1'
gem 'jquery-rails'

注意点
sprockets-railsv2.3.2.以上である必要がある。

$ bundle show |fgrep sprockets-rails
  * sprockets-rails (3.2.1)

Railsアプリの作成時に生成されるapplication.cssをapplication.scssにリネームするなどして、.scss(Sass構文の場合は.sass)ファイルを用意する。

$ mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss

app/assets/stylesheets/application.scssでbootstrapをimportさせる。

/assets/stylesheets/application.scss
@import "bootstrap";

Bootstrapと依存関係をapplication.jsに追記する。

app/assets/javascripts/application.js
//= require jquery3
//= require popper
//= require bootstrap-sprockets

補足

  • Bootstrapのtooltipsやpopoverはpopper.jsに依存している
  • bootstrapの依存gemにpopper_jsが指定されているため新たにインストールは不要
  • コンパイルを高速化したい場合はbootstrap-sprocketsの代わりにbootstrapを指定する
  • レスポンシブ対応にするには<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">を指定する、あとあとmeta-tagsで指定するのもあり

Dotenv

Railsの環境のよって変わる設定値 (database.yml etc...) を環境変数として扱えるようにするGem、Gemfileでdotenv-railsを指定してインストールする。

Gemfile
gem 'dotenv-rails'

ホームディレクトリ直下に.envを作成。

.env
AWS_ACCESS_KEY_ID="hogehoge"
AWS_SECRET_ACCESS_KEY="mogamoga"

上記のように使用する。

Annotate

現状のスキーマの注釈をコメントとしてファイルの上部や下部に追記してくれる。

  • ActiveRecord models
  • Fixture files
  • Tests and Specs
  • Object Daddy exemplars
  • Machinist blueprints
  • Fabrication fabricators
  • Thoughtbot's factory_girl factories, i.e. the (spec|test)/factories/_factory.rb - - files
  • routes.rb file (for Rails projects)

Gemfileでannotateを指定してインストールする。

Gemfile
gem 'annotate', ">=2.6.0"

追加される注釈はこんな感じ:

   # == Schema Info
   #
   # Table name: line_items
   #
   #  id                  :integer(11)    not null, primary key
   #  quantity            :integer(11)    not null
   #  product_id          :integer(11)    not null
   #  unit_price          :float
   #  order_id            :integer(11)
   #

    class LineItem < ActiveRecord::Base
      belongs_to :product
     . . .

オプション

Usage: annotate [options] [model_file]*
オプション 説明
-d, --delete 注釈を削除する。
-p, --position [before or after] 注釈を追記する箇所を指定する。
--pc, --position-in-class [before or after]
:-- :--
--pf, --position-in-factory [before or after] factoryファイルにて、注釈を追記する箇所を指定する。
--px, --position-in-fixture [before or after] fixtureファイルにて、注釈を追記する箇所を指定する。
--pt, --position-in-test [before or after] testファイルにて、注釈を追記する箇所を指定する。
--pr, --position-in-routes [before or after] routes.rbにて、注釈を追記する箇所を指定する。
-r, --routes 'rake routes'の結果をroutes.rbに追記する。
-v, --version annotateのバージョンを表示する。
-m, --show-migration 注釈にマイグレーションの番号を含める。
--model-dir dir dirにあるmodelファイルに注釈を追加する。
--ignore-model-subdirects modelの副階層を無視する。
--sort カラムをアルファベット順に並べる
-e [tests,fixtures,factories] []で囲まれた対象には注記を追記しないようにする。 fixtures, test files, and/or factories --exclude
-f [bare or rdoc or markdown] スキーマ情報を描画するフォーマットを指定する。
--force 変更がなくても注記を更新する。
--trace 注記追記に失敗したときスタックトレースを表示する。
--timestamp routes.rbに更新日時を追記する。

注意点
ファイル末尾に追記された注記よりしたにコメントを書かないこと。

Ridgepole

railsでは通常はmigrateファイルによってDBのスキーマを管理している。
が、追加/削除/リネームが結構面倒くさい。
そこで毎回migrateする必要がなくなり、ファイルを書き直すだけで簡単にスキーマの管理ができるGemであるridgepoleを使おうということである。

gem 'ridgepole'

bundle installする。

config/ridgepole.ymlを新規作成する
ファイル名、階層は自由(config配下であることは必須?)

cofig/ridgepole.yml
adapter: mysql2
encoding: utf8
database: ????????
username: ????????
password: ????????

Schemafileを作成する。

create_table "articles", force: true do |t|
  t.string   "title"
  t.text   "content"
  t.integer  "status"
  t.datetime "created_at"
  t.datetime "updated_at"
end

Ridgepoleコマンド

$ ridgepole -c config/ridgepole.yml --apply -f Schemafile

ridgepole -c (dbの設定ファイルのpath) --apply -f (Schemafileのpath)
でSchemafileの内容を反映できる

今後はmigrateコマンドを一切使わないので、
rails g modelやrails g scaffoldなどmigrateが必要なコマンドを実行する場合は最後に「--skip-migration」を付ける。(rails g controllerはmigrate不要だったはず)

Dotenv

  • pry-rails と pry-bybug をインストールすると...
    • Rails console で Pry が使える
    • binding.pry で処理を止めてステップ実行ができるようになる。

Pry

group :development, :test do
  gem 'pry-rails'
  gem 'pry-byebug'
  gem 'pry-doc'
end
  • pry-rails と pry-bybug をインストールすると...
    • Rails console で Pry が使える
    • binding.pry で処理を止めてステップ実行ができるようになる。
    • 今回は pry-doc も入れてみた
2
0
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
2
0