protocalendar.js 用の rails ヘルパメソッド(のひな形)を公開しました
今回、本当はモデルの特定アトリビュートだけにバリデーションを実行するプラグイン「valid_attributes」を公開しようかと思っていたのですが、
個人的なサイトの方で公開してしまったので、今回は protocalendar.js の ruby on rails 用ヘルパメソッド(のひな形)を公開したいと思います。
オプション関係など完璧ではないのですが、独自ヘルパメソッドを作るベースになればと思います。
action_view_ext.rb
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("%Y/%m/%d")
tag.object.write_attribute(method, time) unless tag.nil?
end
return tag.to_input_field_tag("text", options)
end
end
end
end
デフォルトで lang:’ja’ を使っているため、日付フォーマットが yyyy/mm/dd になるように、
FormHelper を拡張して、text_field_for_date で小細工をしています。
cakephp 用のヘルパメソッドも近々公開します。



スプーキーズラボブログは株式会社スプーキーズの技術関連の情報を発信するブログです。プロダクト一覧、お問い合わせなどは