スプーキーズの中の人。

スプーキーズの中の人が徒然なるままに、垂れ流します。

protocalendar.js 用の rails ヘルパメソッド(のひな形)を公開しました

今回、本当はモデルの特定アトリビュートだけにバリデーションを実行するプラグイン「valid_attributes」を公開しようかと思っていたのですが、

個人的なサイトの方で公開してしまったので、今回は protocalendar.js の ruby on rails 用ヘルパメソッド(のひな形)を公開したいと思います。

オプション関係など完璧ではないのですが、独自ヘルパメソッドを作るベースになればと思います。

action_view_ext.rb [ruby] module ActionView module Helpers module FormHelper def date_field(object_name, method, options = {}) result = text_field_for_date(object_name, method, options) styleId = options[:id] ? options[:id] : object_name.to_s + '_' + method.to_s contents = "InputCalendar.createOnLoaded('#{styleId}', {"; lang = options[:lang] ? "lang:#{options[:lang]} " : "lang:'ja'" contents << lang contents << ", startYear:#{options[:startYear]} " if options[:startYear] contents << ", endYear:#{options[:endYear]} " if options[:endYear] contents << "});"; return (result + javascript_tag(contents)) end

  protected

  def text_field_for_date(object_name, method, options = {})
    tag = InstanceTag.new(object_name, method, self, nil, options.delete(:object))
    result = tag.value(tag.object)
    unless result.nil?
      time = result.strftime(&quot;%Y/%m/%d&quot;)
      tag.object.write_attribute(method, time) unless tag.nil?
    end
    return tag.to_input_field_tag(&quot;text&quot;, options)
  end
end

end end [/ruby] デフォルトで lang:'ja' を使っているため、日付フォーマットが yyyy/mm/dd になるように、

FormHelper を拡張して、text_field_for_date で小細工をしています。

cakephp 用のヘルパメソッドも近々公開します。