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

255 lines
7.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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)
2013-01-28 21:02:59 +00:00
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
2013-02-27 21:12:37 +00:00
def stub_any_instance(klass, method, value)
klass.class_eval do
alias_method :"new_#{method}", method
define_method(method) do
if value.respond_to?(:call)
value.call
else
value
end
end
end
yield
ensure
klass.class_eval do
undef_method method
alias_method method, :"new_#{method}"
undef_method :"new_#{method}"
end
end
2015-03-25 11:17:42 +00:00
def swap_wrapper(name = :default, wrapper = custom_wrapper)
2014-03-14 14:51:59 +00:00
old = SimpleForm.wrappers[name.to_s]
SimpleForm.wrappers[name.to_s] = wrapper
2011-09-04 09:31:24 +00:00
yield
ensure
2014-03-14 14:51:59 +00:00
SimpleForm.wrappers[name.to_s] = old
2011-09-04 09:31:24 +00:00
end
2011-09-03 17:04:40 +00:00
def custom_wrapper
2013-01-28 21:02:59 +00:00
SimpleForm.build tag: :section, class: "custom_wrapper", pattern: false do |b|
b.use :pattern
2013-01-28 21:02:59 +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
2013-01-28 21:02:59 +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
2013-01-28 21:02:59 +00:00
b.use :hint, wrap_with: { class: "omg_hint" }
2011-09-03 17:04:40 +00:00
end
end
2014-06-23 19:14:29 +00:00
def custom_wrapper_with_wrapped_optional_component
SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
b.wrapper tag: :div, class: 'no_output_wrapper' do |ba|
ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' }
end
end
end
2014-06-23 19:14:29 +00:00
def custom_wrapper_with_unless_blank
SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
b.wrapper tag: :div, class: 'no_output_wrapper', unless_blank: true do |ba|
ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' }
end
end
end
def custom_wrapper_with_input_class
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label
b.use :input, class: 'inline-class'
end
end
def custom_wrapper_with_input_data_modal
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label
b.use :input, data: { modal: 'data-modal', wrapper: 'data-wrapper' }
end
end
def custom_wrapper_with_input_aria_modal
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label
b.use :input, aria: { modal: 'aria-modal', wrapper: 'aria-wrapper' }
end
end
2014-03-11 14:22:20 +00:00
def custom_wrapper_with_label_class
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label, class: 'inline-class'
b.use :input
end
end
2014-03-11 14:39:41 +00:00
def custom_wrapper_with_input_attributes
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :input, data: { modal: true }
end
end
2014-03-11 14:31:48 +00:00
def custom_wrapper_with_label_input_class
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label_input, class: 'inline-class'
end
end
def custom_wrapper_with_wrapped_input
2013-01-28 21:02:59 +00:00
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.wrapper tag: :div, class: 'elem' do |component|
component.use :label
2013-01-28 21:02:59 +00:00
component.use :input, wrap_with: { tag: :div, class: 'input' }
end
end
end
def custom_wrapper_with_wrapped_label
2013-01-28 21:02:59 +00:00
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
2013-01-28 21:02:59 +00:00
SimpleForm.build tag: false, class: 'custom_wrapper_without_top_level' do |b|
b.use :label_input
2013-01-28 21:02:59 +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
2013-01-28 21:02:59 +00:00
SimpleForm.build tag: :div, wrapper_html: { id: 'custom_wrapper_without_class' } do |b|
2012-06-29 19:13:15 +00:00
b.use :label_input
end
end
def custom_wrapper_with_label_html_option
2013-01-28 21:02:59 +00:00
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
2013-01-28 21:02:59 +00:00
SimpleForm.build tag: :section, class: "custom_wrapper", pattern: false do |b|
b.use :label_input, wrap_with: { tag: :div, class: :field }
2012-10-05 16:45:20 +00:00
end
end
def custom_wrapper_with_additional_attributes
SimpleForm.build tag: :div, class: 'custom_wrapper', html: { data: { wrapper: :test }, title: 'some title' } do |b|
b.use :label_input
end
end
def custom_wrapper_with_full_error
SimpleForm.build tag: :div, class: 'custom_wrapper' do |b|
b.use :full_error, wrap_with: { tag: :span, class: :error }
end
end
2012-09-11 12:45:26 +00:00
def custom_wrapper_with_label_text
SimpleForm.build :label_text => proc { |label, required| "**#{label}**" } do |b|
2012-09-11 12:45:26 +00:00
b.use :label_input
end
end
def custom_wrapper_with_custom_label_component
SimpleForm.build tag: :span, class: 'custom_wrapper' do |b|
b.use :label_text
end
end
def custom_wrapper_with_html5_components
SimpleForm.build tag: :span, class: 'custom_wrapper' do |b|
b.use :label_text
end
end
def custom_wrapper_with_required_input
SimpleForm.build tag: :span, class: 'custom_wrapper' do |b|
b.use :html5
b.use :input, required: true
end
end
def custom_form_for(object, *args, &block)
2013-04-21 15:08:00 +00:00
simple_form_for(object, *args, { builder: CustomFormBuilder }, &block)
end
def custom_mapping_form_for(object, *args, &block)
2013-04-21 15:08:00 +00:00
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
2014-03-11 22:12:08 +00:00
def with_input_for(object, attribute_name, type, options = {})
with_concat_form_for(object) do |f|
2013-01-28 21:02:59 +00:00
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)
2013-04-21 15:08:00 +00:00
super(attribute_name, *args, { input_html: { class: 'custom' } }, &block)
2011-01-05 19:52:49 +00:00
end
end
class CustomMapTypeFormBuilder < SimpleForm::FormBuilder
2013-01-28 21:02:59 +00:00
map_type :custom_type, to: SimpleForm::Inputs::StringInput
end