Rails の設定については、Rails4でheroku Pushまでの最短手順 Haml/bootstrap 3.0/postgresql or MySQLのページを参考にしています。
最初に、ローカル Redmine に「moritaka-db」プロジェクトを作成します。最初のチケットは「アプリケーション開発環境の整備」としました。次にベースリポジトリを自分の git リポジトリ置き場に作成します。
mkdir ~/git/moritaka-db.git cd ~/git/moritaka-db.git git init ―bare
Redmine のプロジェクト設定でこのリポジトリを設定します。
Spring が gem 2.1 以上の gem を期待しているので、当初 gem を update したのですが、せっかくならと Ruby も 2.1.0 に上げてしまいました。
rbenv install 2.1.0 rbenv global 2.1.0
バージョンを確認すると以下のようになりました。
hkob@rMBP ~> ruby -v ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0] hkob@rMBP ~> gem -v 2.2.0
とにもかくにも bundler をインストールします。
gem install bundler
それでは、アプリケーション環境の準備に入ります。まずはフォルダを作成し,bundle の初期化をします.
mkdir moritaka-db cd moritaka-db bundle init
Gemfile が作成されるので,最後の行のコメントを削除します.
# A sample Gemfile source "https://rubygems.org" gem “rails” # この行のコメントを外す
vendor/bundle の下に rails をインストールします.
bundle install --path vendor/bundle
bundle 内の rails を使ってアプリを作成します.bundle 作成はスキップし,データベースは postgresql を使います.また,bundle exec は何度も使うので be とエイリアスしています.Gemfile の上書きを聞かれるので,許可します.
be rails new . --skip-bundle --skip-test-unit -d postgresql
gitignore-boilerplates を使って、.gitignore を設定します。
gibo Ruby Rails OSX vim > .gitignore
ローカル git リポジトリを作成します。
git init git add -A git commit -m “first commit”
Gemfile を修正します。のサイトを参考にして必要なものを入れてみます。
source 'https://rubygems.org' ruby ‘2.1.0’ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails’ gem 'rails', ‘4.0.2’ # Use postgresql as the database for Active Record gem ‘pg’ # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0’ # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0’ # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0’ # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails’ # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem ‘turbolinks’ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 1.2’ group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.1.2’ # Use unicorn as the app server # gem ‘unicorn’ # Use Capistrano for deployment # gem 'capistrano', group: :development # Use debugger # gem 'debugger', group: [:development, :test] # Haml gem ‘haml' gem 'haml-rails’ # Assets log cleaner gem ‘quiet_assets' # Pagenation gem ‘kaminari’ # App Server gem 'puma' # Process Management gem 'foreman' # HTML5 Validator gem 'html5_validators' group :development do # Converter erb => haml gem ‘erb2haml’ end group :development, :test do # Rails application preloader gem ‘spring’ # Railsコンソールの多機能版 gem 'pry-rails’ # pryの入力に色付け gem 'pry-coolline’ # デバッカー gem 'pry-byebug’ # Pryでの便利コマンド gem 'pry-doc’ # PryでのSQLの結果を綺麗に表示 gem ‘hirb' gem 'hirb-unicode’ # pryの色付けをしてくれる gem ‘awesome_print’ # Rspec gem 'rspec-rails’ gem ‘rake_shared_context’ # fixtureの代わり gem “factory_girl_rails" # テスト環境のテーブルをきれいにする gem ‘database_rewinder’ # Guard gem 'guard-rspec’ gem 'guard-spring’ # Turnip gem 'turnip' # Capybara gem 'capybara-webkit' end group :production, :staging do # ログ保存先変更、静的アセット Heroku 向けに調整 gem ‘rails_12factor' end
bundle を行います。インストールされた gem の一覧は以下のようになりました。ものすごい数の gem がインストールされます。
Using rake (10.1.1) Using i18n (0.6.9) Using minitest (4.7.5) Using multi_json (1.8.4) Using atomic (1.1.14) Using thread_safe (0.1.3) Using tzinfo (0.3.38) Using activesupport (4.0.2) Using builder (3.1.4) Using erubis (2.7.0) Using rack (1.5.2) Using rack-test (0.6.2) Using actionpack (4.0.2) Using mime-types (1.25.1) Using polyglot (0.3.3) Using treetop (1.4.15) Using mail (2.5.4) Using actionmailer (4.0.2) Using activemodel (4.0.2) Using activerecord-deprecated_finders (1.0.3) Using arel (4.0.1) Using activerecord (4.0.2) Using awesome_print (1.2.0) Using columnize (0.3.6) Using debugger-linecache (1.2.0) Using byebug (2.5.0) Using mini_portile (0.5.2) Using nokogiri (1.6.1) Using xpath (2.0.0) Using capybara (2.1.0) Using json (1.8.1) Using capybara-webkit (1.1.1) Using timers (1.1.0) Using celluloid (0.15.2) Using coderay (1.1.0) Using coffee-script-source (1.7.0) Using execjs (2.0.2) Using coffee-script (2.2.0) Using thor (0.18.1) Using railties (4.0.2) Using coffee-rails (4.0.1) Using coolline (0.4.3) Using database_rewinder (0.0.2) Using diff-lcs (1.2.5) Using dotenv (0.9.0) Using tilt (1.4.1) Using haml (4.1.0.beta.1) Using hpricot (0.8.6) Using sexp_processor (4.4.1) Using ruby_parser (3.1.3) Using html2haml (1.0.1) Using erb2haml (0.1.5) Using factory_girl (4.3.0) Using factory_girl_rails (4.3.0) Using ffi (1.9.3) Using foreman (0.63.0) Using formatador (0.2.4) Using gherkin (2.12.2) Using rb-fsevent (0.9.4) Using rb-inotify (0.9.3) Using listen (2.4.0) Using lumberjack (1.0.4) Using method_source (0.8.2) Using slop (3.4.7) Using pry (0.9.12.6) Using guard (2.3.0) Using rspec-core (2.14.7) Using rspec-expectations (2.14.4) Using rspec-mocks (2.14.4) Using rspec (2.14.1) Using guard-rspec (4.2.5) Using spring (1.1.0) Using guard-spring (0.0.4) Using haml-rails (0.5.3) Using hike (1.2.3) Using hirb (0.7.1) Using unicode-display_width (0.1.1) Using hirb-unicode (0.0.5) Using html5_validators (1.1.2) Using jbuilder (1.5.3) Using jquery-rails (3.1.0) Using kaminari (0.15.1) Using pg (0.17.1) Using pry-byebug (1.2.1) Using pry-coolline (0.2.3) Using yard (0.8.7.3) Using pry-doc (0.5.1) Using pry-rails (0.3.2) Using puma (2.7.1) Using quiet_assets (1.0.2) Using bundler (1.5.2) Using sprockets (2.10.1) Using sprockets-rails (2.0.1) Using rails (4.0.2) Using rails_serve_static_assets (0.0.2) Using rails_stdout_logging (0.0.3) Using rails_12factor (0.0.2) Using rake_shared_context (0.2.0) Using rdoc (4.1.1) Using rspec-rails (2.14.1) Using sass (3.2.14) Using sass-rails (4.0.1) Using sdoc (0.4.0) Using spring-commands-rspec (1.0.1) Using turbolinks (2.2.0) Using turnip (1.2.0) Using uglifier (2.4.0)
ターミナルで postgresql のユーザを作成します
createuser moritaka-db <|| postgresql 上でパスワードを設定します. >|| alter user moritakadb with encrypted password 'パスワード'; alter user moritakadb createdb; <|| config/database.yml にてユーザ名,パスワードを設定します. >|rb| development: adapter: postgresql encoding: unicode database: moritakadb_development pool: 5 username: moritakadb password: 設定したパスワード test: adapter: postgresql encoding: unicode database: moritakadb_test pool: 5 username: moritakadb password: 設定したパスワード production: adapter: postgresql encoding: unicode database: moritakadb_production pool: 5 username: moritakadb password: 設定したパスワード <|| データベースを作成します。spring が gem で入ったので、普段の rake も spring を使いましょう。ただし、bundle exec spring と書くのは面倒なので「bes」とエイリアスを設定しています。.config/fish/config.fish の該当部は以下のようになります >|sh| alias be 'bundle exec’ alias bes 'be spring’ alias r 'bes rails'
この結果、データベスの作成コマンドは以下のようになります。
bes rake db:create
次にテスト環境を設定します。最初に rspec を設定します。
r g rspec:install
config/application.rb を設定します。locale は動的に判定する予定ですが、default_locale は後で設定します。詳しくはRailsの多言語化対応 I18nのやり方を整理してみた!【国際化/英語化】を参照のこと。model のテストは Rspec で行いますが、それ以外は Turnip を使う予定なので、spec ファイルは設定しないようにします。また、helper や assets も必要な時に自分で作ることにします。
config.time_zone = 'Tokyo' config.active_record.default_timezone = :local config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s] # generatorの設定 config.generators do |g| g.orm :active_record g.template_engine :haml g.test_framework :rspec, :fixture => true g.fixture_replacement :factory_girl, :dir => "spec/factories" g.view_specs false g.controller_specs false g.routing_specs false g.helper_specs false g.request_specs false g.assets false g.helper false end
config/initializes/secret_token.rb を環境変数を優先に設定するようにします。
MoritakaDb::Application.config.secret_key_base = ENV['SECRET_KEY_BASE'] || '元のキー'
locale ファイルを取得しておきます。
wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/{ja,en}.yml -P config/locales/
雛形で作成されてしまった、erb を haml に変換します。
bes rake haml:replace_erbs
また、config/initializers/after_initilize.rb を作成します。
Rails.application.config.after_initialize do ActiveRecord::Base.connection_pool.disconnect! ActiveSupport.on_load(:active_record) do if Rails.application.config.database_configuration config = Rails.application.config.database_configuration[Rails.env] config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds config['pool'] = ENV['DB_POOL'] || 5 ActiveRecord::Base.establish_connection(config) end end end
さらに Procfile を記述し、foreman から puma を起動するように設定します。
web: bundle exec puma -t ${PUMA_MIN_THREADS:-8}:${PUMA_MAX_THREADS:-12} -w ${PUMA_WORKERS:-2} -p $PORT -e ${RACK_ENV:-development}
とりあえず、アプリケーションが起動するかどうかを確認
foreman start
localhost:5000 にアクセスしてアプリケーションが Rails のいつもの画面が出てくるところまでを確認します。とりあえず、ここまでの状況を commit しておきます。
git commit -am "アプリケーション起動まで"
git remote add origin ~/git/moritaka-db
git push -u origin master
heroku の設定を行います。
heroku create moritaka-db git push heroku master
運用環境ではルーティングエラーの画面になっていますね。これからテストを書いて進めていきます。とりあえず今日はここまで。