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

Instance Method Details

#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)

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 63

def fill_in_editor(locator, params = {})
  raise ArgumentError if params[:with].blank?
  page.execute_script "    $('#\#{locator}').siblings('.editor-container').find('.ql-editor')[0].innerHTML = \"\#{params[:with]}\";\n    $('#\#{locator}').val(\"\#{params[:with]}\")\n  SCRIPT\nend\n"

#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 ususally 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.


37
38
39
40
41
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 37

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_slector - a String representing the ID of the HTML element that holds

the tabs for this input. It ususally 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.


52
53
54
55
56
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 52

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, locale: I18n.locale, upcase: 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 locale - the ID of the locale to check upcase - a boolean to indicate whether the string must be checked upcased or not.

rubocop:disable Style/PredicateName



22
23
24
25
26
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 22

def have_i18n_content(field, locale: I18n.locale, upcase: false)
  content = stripped(translated(field, locale: locale))
  content = content.upcase if upcase
  have_content(content)
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.



10
11
12
# File 'lib/decidim/dev/test/rspec_support/translation_helpers.rb', line 10

def translated(field, locale: I18n.locale)
  field.try(:[], locale.to_s)
end