railsでscopeを色々チェインする時にそういや二回joinしたらどうなるんだろうと思ってやってみた。(rails4)

>> User.joins(:events).to_sql
=> "SELECT \"users\".* FROM \"users\" INNER JOIN \"events\" ON \"events\".\"user_id\" = \"users\".\"id\""
>> User.joins(:events).joins(:events).to_sql
=> "SELECT \"users\".* FROM \"users\" INNER JOIN \"events\" ON \"events\".\"user_id\" = \"users\".\"id\""

ホッ、変わらないみたい。

herokuでsmtpを使うためにsendgrid addonを入れる。(smtpにgmailを使う場合は要らない)

$ heroku addons:add sendgrid:starter
# config/environment.rb:

(snip)

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',                                                                                            
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],                                                                         
  :password       => ENV['SENDGRID_PASSWORD'],                                                                         
  :domain         => 'heroku.com',
  :enable_starttls_auto => true
}
# config/initializers/exception_notification.rb:
AppName::Application.config.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "[AppName] ",
    :sender_address => %{"notifier" <notifier@example.com>},
    :exception_recipients => %w{komagata@example.com}
  }
# Gemfile:
gem 'exception_notification'
# config/environments/production.rb:

(snip)

  # config.action_mailer.raise_delivery_errors = false
  config.action_mailer.raise_delivery_errors = true

(snip)

  config.action_mailer.perform_deliveries = true
end

modelのscope書く時、現在時間を使う時はテストしやすいようにデフォルト値ありの引数を与えることにしてるんだけど、

「あれ、引数だけ先に評価されちゃわなかったっけな?」

と心配になったので試してみた。

foo = ->(now = Time.now) {
  puts now, Time.now
}

sleep 10

foo.call
% ruby arg.rb
2013-07-11 16:07:16 +0900
2013-07-11 16:07:16 +0900

杞憂に終わった。

Sunzi: Server provisioning utility for minimalists

"The supreme art of war is to subdue the enemy without fighting." - Sunzi

いわゆるchefのようなサーバープロビジョニングツール。

特徴

  • レシピはshell script
  • 冪等性?犬にくれてやれ!
  • root前提
  • デフォルト設定との差分だけを設定する
  • サーバー側に入れなきゃいけないソフトは無し(shell scriptだから)

ミニマリスト向けツールに見えますが意外と高機能で、基本的にknife-soloのようにクライアントからサーバーに対して使うの前提だし、berkshelfのようにリモートのrecipeを取り込む機能があるし、erbのテンプレートやweb, dbみたいなrole機能もあります。

そもそも俺はdebian(今だったらwheezy)しか使わないし、冪等性とか要らないし、VPSをサクッと設定したいだけなのでちょうどハマりました。

recipes/ruby.sh:

apt-get install -y ruby1.9.3

例えばrubyのrecipeはこんな感じ(少しだけ賢いsunzi.installの方がおすすめですが)。shell scriptなので他人のrecipeがいきなり読めるのがおっさんエンジニアには嬉しい。ちょっとした設定書き換えをsedでやってるの見るとホッとします。

これだったらちょこっと設定してcapistranoでrailsアプリデプロイするとこまで道筋がすごく見えやすいです。

vagrantでsandbox rollbackしながらrecipeを書いてくのが快適です。

疑問

仕事に使おうとしてるんですがいくつか疑問点が。

  • 鍵認証のrootユーザー、sshはデフォルトの22番で運用するのが普通?
  • capistranoでdeploy(=appサーバーの実行者)もrootユーザーでやるの?
  • ソフトのバージョンアップとかはどういう感じでやるの?

$ gem install icalendar
requre 'icalendar'

calendar = Icalendar::Calendar.new                                                                                 
event = Icalendar::Event.new                                                                                       
event.start = Date.new(2013, 7, 1)
event.end = Date.new(2013, 7, 8)
event.summary = "Foo Festival"
event.description = "Greatest event in the world."
event.location = "Hatsudai Tokyo Japan"
calendar.add_event(event)

puts calendar.to_ical

railsでリンクをクリックしたらiCalファイルダウンロード → iCal起動+登録ってやりたい場合。

class FooController < ApplicationController
  def calendar
    calendar = Icalendar::Calendar.new

    # implementation...

    headers['Content-Type'] = 'text/calendar; charset=UTF-8'
    render :inline, layout: false, text: calendar.to_ical
  end
end
$ heroku labs:enable user-env-compile

これすると行ける。理由はわからん・・・。

怖話(こわばな)でラジオにCMを出しました。

怖話が山口敏太郎のラジオ番組に進出!ラジオCMになりました! - 怖話からのお知らせ

世界最大の怖い話サイト「怖話」がラジオに進出!

オカルト評論家 山口敏太郎のラジオ番組内でラジオCMが放送されます!

以前発表いたしました「怖い話グランプリ」の期待の高まるなか、タッグを組んでいる山口敏太郎のラジオ番組「日本大好き」に怖話のラジオCMが流れることになりました!

音源は@machidaさんが知り合いに声も録音して作ってくれました。

@machidaさんはWebデザインよりDTM歴の方が圧倒的に長いのでメインで使われてるBGMも自作です。PS版弟切草のCMのイメージで作ったと言ってました。

ハマりポイント。

VMを壊す例として$ sudo rm -rf / --no-preserve-rootすると/vagrantにマウントされてるMac上のディレクトリまでVagrantfileごと消えるのでおすすめできない。

vagrantのplugin、saharaを使う

saharaを入れる。

% vagrant plugin install sahara

sandboxモードに入る。

% vagrant sandbox on

壊す。

% vagrant ssh
$ sudo rm -rf /usr
$ ls /usr
ls: cannot access /usr: No such file or directory
$ exit

戻す。

% vagrant sandbox rollback
% vagrant ssh
$ ls /usr/
bin  games  include  lib  local  sbin  share  src

ワオ、戻ってる!

昨日、隣の仙川でねじり鉢巻した建設関係っぽい軽トラのおっさんが、前にいるチャリンコのおっさんに対して、

「道の真ん中でフラフラと走ってんじゃねーよ!」

と非常に大きな声でスムーズに文句言ってた。

「ったく、道の真ん中でフラフラしてんじゃねーよ!」

あ、二回言った。

ものすごく怒ってるという感じではなくて、単に思ってることを一番気持ちいい声量で発声してる感じ。

あんな風に思ってることを微塵の遠慮もなく表明できたらストレス溜まらないだろうなあ。

現状渡しされてパスワードがわからない時。

mysql> update wp_users set user_pass = '$P$BQ6n8cNLFJBJyxYYoPK3bDWymBXILO.' where user_login = 'admin';

これでadminユーザーのパスワードがfooになります。