logrotate

slow-queries.logのlogrotateがしたい。

{mysqlのsrc}/upport-files/mysql-log-rotate
を参考にすると良い。
ちなみに中身は

# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/usr/local/mysql/var/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret>
# user= root
#
# where "<secret>" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !

/usr/local/mysql/var/mysqld.log {
        # create 600 mysql mysql
        notifempty
        daily
        rotate 3
        missingok
        compress
    postrotate
        # just if mysqld is really running
        if test -x /usr/local/mysql/bin/mysqladmin && \
           /usr/local/mysql/bin/mysqladmin ping &>/dev/null
        then
           /usr/local/mysql/bin/mysqladmin flush-logs
        fi
    endscript
}

mysqladminにはパスワードをかけていたので、my.cnfに

[mysqladmin]
password = <secret>
user= root

を追加しておく必要がある

携帯変換proxy

電車でよくはてブを眺めているわけだが、どうもUIが気に食わない。特にニュースサイト。

実際の記事にたどり着くまでなんども『次へ』のリンクを押さないといけないなんてあり得ん。何ページも続く長ったらしいカテゴリ一覧とか要らん。記事そのものにページ送りの有る場合、また『次へ』のリンクを押しまくらんといかん。何という地獄連鎖。

これはほんとにイライラする。ただでさえ地下鉄乗ってて電波が途切れ途切れなのに時間の無駄。

僕ならもっとうまく携帯変換proxy作るぜよ。

validationエラー時にdivで囲わない

今はerror_message_onにhtml_tagというオプションが追加されて、好きなtagで囲めるようになりました。以下の記事のような対応は必要ありません。コメント感謝

railsのデフォのvalidationエラーはdivタグを使用する。

通常時

 <%= error_message_on "post", "title" %> =>
    <div class="formError">can't be empty</div>
 <%= text_field "post", "title", :size=>"20" %> =>
    <div class="fieldWithErrors">
        <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />
    </div>

携帯の画面をつくっているとき、divを使われてしまうと改行してしまう。
docomoはtableタグが使えないので、tableタグで回避することもできない。

inputフィールドのdivタグ回避

active_record_helper.rbには

module ActionView
  class Base
    @@field_error_proc = 
            Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }
    cattr_accessor :field_error_proc
  end
  ...
end

と書かれているのでこいつを、environment.rbかapprication_controller.rbで

ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }

としてやればspanタグで回避してもらえる。

errorメッセージのdivタグ回避

active_record_helper.rbで

87:       def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
88:         if (obj = instance_variable_get("@#{object}")) && (errors = obj.errors.on(method))
89:           content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class)
90:         else 
91:           ''
92:         end
93:       end

となっているので、これをapplication_helperでoverrideする。

def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
  if (obj = instance_variable_get("@#{object}")) && (errors = obj.errors.on(method))
     content_tag("span", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class)
  else 
  ''
  end
end

これでspanで囲んでくれるようになった。
ただし、style指定はできないので、そのときはerror_message_on_hogeとかつくって使えばよろし。

force_quotes

通常、FasterCSVではき出されるCSVでは数字は、""で囲われない。

csv_str = FasterCSV.generate do |csv|
  csv << %w[1, 2]
  csv << %w[3, 4]
  csv << %w[5, 6]
end
puts csv_str
#=> 1,2
#=> 3,4
#=> 5,6

FasterCSVには渡せるオプションがいくつかあって、force_quotesを使えば""で囲んでくれる。

csv_str = FasterCSV.generate(:force_quotes=>true) do |csv|
  csv << %w[1, 2]
  csv << %w[3, 4]
  csv << %w[5, 6]
end
puts csv_str
#=> "1","2"
#=> "3","4"
#=> "5","6"

ほかに渡せるオプションとデフォルト値一覧

オプション デフォルト値 意味
:col_sep "," 列の区切り
:row_sep :auto 行の区切り
:quote_char '"' セルの区切り
:converters nil 未調査
:unconverted_fields nil 未調査
:headers false 未調査
:return_headers false 未調査
:header_converters nil 未調査
:skip_blanks false 未調査
:force_quotes false 強制的にquote_charで囲むかどうか

polymorphic association で Eager Loading

polymorphic association使おうとしたらEagerLoadできないでやんの。
いろいろ探してたら、

http://software.haruska.com/2007/08/eager-loading-polymorphic-associations.html

このページ見つけたのだけど、ネストした:includeが使えない。
おしい。非常におしい。

と思っていたら、タイミングが良いことに昨日本家にパッチがあがっていた。

There is an error condition with nested includes in this situation:

ShapeExpression.find :all, :include => [:shape, { :paint => :non_poly } ]

In the include used above, :paint is a polymorphic relationship, therefore, the association to :non_poly is not necessarily the same for all :paint objects. However, as currently implemented, the eager include code will try to load all :non_poly associations using the association information from the first :paint object it checks. The test 'eager_load_nested_include_test.rb' demonstrates the problem using a simple object graph (with class defs provided at the bottom of the file).

The patched version of AssociationPreload#preload_one_association first sorts the record by class then preloads each group of sorted records.

そうそう。これがしたかったんよね。
試してみる。

radio_button_tagでobserve_fieldが使えない

ラジオボタンの選択で、フォームに入力する項目を切り替えようとしたのだけど、ぜんぜんうまくいかない。
ググったら出てきた。やはりradio_button_tagでobserve_fieldが使えないらしい。

This is what I had and it doesn't work.

<%= radio_button_tag 'ship_method', 'pickup', :checked => true -%> Pickup

<%= radio_button_tag 'ship_method', 'deliver' -%> Deliver

<%= observe_field 'ship_method_pickup',
:url => { :controller => 'checkout', :action => 'ship_method_select' },
:on => 'click', :with => 'method' -%>

<%= observe_field 'ship_method_deliver',
:url => { :controller => 'checkout', :action => 'ship_method_select' },
:on => 'click', :with => 'method' -%>

This is what I ended up with

<input type="radio" name='ship_method' id='ship_method_pickup'
 checked="checked" value="pickup"
onclick="<%=
remote_function(
:url => {:controller => 'checkout', :action => 'ship_method_select', :method => 'pickup'}
)-%>" />

気持ち悪い。