2 # 例: @search = SearchForm.new(params[:search])
4 class SearchForm < ActiveForm
5 VALID_NAME = /^[a-zA-Z_][a-zA-Z0-9_]*$/
6 include ActiveRecord::Validations
10 attributes.each do | name, value |
12 if (m = name.match(/_from$/))
14 name_to = prefix+'_to'
19 unless [from, to].any?{|v| v =~ /\D/} # 数字以外が含まれていない
20 from = from.strftime("%Y%m%d%H%M%S").to_i
21 to = to.strftime("%Y%m%d%H%M%S").to_i
24 errors.add name, 'の範囲指定が不正です。'
25 errors.add name_to, 'の範囲指定が不正です。'
31 # ActiveForm からコピペして改造
33 attributes = instance_variables
34 attributes.delete("@errors")
35 attributes.inject({}) do |hash, attribute|
36 hash[attribute[1..-1]] = instance_variable_get(attribute)
42 hash.each do | key, value |
43 next if value.nil? || value == ''
46 if (m = name.match(/\((\d)i\)$/))
49 if (date = parse_date_select(hash, name))
52 elsif value.is_a?(String) &&
53 value =~ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/
55 send(name+'=', Time.zone.parse(value))
62 def method_missing(key, *args)
67 if name.size >= 2 && name[-1] == ?= # name =~ /^.+=$/
69 name = name[0, name.size-1]
71 instance_variable_set('@'+name, value) if name =~ VALID_NAME
74 instance_variable_get('@'+name) if name =~ VALID_NAME
81 def parse_date_select(params, name)
82 arr = (1..6).map do |i|
83 params["%s(%di)" % [name, i]]
88 year,month,day,hour,min,sec = arr
93 if year || month || day
108 month = now.month.to_s
112 month = now.month.to_s
144 Time.zone.local(year.to_i,month.to_i,day.to_i,hour.to_i,min.to_i,sec.to_i)
146 Time.zone.local(year.to_i,month.to_i,day.to_i)
153 def set_field_names(field_names = {})
154 @field_names = HashWithIndifferentAccess.new unless @field_names
155 @field_names.update(field_names)
158 alias_method :_human_attribute_name, :human_attribute_name
159 def human_attribute_name(attribute_key_name)
160 if @field_names && @field_names[attribute_key_name]
161 @field_names[attribute_key_name]
163 _human_attribute_name(attribute_key_name)
171 def self_and_descendants_from_active_record
175 def human_name(*args)