Module: SimplyHelpful::FormHelper

Defined in:
lib/simply_helpful/form_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



118
119
120
121
122
123
# File 'lib/simply_helpful/form_helper.rb', line 118

def self.included(base)
	base.class_eval do
		alias_method_chain( :method_missing, :wrapping 
			) unless base.respond_to?(:method_missing_without_wrapping)
	end
end

Instance Method Details

#_wrapped_date_spans(object_name, method, options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/simply_helpful/form_helper.rb', line 48

def _wrapped_date_spans(object_name,method,options={})
	object = instance_variable_get("@#{object_name}")
	_wrapped_spans(object_name,method,options.update(
		:value => mdy(object.send(method)) ) )
end

#_wrapped_spans(object_name, method, options = {}) ⇒ Object

This is NOT a form field



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simply_helpful/form_helper.rb', line 29

def _wrapped_spans(object_name,method,options={})
	s =  "<span class='label'>#{options[:label_text]||method}</span>\n"
	value = if options[:value]
		options[:value]
	else
		object = instance_variable_get("@#{object_name}")
		value = object.send(method)
		value = (value.to_s.blank?)?'&nbsp;':value
	end
	s << "<span class='value'>#{value}</span>"
end

#_wrapped_y_n_dk_spans(object_name, method, options = {}) ⇒ Object Also known as: _wrapped_yndk_spans



41
42
43
44
45
# File 'lib/simply_helpful/form_helper.rb', line 41

def _wrapped_y_n_dk_spans(object_name,method,options={})
	object = instance_variable_get("@#{object_name}")
	_wrapped_spans(object_name,method,options.update(
		:value => y_n_dk(object.send(method)) ) )
end

#_wrapped_yes_or_no_spans(object_name, method, options = {}) ⇒ Object

This is NOT a form field



81
82
83
84
85
86
# File 'lib/simply_helpful/form_helper.rb', line 81

def _wrapped_yes_or_no_spans(object_name,method,options={})
	object = instance_variable_get("@#{object_name}")
	s =  "<span class='label'>#{options[:label_text]||method}</span>\n"
	value = (object.send("#{method}?"))?'Yes':'No'
	s << "<span class='value'>#{value}</span>"
end

#date_text_field(object_name, method, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/simply_helpful/form_helper.rb', line 62

def date_text_field(object_name,method,options={})
	format = options.delete(:format) || '%m/%d/%Y'
	tmp_value = if options[:value].blank? #and !options[:object].nil?
		object = options[:object]
		tmp = object.send("#{method}") ||
		      object.send("#{method}_before_type_cast")
	else
		options[:value]
	end
	begin
		options[:value] = tmp_value.to_date.try(:strftime,format)
	rescue NoMethodError, ArgumentError
		options[:value] = tmp_value
	end
	options.update(:class => [options[:class],'datepicker'].compact.join(' '))
	text_field( object_name, method, options )
end

#field_wrapper(method, options = {}, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/simply_helpful/form_helper.rb', line 21

def field_wrapper(method,options={},&block)
	classes = [method,options[:class]].compact.join(' ')
	s =  "<div class='#{classes} field_wrapper'>\n"
	s << yield 
	s << "\n</div><!-- class='#{classes}' -->"
end

#hour_select(object_name, method, options = {}, html_options = {}) ⇒ Object



96
97
98
99
100
101
# File 'lib/simply_helpful/form_helper.rb', line 96

def hour_select(object_name, method, 
		options={}, html_options={})
	select(object_name, method,
		(1..12),
		{:include_blank => 'Hour'}.merge(options), html_options)
end

#mdy(date) ⇒ Object



3
4
5
# File 'lib/simply_helpful/form_helper.rb', line 3

def mdy(date)
	( date.nil? ) ? '&nbsp;' : date.strftime("%m/%d/%Y")
end

#meridiem_select(object_name, method, options = {}, html_options = {}) ⇒ Object



111
112
113
114
115
116
# File 'lib/simply_helpful/form_helper.rb', line 111

def meridiem_select(object_name, method, 
		options={}, html_options={})
	select(object_name, method,
		['AM','PM'], 
		{:include_blank => 'Meridiem'}.merge(options), html_options)
end

#method_missing_with_wrapping(symb, *args, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/simply_helpful/form_helper.rb', line 125

def method_missing_with_wrapping(symb,*args, &block)
	method_name = symb.to_s
	if method_name =~ /^wrapped_(.+)$/
		unwrapped_method_name = $1
#
#	It'd be nice to be able to genericize all of the
#	wrapped_* methods since they are all basically
#	the same.
#		Strip of the "wrapped_"
#		Label
#		Call "unwrapped" method
#

		object_name = args[0]
		method      = args[1]

		content = field_wrapper(method,:class => unwrapped_method_name) do
			s = if respond_to?(unwrapped_method_name)
				options    = args.detect{|i| i.is_a?(Hash) }
				label_text = options.delete(:label_text) unless options.nil?
				if unwrapped_method_name == 'check_box'
					send("#{unwrapped_method_name}",*args,&block) <<
					label( object_name, method, label_text )
				else
					label( object_name, method, label_text ) <<
					send("#{unwrapped_method_name}",*args,&block)
				end
			else
				send("_#{method_name}",*args,&block)
			end

			s << (( block_given? )? capture(&block) : '')
#				send("_#{method_name}",*args) << 
#					(( block_given? )? capture(&block) : '')
		end
		if block_called_from_erb?(block)
			concat(content)
		else
			content
		end
	else
		method_missing_without_wrapping(symb,*args, &block)
	end
end

#minute_select(object_name, method, options = {}, html_options = {}) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/simply_helpful/form_helper.rb', line 103

def minute_select(object_name, method, 
		options={}, html_options={})
	minutes = (0..59).to_a.collect{|m|[sprintf("%02d",m),m]}
	select(object_name, method,
		minutes,
		{:include_blank => 'Minute'}.merge(options), html_options)
end

#sex_select(object_name, method, options = {}, html_options = {}) ⇒ Object Also known as: gender_select



54
55
56
57
58
59
# File 'lib/simply_helpful/form_helper.rb', line 54

def sex_select(object_name, method, 
		options={}, html_options={})
	select(object_name, method,
		[['male','M'],['female','F']],
		options, html_options)
end

#time_mdy(time) ⇒ Object



7
8
9
# File 'lib/simply_helpful/form_helper.rb', line 7

def time_mdy(time)
	( time.nil? ) ? '&nbsp;' : time.strftime("%I:%M %p %m/%d/%Y")
end

#y_n_dk(value) ⇒ Object Also known as: yndk



11
12
13
14
15
16
17
18
# File 'lib/simply_helpful/form_helper.rb', line 11

def y_n_dk(value)
	case value
		when 1   then 'Yes'
		when 2   then 'No'
		when 999 then "Don't Know"
		else '&nbsp;'
	end
end

#y_n_dk_select(object_name, method, options = {}, html_options = {}) ⇒ Object Also known as: yndk_select



88
89
90
91
92
93
# File 'lib/simply_helpful/form_helper.rb', line 88

def y_n_dk_select(object_name, method, 
		options={}, html_options={})
	select(object_name, method,
		[['Yes',1],['No',2],["Don't Know",999]],
		{:include_blank => true}.merge(options), html_options)
end