2017-10-24 01:01:08 +00:00
|
|
|
# frozen_string_literal: true
|
2009-12-10 01:57:05 +00:00
|
|
|
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
|
2009-12-10 01:57:05 +00:00
|
|
|
end
|
|
|
|
|
2010-09-14 03:40:03 +00:00
|
|
|
def assert_no_select(selector, value = nil)
|
2013-01-28 21:02:59 +00:00
|
|
|
assert_select(selector, text: value, count: 0)
|
2009-12-10 01:57:05 +00:00
|
|
|
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|
|
2011-12-04 13:57:12 +00:00
|
|
|
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
|
2014-05-28 10:16:51 +00:00
|
|
|
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
|
2014-05-28 10:16:51 +00:00
|
|
|
SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
|
2014-06-23 19:11:35 +00:00
|
|
|
b.wrapper tag: :div, class: 'no_output_wrapper', unless_blank: true do |ba|
|
2014-05-28 10:16:51 +00:00
|
|
|
ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-10 21:39:31 +00:00
|
|
|
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
|
|
|
|
|
2015-08-31 23:14:09 +00:00
|
|
|
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
|
|
|
|
|
2012-06-23 17:59:11 +00:00
|
|
|
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|
|
2012-06-23 17:59:11 +00:00
|
|
|
component.use :label
|
2013-01-28 21:02:59 +00:00
|
|
|
component.use :input, wrap_with: { tag: :div, class: 'input' }
|
2012-06-23 17:59:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 02:22:25 +00:00
|
|
|
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' }
|
2012-06-27 02:22:25 +00:00
|
|
|
component.use :input
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-09 22:02:27 +00:00
|
|
|
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|
|
2011-11-09 22:02:27 +00:00
|
|
|
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 }
|
2011-11-09 22:02:27 +00:00
|
|
|
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
|
|
|
|
|
2012-04-21 12:51:02 +00:00
|
|
|
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|
|
2012-04-21 12:51:02 +00:00
|
|
|
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
|
|
|
|
|
2014-03-11 23:27:10 +00:00
|
|
|
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
|
|
|
|
|
2014-04-03 15:08:29 +00:00
|
|
|
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
|
2018-01-07 17:55:19 +00:00
|
|
|
SimpleForm.build label_text: proc { |label, required| "**#{label}**" } do |b|
|
2012-09-11 12:45:26 +00:00
|
|
|
b.use :label_input
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-19 08:11:27 +00:00
|
|
|
def custom_wrapper_with_custom_label_component
|
|
|
|
SimpleForm.build tag: :span, class: 'custom_wrapper' do |b|
|
|
|
|
b.use :label_text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-19 21:20:17 +00:00
|
|
|
def custom_wrapper_with_html5_components
|
|
|
|
SimpleForm.build tag: :span, class: 'custom_wrapper' do |b|
|
|
|
|
b.use :label_text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-03 17:02:09 +00:00
|
|
|
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
|
|
|
|
|
2018-03-02 21:29:54 +00:00
|
|
|
def custom_wrapper_with_input_error_class
|
|
|
|
SimpleForm.build tag: :div, class: "custom_wrapper", error_class: :field_with_errors do |b|
|
|
|
|
b.use :label
|
|
|
|
b.use :input, class: 'inline-class', error_class: 'is-invalid'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-23 17:41:16 +00:00
|
|
|
def custom_wrapper_with_input_valid_class(valid_class: :field_without_errors)
|
|
|
|
SimpleForm.build tag: :div, class: "custom_wrapper", valid_class: valid_class do |b|
|
2018-03-11 14:27:41 +00:00
|
|
|
b.use :label
|
2018-03-22 21:21:29 +00:00
|
|
|
b.use :input, class: 'inline-class', valid_class: 'is-valid'
|
2018-03-11 14:27:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-03 16:49:54 +00:00
|
|
|
def custom_form_for(object, *args, &block)
|
2013-04-21 15:08:00 +00:00
|
|
|
simple_form_for(object, *args, { builder: CustomFormBuilder }, &block)
|
2011-09-03 16:49:54 +00:00
|
|
|
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)
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2011-11-11 12:28:22 +00:00
|
|
|
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
|
|
|
|
2011-11-11 12:28:22 +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
|
|
|
|
|
2011-11-11 12:28:22 +00:00
|
|
|
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
|
2011-03-29 18:46:08 +00:00
|
|
|
|
2011-09-03 16:49:54 +00:00
|
|
|
def with_form_for(object, *args, &block)
|
|
|
|
with_concat_form_for(object) do |f|
|
|
|
|
f.input(*args, &block)
|
|
|
|
end
|
2011-03-29 18:46:08 +00:00
|
|
|
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 = {})
|
2011-09-03 16:49:54 +00:00
|
|
|
with_concat_form_for(object) do |f|
|
2013-01-28 21:02:59 +00:00
|
|
|
f.input(attribute_name, options.merge(as: type))
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
2011-03-29 18:46:08 +00:00
|
|
|
end
|
2018-05-13 14:05:30 +00:00
|
|
|
|
|
|
|
def with_input_field_for(object, *args)
|
|
|
|
with_concat_form_for(object) do |f|
|
|
|
|
f.input_field(*args)
|
|
|
|
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
|
2009-12-10 01:57:05 +00:00
|
|
|
end
|
2011-03-29 18:46:08 +00:00
|
|
|
|
|
|
|
class CustomMapTypeFormBuilder < SimpleForm::FormBuilder
|
2013-01-28 21:02:59 +00:00
|
|
|
map_type :custom_type, to: SimpleForm::Inputs::StringInput
|
2011-03-29 18:46:08 +00:00
|
|
|
end
|