Module: TranslationHelpers
- Defined in:
- lib/decidim/dev/test/rspec_support/translation_helpers.rb
Overview
A collection of methods to help dealing with translated attributes.
Instance Method Summary collapse
-
#clear_editor(locator) ⇒ Object
Handles how to clear a WYSIWYG editor.
-
#clear_i18n_editor(field, tab_selector, locales) ⇒ Object
Handles how to clear i18n form fields which uses a WYSIWYG editor.
-
#fill_in_editor(locator, params = {}) ⇒ Object
Handles how to fill a WYSIWYG editor.
-
#fill_in_i18n(field, tab_selector, localized_values) ⇒ Object
Handles how to fill in i18n form fields.
-
#fill_in_i18n_editor(field, tab_selector, localized_values) ⇒ Object
Handles how to fill in i18n form fields which uses a WYSIWYG editor.
-
#have_i18n_content(field, upcase: false, strip_tags: false) ⇒ Object
Checks that the current page has some translated content.
-
#have_no_i18n_content(field, upcase: false) ⇒ Object
Checks that the current page does not have some translated content.
-
#t(key, scope: nil) ⇒ Object
Allows using the ‘t` shortcut inside specs just like in views.
-
#translated(field, locale: I18n.locale) ⇒ Object
Gives the localized version of the attribute for the given locale.
Instance Method Details
#clear_editor(locator) ⇒ Object
Handles how to clear a WYSIWYG editor.
locator - The input field ID. The DOM element is selected using jQuery.
101 102 103 104 105 106 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 101 def clear_editor(locator) page.execute_script <<-SCRIPT document.querySelector('##{locator} .editor-container .ProseMirror').innerHTML = '<p><br class="ProseMirror-trailingBreak"></p>'; document.querySelector('##{locator} input').value = ""; SCRIPT end |
#clear_i18n_editor(field, tab_selector, locales) ⇒ Object
Handles how to clear i18n form fields which uses a WYSIWYG editor.
field - the name of the field that should be cleared, without the
locale-related part (e.g. `:participatory_process_title`)
tab_selector - a String representing the ID of the HTML element that holds
the tabs for this input. It usually is `"#<attribute_name>-tabs" (e.g.
"#title-tabs")
locales - an Array with the locales IDs.
78 79 80 81 82 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 78 def clear_i18n_editor(field, tab_selector, locales) clear_i18n_fields(field, tab_selector, locales) do |locator| clear_editor locator end end |
#fill_in_editor(locator, params = {}) ⇒ Object
Handles how to fill a WYSIWYG editor.
locator - The input field ID. The DOM element is selected using jQuery. params - A Hash of options
:with - A String value that will be entered in the form field. (required)
89 90 91 92 93 94 95 96 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 89 def fill_in_editor(locator, params = {}) raise ArgumentError if params[:with].blank? page.execute_script <<-SCRIPT document.querySelector('##{locator} .editor-container .ProseMirror').innerHTML = `#{params[:with]}`; document.querySelector('##{locator} input').value = `#{params[:with]}`; SCRIPT end |
#fill_in_i18n(field, tab_selector, localized_values) ⇒ Object
Handles how to fill in i18n form fields.
field - the name of the field that should be filled, without the
locale-related part (e.g. `:participatory_process_title`)
tab_selector - a String representing the ID of the HTML element that holds
the tabs for this input. It usually is `"#<attribute_name>-tabs" (e.g.
"#title-tabs")
localized_values - a Hash where the keys are the locales IDs and the values
are the values that will be entered in the form field.
49 50 51 52 53 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 49 def fill_in_i18n(field, tab_selector, localized_values) fill_in_i18n_fields(field, tab_selector, localized_values) do |locator, value| fill_in locator, with: value end end |
#fill_in_i18n_editor(field, tab_selector, localized_values) ⇒ Object
Handles how to fill in i18n form fields which uses a WYSIWYG editor.
field - the name of the field that should be filled, without the
locale-related part (e.g. `:participatory_process_title`)
tab_selector - a String representing the ID of the HTML element that holds
the tabs for this input. It usually is `"#<attribute_name>-tabs" (e.g.
"#title-tabs")
localized_values - a Hash where the keys are the locales IDs and the values
are the values that will be entered in the form field.
64 65 66 67 68 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 64 def fill_in_i18n_editor(field, tab_selector, localized_values) fill_in_i18n_fields(field, tab_selector, localized_values) do |locator, value| fill_in_editor locator, with: value end end |
#have_i18n_content(field, upcase: false, strip_tags: false) ⇒ Object
Checks that the current page has some translated content. It strips the HTML tags from the field (in case there are any).
field - the field that holds the translations upcase - a boolean to indicate whether the string must be checked upcased or not.
27 28 29 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 27 def have_i18n_content(field, upcase: false, strip_tags: false) have_content(i18n_content(field, upcase:, strip_tags:).strip) end |
#have_no_i18n_content(field, upcase: false) ⇒ Object
Checks that the current page does not have some translated content. It strips the HTML tags from the field (in case there are any).
field - the field that holds the translations upcase - a boolean to indicate whether the string must be checked upcased or not.
36 37 38 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 36 def have_no_i18n_content(field, upcase: false) have_no_content(i18n_content(field, upcase:)) end |
#t(key, scope: nil) ⇒ Object
Allows using the ‘t` shortcut inside specs just like in views
6 7 8 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 6 def t(key, scope: nil) I18n.t(key, scope:, raise: true) end |
#translated(field, locale: I18n.locale) ⇒ Object
Gives the localized version of the attribute for the given locale. The locale defaults to the application’s default one.
It is intended to be used to avoid the implementation details, so that the translated attributes implementation can change more easily.
15 16 17 18 19 20 |
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 15 def translated(field, locale: I18n.locale) return field if field.is_a?(String) return if field.nil? field[locale.to_s] || field.dig("machine_translations", locale.to_s) end |