2015年2月26日木曜日

2015年2月25日水曜日

turbolinks で jquery.ready が動かない


var ready = function() {
  ...your javascript goes here...
};

$(document).ready(ready);
$(document).on('page:load', ready);  // turbolinks対応


http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links


page:load, ready 以外にも
https://github.com/rails/turbolinks/

2015年2月21日土曜日

Passenger 5


passenger が刷新されてる?

https://www.phusionpassenger.com/
https://github.com/phusion/passenger


Passenger 5 (コードネームはラプター)

「RaptorはどのようにしてUnicornの4倍、Puma, Torqueboxの2倍の速度を達成したのか」を読んでまとめてみた


とりあえず、standalone で動かしてみた。
(nginx は yum でインストールしたものなので、standalone しか道はない)
環境は centos7, rbenv
nginx --- passenger5 --- rails


インストール
rails のGemfile に追加# Gemfile
gem passenger, '5.0.5'

$ bundle install --path vendor/bundle

# rails のbin にpassenger コマンドを追加
# bin/passenger が作成される
bundle install --binstubs
(bin/passenger でなく、bundle exec passenger でもたぶんOKだと思う)

# 確認
$ bin/passenger start
 (デフォルトでは http://0.0.0.0:3000/ )

# nginx 経由でアクセスする。
# ドメインは raptor.local として、hosts に登録しておく。

$ vi /etc/nginx/conf.d/raptor.local.conf
server {
    listen 80;
    server_name raptor.local;

    location / {
        proxy_pass http://0.0.0.0:3000;
    }
}

# 変更を反映させる
$ systemctl reload nginx

これで、http://raptor.local/ でアクセスできる。
次に、systemd に登録してデーモン化する。

# サービス名を raptor.service としておく。
vi /etc/systemd/system/raptor.service

[Unit]
Description=Raptor (passenger5 standalone) with rails
After=syslog.target network.target nss-lookup.target

[Service]
Type=forking
User=hoge
Group=hoge
WorkingDirectory=/public/raptor
PIDFile=/public/raptor/tmp/pids/passenger.3000.pid

ExecStart=/public/raptor/bin/passenger start --daemonize
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl enable raptor
systemctl start raptor

# /etc/systemd/system/raptor.service を変更したら
$ systemctl daemon-reload


メモ、注意点、TODO
 - bin/passenger start --help でオプションの確認ができる
 -  --deamonize は必須
 - Unix domain の利用を考える (--socket オプション)
 - プロセス数の指定
 - この設定だと development でrails が動いているので変更も即座に反映してくれる。
(production 指定も調べる)



最後にrbenv について
rbenv はユーザーhoge だけに有効なので
(/home/hoge/.rbenv にインストールした)
bin/passenger の先頭を以下のように変更した。
#!/usr/bin/env ruby
  ↓
#!/home/hoge/.rbenv/versions/2.2.1/bin/ruby

/usr/local にインストールしたり、他の方法もあるのかもしれないけど、わからないので、自分(作業ユーザー) のrbenv を直接指定して動かした。

2015年2月16日月曜日

git でdiff-highlight を使いたい


centos6 にgit をソースからインストール
centos6 ではrpmパッケージのgit は1.7

$ sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker

$ tar -zxf git-1.7.2.2.tar.gz
$ cd git-1.7.2.2
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install

参考
 Gitのインストール


diff-highlight の設定

git のソースディレクトリにある
contrib/diff-highlight/diff-highlight
をコピー
/usr/local/bin へ

~/.gitconfig に追加
[pager]
    log = diff-highlight | less
    show = diff-highlight | less
    diff = diff-highlight | less

参考
Git の diff を美しく表示するために必要なたった 1 つの設定

2015年2月10日火曜日

rubocop のcopname


エラーを表示しているcop name を表示する方法

rubocop -D (D オプション)
これで、cop name も表示されるので、rubocop disable 時に迷わない

rubocop はoffence 毎にcop がいるということなのかな?

2015年2月9日月曜日

rails プロジェクトの作成メモ


環境 rails4.2

systemにrails を入れない場合
Rspec, Postgres を利用する。

プロジェクト名が projx の場合
$ mkdir projx
$ cd projx

# vender/bundle にrails を入れるため、仮のGemファイル作成
# ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] はnokogiri対策
source 'http://rubygems.org'

ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES'
gem 'rails'

# rails を入れる
$ bundle install --path vendor/bundle

$ bundle exec rails new . -T -d postgresql --skip-bundle --skip-sprockets
-T : テスト作成スキップ (Rspec を使う)
-d : デフォルトは sqlite (mysql, postgresql)
(rails new --help で確認)

# postgres を使う場合
# bundle installの前に以下を実行。
bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
(.bundle/config へ追加しておくほうがいいかも)

# Gemfile を編集
ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES' 追加 (先頭)
gem 'rspec-rails'  追加
gem 'therubyracer'  追加
gem 'coffee-rails'  削除

# gemは全てプロジェクト内に入れる
bundle install --path vendor/bundle

# Rspec 初期化
bundle exec rails g rspec:install

# spring の用意
bundle exec spring binstub --all