forked from heartcombo/simple_form
/
misc_helpers.rb
90 lines (77 loc) · 2.37 KB
/
misc_helpers.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
module MiscHelpers
def store_translations(locale, translations, &block)
begin
I18n.backend.store_translations locale, translations
yield
ensure
I18n.reload!
end
end
def assert_no_select(selector, value = nil)
assert_select(selector, :text => value, :count => 0)
end
def swap(object, new_values)
old_values = {}
new_values.each do |key, value|
old_values[key] = object.send key
object.send :"#{key}=", value
end
yield
ensure
old_values.each do |key, value|
object.send :"#{key}=", value
end
end
def swap_wrapper(name=:default, wrapper=self.custom_wrapper)
old = SimpleForm.wrappers[name]
SimpleForm.wrappers[name] = wrapper
yield
ensure
SimpleForm.wrappers[name] = old
end
def custom_wrapper
SimpleForm.build :tag => :section, :class => "custom_wrapper" do |b|
b.use :another, :class => "another_wrapper" do |ba|
ba.use :label
ba.use :input
end
b.use :error_wrapper, :tag => :div, :class => "error_wrapper" do |be|
be.use :error, :tag => :span, :class => "omg_error"
end
b.use :hint, :tag => :span, :class => "omg_hint"
end
end
def custom_form_for(object, *args, &block)
simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block)
end
def custom_mapping_form_for(object, *args, &block)
simple_form_for(object, *(args << { :builder => CustomMapTypeFormBuilder }), &block)
end
def with_concat_form_for(object, &block)
concat simple_form_for(object, &block)
end
def with_concat_custom_form_for(object, &block)
concat custom_form_for(object, &block)
end
def with_concat_custom_mapping_form_for(object, &block)
concat custom_mapping_form_for(object, &block)
end
def with_form_for(object, *args, &block)
with_concat_form_for(object) do |f|
f.input(*args, &block)
end
end
def with_input_for(object, attribute_name, type, options={})
with_concat_form_for(object) do |f|
f.input(attribute_name, options.merge(:as => type))
end
end
end
class CustomFormBuilder < SimpleForm::FormBuilder
def input(attribute_name, *args, &block)
super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block)
end
end
class CustomMapTypeFormBuilder < SimpleForm::FormBuilder
map_type :custom_type, :to => SimpleForm::Inputs::StringInput
end