This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
/
adapter.rb
114 lines (89 loc) · 2.89 KB
/
adapter.rb
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module LiveValidations
# The base class of an adapter.
class Adapter
attr_reader :data, :active_record_instance, :rendered_attributes
def initialize(active_record_instance)
@active_record_instance = active_record_instance
@rendered_attributes = []
# Initialize the data hash with the 'setup' call from
# the validator.
@data = {}
self.class.setup_proc.call(self)
end
def run_validations
active_record_instance.validation_callbacks.each do |callback|
next unless callback_has_visible_attributes?(callback)
method = callback.options[:validation_method]
validation_hook = self.class.validation_hooks[method]
if validation_hook
validation_hook.setup(&self.class.setup_proc)
validation_hook.run_validation(self, callback)
end
end
end
# Is called whenever a field helper is called (<%= f.foo %>).
def renders_attribute(attribute)
@rendered_attributes << attribute
end
# Tells the form builder wether or not the adapter will
# alter tag attributes in the generated forms. Overrided
# per-adapter.
def alters_tag_attributes?
false
end
def [](key)
data[key]
end
def []=(key, value)
data[key] = value
end
def self.validates(name, &block)
self.validation_hooks[name] = ValidationHook.new(&block)
end
def self.renders_inline(&block)
@inline_javascript_proc = block
end
def self.response(name, &block)
self.validation_responses[name] = ValidationResponse.new(&block)
end
def self.setup(&block)
@setup_proc = block
end
def self.form_for_options(&block)
@form_for_options_proc = block
end
def render_inline_javascript
self.class.inline_javascript_proc.call(self)
end
def utilizes_inline_javascript?
self.class.inline_javascript_proc && !data.blank?
end
# The DOM prefix, e.g. "post" for Post. Used to reference DOM ids
# and DOM names, such as "post[title]" and "post_title".
def prefix
active_record_instance.class.name.downcase
end
def handle_form_for_options(options)
self.class.form_for_options_proc.call(options) if self.class.form_for_options_proc
end
private
def callback_has_visible_attributes?(callback)
callback.options[:attributes] && callback.options[:attributes].any? {|attribute| rendered_attributes.include?(attribute)}
end
def self.validation_hooks
@validation_hooks ||= {}
end
def self.validation_responses
@validation_responses ||= {}
end
def self.inline_javascript_proc
@inline_javascript_proc
end
def self.setup_proc
@setup_proc
end
def self.form_for_options_proc
@form_for_options_proc
end
end
end