heartcombo--simple_form/test/support/misc_helpers.rb

139 lines
4.0 KiB
Ruby
Raw Normal View History

module MiscHelpers
def store_translations(locale, translations, &block)
2012-02-09 13:33:34 +00:00
I18n.backend.store_translations locale, translations
yield
ensure
I18n.reload!
I18n.backend.send :init_translations
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
2010-09-26 22:24:30 +00:00
2011-09-04 09:31:24 +00:00
def swap_wrapper(name=:default, wrapper=self.custom_wrapper)
old = SimpleForm.wrappers[name]
SimpleForm.wrappers[name] = wrapper
yield
ensure
SimpleForm.wrappers[name] = old
end
2011-09-03 17:04:40 +00:00
def custom_wrapper
SimpleForm.build :tag => :section, :class => "custom_wrapper", :pattern => false do |b|
b.use :pattern
2012-02-16 14:07:46 +00:00
b.wrapper :another, :class => "another_wrapper" do |ba|
2011-09-03 17:04:40 +00:00
ba.use :label
ba.use :input
end
2012-02-16 14:07:46 +00:00
b.wrapper :error_wrapper, :tag => :div, :class => "error_wrapper" do |be|
be.use :error, :wrap_with => { :tag => :span, :class => "omg_error" }
2011-09-03 17:04:40 +00:00
end
2012-02-18 14:47:02 +00:00
b.use :hint, :wrap_with => { :class => "omg_hint" }
2011-09-03 17:04:40 +00:00
end
end
def custom_wrapper_with_wrapped_input
SimpleForm.build :tag => :div, :class => "custom_wrapper" do |b|
b.wrapper :tag => :div, :class => 'elem' do |component|
component.use :label
component.use :input, :wrap_with => { :tag => :div, :class => 'input' }
end
end
end
def custom_wrapper_with_wrapped_label
SimpleForm.build :tag => :div, :class => "custom_wrapper" do |b|
b.wrapper :tag => :div, :class => 'elem' do |component|
component.use :label, :wrap_with => { :tag => :div, :class => 'label' }
component.use :input
end
end
end
def custom_wrapper_without_top_level
SimpleForm.build :tag => false, :class => 'custom_wrapper_without_top_level' do |b|
b.use :label_input
2012-02-16 14:07:46 +00:00
b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
b.use :error, :wrap_with => { :tag => :span, :class => :error }
end
end
2012-06-29 19:13:15 +00:00
def custom_wrapper_without_class
SimpleForm.build :tag => :div, :wrapper_html => { :id => 'custom_wrapper_without_class' } do |b|
b.use :label_input
end
end
def custom_wrapper_with_label_html_option
SimpleForm.build :tag => :div, :class => "custom_wrapper", :label_html => { :class => 'extra-label-class' } do |b|
b.use :label_input
end
end
2012-10-05 16:45:20 +00:00
def custom_wrapper_with_wrapped_label_input
SimpleForm.build :tag => :section, :class => "custom_wrapper", :pattern => false do |b|
b.use :label_input, :wrap_with => { :tag => :div, :class => :field }
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(*args, &block)
2012-03-01 06:05:15 +00:00
concat simple_form_for(*args, &(block || proc {}))
end
def with_concat_fields_for(*args, &block)
concat simple_fields_for(*args, &block)
2010-09-26 22:24:30 +00:00
end
2011-01-05 19:52:49 +00:00
def with_concat_custom_form_for(*args, &block)
concat custom_form_for(*args, &block)
2011-01-05 19:52:49 +00:00
end
def with_concat_custom_mapping_form_for(*args, &block)
concat custom_mapping_form_for(*args, &block)
2011-01-05 19:52:49 +00:00
end
def with_form_for(object, *args, &block)
with_concat_form_for(object) do |f|
f.input(*args, &block)
end
end
2011-05-16 10:18:17 +00:00
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
2011-01-05 19:52:49 +00:00
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