RailsによるアジャイルWebアプリケーション開発
p.260まで。
第16章 タスクK:デプロイと本番環境
- 過去のスタンダードはApache + Passenger
- 今は Nginx + Unicorn がスタンダードのようだ
- Capistranoを使って自動化しないと手間&ミスが起こる
- 2 -> 3 になってコマンドや書き方が違うので注意
- ログのローテート
- config/environments/production.rb
- daily や weeklyにしたい場合
config.logger = Logger.new(config.paths["log"].first, 'dialy')
-
- サイズでわけたい場合(10ファイル保持)
require 'active_support/core_ext/numeric/bytes' config.logger = Logger.new(config.paths["log"].first, 10, 10.megabytes)
第17章 Depotのふりかえり
- 成果物のドキュメント
- sdocがGemfileに初期設定されている
https://rubygems.org/gems/sdoc
% sdoc -o doc/rails rails
- コードの分量確認
% ./bin/rake stats +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 502 | 361 | 9 | 56 | 6 | 4 | | Helpers | 22 | 22 | 0 | 1 | 0 | 20 | | Models | 79 | 64 | 5 | 6 | 1 | 8 | | Mailers | 17 | 15 | 2 | 2 | 1 | 5 | | Javascripts | 40 | 3 | 0 | 2 | 0 | -1 | | Libraries | 0 | 0 | 0 | 0 | 0 | 0 | | Controller specs | 328 | 269 | 0 | 0 | 0 | 0 | | Feature specs | 70 | 59 | 0 | 0 | 0 | 0 | | Mailer specs | 43 | 34 | 0 | 0 | 0 | 0 | | Model specs | 72 | 63 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 1173 | 890 | 16 | 67 | 4 | 11 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 465 Test LOC: 425 Code to Test Ratio: 1:0.9
第18章 Railsの構造
- libフォルダもautoloadされるパスに最初から追加されている
- lib/sample_libs/sample_class.rb が有ったとすると、requireすれば使える
require 'sample_libs/sample_class' SampleClass.new.say
- rakeタスク
- lib/tasks/db_schema_migrations.rb
namespace :db do desc "マイグレーションバージョンを表示する" task schema_migrations: :environment do puts ActiveRecord::Base.connection.select_values( 'select version from schema_migrations order by version' ) end end
% ./bin/rake db:schema_migrations
- Controller は階層を付きでgenerate出来る
% rails g controller Admin::Books index show create app/controllers/admin/books_controller.rb route namespace :admin do get 'books/show' end route namespace :admin do get 'books/index' end invoke haml create app/views/admin/books create app/views/admin/books/index.html.haml create app/views/admin/books/show.html.haml invoke rspec create spec/controllers/admin/books_controller_spec.rb invoke helper create app/helpers/admin/books_helper.rb invoke rspec invoke assets invoke coffee create app/assets/javascripts/admin/books.coffee invoke scss create app/assets/stylesheets/admin/books.scss
# 第19章 ActiveRecord
- 単数形、複数形でうまくいかない場合は infrection を設定するより、self.table_name を設定してやるのが解りよさそう
- _before_type_cast は Rubyの型に直す前の状態が取れる(全部文字列?)
% rails c --sandbox [git:master] Loading development environment in sandbox (Rails 4.2.0) Any modifications you make will be rolled back on exit [1] pry(main)> order = FactoryGirl.create(:order) (0.1ms) SAVEPOINT active_record_1 SQL (17.8ms) INSERT INTO "orders" ("name", "address", "email", "pay_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["name", "Guadalupe Kassulke Sr."], ["address", "East Helene"], ["email", "jairo_brekke@lubowitz.com"], ["pay_type", "現金"], ["created_at", "2015-01-28 13:08:39.837913"], ["updated_at", "2015-01-28 13:08:39.837913"]] (0.1ms) RELEASE SAVEPOINT active_record_1 => #<Order:0x007ff12b872368 id: 102, name: "Guadalupe Kassulke Sr.", address: "East Helene", email: "jairo_brekke@lubowitz.com", pay_type: "現金", created_at: Wed, 28 Jan 2015 13:08:39 UTC +00:00, updated_at: Wed, 28 Jan 2015 13:08:39 UTC +00:00> [2] pry(main)> order.created_at_before_type_cast => 2015-01-28 13:08:39 UTC [3] pry(main)> order.created_at => Wed, 28 Jan 2015 13:08:39 UTC +00:00
- 特別な列名
http://guides.rubyonrails.org/active_record_basics.html
- 楽観的ロック
- IDがない場合のマイグレーションファイル
class CreateBooks < ActiveRecord::Migration def change create_table :books, id: false do |t| t.string :isbn, null: false t.string :title t.integer :price t.timestamps null: false end add_index :books, :isbn, unique: true end end
- app/models/book.rb
class Book < ActiveRecord::Base self.primary_key = :isbn end
% rails c --sandbox Loading development environment in sandbox (Rails 4.2.0) Any modifications you make will be rolled back on exit [1] pry(main)> book = Book.new => #<Book:0x007fa4b3a44408 isbn: nil, title: nil, price: nil, created_at: nil, updated_at: nil> [2] pry(main)> book.isbn = "4774165166" => "4774165166" [3] pry(main)> book.title = "パーフェクト Ruby on Rails" => "パーフェクト Ruby on Rails" [4] pry(main)> book.save (0.1ms) SAVEPOINT active_record_1 SQL (0.4ms) INSERT INTO "books" ("isbn", "title", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["isbn", "4774165166"], ["title", "パ ーフェクト Ruby on Rails"], ["created_at", "2015-01-28 13:26:19.199587"], ["updated_at", "2015-01-28 13:26:19.199587"]] (0.1ms) RELEASE SAVEPOINT active_record_1 => true [5] pry(main)> book.id => "4774165166" [6] pry(main)> book.isbn => "4774165166" [7] pry(main)> Book.find "4774165166" Book Load (0.2ms) SELECT "books".* FROM "books" WHERE "books"."isbn" = ? LIMIT 1 [["isbn", "4774165166"]] => #<Book:0x007fa4b7ce1158 isbn: "4774165166", title: "パーフェクト Ruby on Rails", price: nil, created_at: Wed, 28 Jan 2015 13:26:19 UTC +00:00, updated_at: Wed, 28 Jan 2015 13:26:19 UTC +00:00>