ActiveRecord の with_options に関する注意書き

with_options (Object) - APIdock にて、ActiveRecord の with_options について注意書きが書かれていたのでメモ。 和訳はちょっと雑です。


with_options はネストすることができ、receiver に引き継がれます。

メモ: ネスト階層は継承したデフォルトオプション値を自身のオプションとマージします。

class Post < ActiveRecord::Base
  with_options if: :persisted?, length: { minimum: 50 } do
    validates :content, if: -> { content.present? }
  end
end

このコードは以下と等価です。

validates :content, length: { minimum: 50 }, if: -> { content.present? }

継承されたデフォルトの if キーは無視されます。

/etc/shadow の password field

/etc/shadow の password field に出現する "!" や "*" についてのメモ。

MAN USERMOD(8)

OPTIONS
       The options which apply to the usermod command are:

       -L, --lock
           Lock a user's password. This puts a '!' in front of the encrypted password, effectively disabling the password. You can't use this option with -p or -U.

REFERENCES

superuser.com

ubuntuforums.org

deploy:assets:backup_manifest が失敗する

Rails を 4.2.0 から 4.2.4 にアップグレードしようと capistrano でデプロイしたらエラーが発生した。

 INFO [e43c382f] Running /usr/bin/env cp /home/myapp/myapp/releases/20150916085015/public/assets/manifest* /home/myapp/myapp/releases/20150916085015/assets_manifest_backup on ***.***.***.***

SSHKit::Command::Failed: cp exit status: 1
cp stdout: Nothing written
cp stderr: cp: cannot stat ‘/home/myapp/myapp/releases/20150916085015/public/assets/manifest*’: No such file or directory

Tasks: TOP => deploy:assets:backup_manifest
(See full trace by running task with --trace)

manifest を cp できてない模様。

capistrano に本件の issue が上がっており、コメントに

Rails 4.2.4, it works after upgrading capistrano-rails to 1.1.3 and capistrano-bundler to 1.1.4

とのこと。

capistrano-rails を 1.1.3 上げたら、エラーは発生せずデプロイは成功。

単なるメモになってしまったが、覚えてないとハマりそうなので備忘録として。

thorでサブコマンドを指定しない方法

thorCLI を作成するのによく使われる gem (らしい)ですが、サブコマンドを指定せずに実行する方法が Github Wiki公式サイト に見当たらず、小一時間悩んだのでメモっておく。

例えば こんなことがやりたい。

$ bin/cli.rb -l ja world
こんにちは world

$ bin/cli.rb -l en world
Hello world

解決方法

default_command と 可変長引数を使えばよい。

#!/usr/bin/env ruby
require 'thor'

module MyApp
  class CLI < Thor
    default_command :hello

    desc 'hello', 'description here'
    method_option :lang, type: :string,  aliases: ['-l']
    def hello *args
      case options[:lang]
      when "ja"
        hello = 'こんにちは'
      else
        hello = 'Hello'
      end

      puts "#{hello} #{args.join(', ')}"
    end
  end

end

MyApp::CLI.start

Rails で発生した Error を NewRelicに通知する

Rails アプリでは newrelic_rpm を Gemfile に書いておけば、よしなにエラーを NewRelic のダッシュボードに通知してくれます。

が、

rescue_from Exception, with: :render_500

def render_500(e = nil)
  logger.error e
  render file: 'public/500', status: :internal_server_error, layout: false   
end

のようなコードで Exception を握りつぶしてるとダッシュボードで通知されません。

Sending New Relic handled errors によれば、

Use the Ruby Agent API NewRelic::Agent.notice_error within your error handler, which tells the agent to send the error data to New Relic. This API call takes the exception and an optional options hash. Use this format:

とのことなので、

def render_500(e = nil)
  logger.error e
  NewRelic::Agent.notice_error(e)

  render file: 'public/500', status: :internal_server_error, layout: false
end

と書けば ok です。

XMLにおいて アンパサンド(&)はエスケープが必要

基本的すぎる話かもしれないが... sitemap_generator で XML を生成していて気付いた。

サイトマップ ファイルは UTF-8 エンコードで作成する必要があります (ファイルを保存すると、通常は UTF-8 エンコードで保存されます)。 他の XML ファイルと同じように、URL などのデータ値では、次の文字にエンティティのエスケープ コードを使用する必要があります。

文字 エスケープコード
アンパサンド & &amp;
一重引用符 ' &apos;
二重引用符 " &quot;
不等記号 (より大) > &gt;
不等記号 (より小) < &lt;

http://www.sitemaps.org/ja/protocol.html#escaping

W3C Recommendation には

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings " & " and " < " respectively.

と書かれている。

知らなかったー