-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathactive_record_helper.rb
More file actions
98 lines (86 loc) · 3.31 KB
/
active_record_helper.rb
File metadata and controls
98 lines (86 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
if defined?(Rails::Generator)
module Rails
module Generator
class GeneratedAttribute
def field_type_with_enumerated_attribute
return (@field_type = :enum_select) if type == :enum
field_type_without_enumerated_attribute
end
alias_method_chain :field_type, :enumerated_attribute
end
end
end
end
if defined?(ActionView::Base)
module ActionView
module Helpers
#form_options_helper.rb
module FormOptionsHelper
#def select
def enum_select(object, method, options={}, html_options={})
InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options)
end
def enum_radio(object, method, options={}, html_options={})
InstanceTag.new(object, method, self, options.delete(:object)).to_enum_radio_tag(options, html_options)
end
end
class InstanceTag
def to_enum_select_tag(options, html_options={})
if self.object.respond_to?(method_name.to_sym)
column = self.object.column_for_attribute(method_name)
if (value = self.object.__send__(method_name.to_sym))
options[:selected] ||= value.to_s
else
options[:include_blank] = column.null if options[:include_blank].nil?
end
end
to_select_tag(column.limit, options, html_options)
end
def to_enum_radio_tag(options, html_options={})
if self.object.respond_to?(method_name.to_sym)
column = self.object.column_for_attribute(method_name)
if (value = self.object.__send__(method_name.to_sym))
options[:selected] ||= value.to_s
else
options[:include_blank] = column.null if options[:include_blank].nil?
end
end
values = column.limit
raise ArgumentError, "No values for enum select tag" unless values
tag_text = ''
template = options.dup
template.delete(:selected)
values.each do |enum|
opts = template.dup
opts['checked'] = 'checked' if options[:selected] and options[:selected] == enum
opts['id'] = "#{opts['id']}_#{enum}"
tag_text << to_radio_button_tag(enum, opts)
tag_text << "<label>#{enum} "
tag_text << "</label>"
end
tag_text.html_safe
end
#initialize record_name, method, self
if respond_to?(:to_tag)
def to_tag_with_enumerated_attribute(options={})
#look for an enum
if (column_type == :enum && self.object.class.respond_to?(method_name.to_sym))
to_enum_select_tag(options)
else
to_tag_without_enumerated_attribute(options)
end
end
alias_method_chain :to_tag, :enumerated_attribute
end
end
class FormBuilder
def enum_select(method, options={}, html_options={})
@template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
end
def enum_radio(method, options={}, html_options={})
@template.enum_radio(@object_name, method, objectify_options(options), @default_options.merge(html_options))
end
end
end
end
end