Extract spec methods to commons module to avoid code duplication

Because "translatable" and "new_translatable" shared examples needs same methods to manipulate translations.
This commit is contained in:
Senén Rodero Rodríguez
2019-04-25 15:50:05 +02:00
committed by voodoorai2000
parent d0bf0f8857
commit 64bfab9c9c
3 changed files with 64 additions and 60 deletions

View File

@@ -338,62 +338,6 @@ def updated_path_for(resource)
send(path_name, *resource_hierarchy_for(resource.reload))
end
def text_for(field, locale)
I18n.with_locale(locale) do
"#{translatable_class.human_attribute_name(field)} #{language_texts[locale]}"
end
end
def field_for(field, locale, visible: true)
if translatable_class.name == "I18nContent"
"contents_content_#{translatable.key}values_#{field}_#{locale}"
else
within(".translatable-fields[data-locale='#{locale}']") do
find("input[id$='_#{field}'], textarea[id$='_#{field}']", visible: visible)[:id]
end
end
end
def fill_in_field(field, locale, with:)
if input_fields.include?(field)
fill_in field_for(field, locale), with: with
else
fill_in_textarea(field, textarea_fields[field], locale, with: with)
end
end
def fill_in_textarea(field, textarea_type, locale, with:)
if textarea_type == :markdownit
click_link class: "fullscreen-toggle"
fill_in field_for(field, locale), with: with
click_link class: "fullscreen-toggle"
elsif textarea_type == :ckeditor
fill_in_ckeditor field_for(field, locale, visible: false), with: with
end
end
def expect_page_to_have_translatable_field(field, locale, with:)
if input_fields.include?(field)
if translatable_class.name == "I18nContent" && with.blank?
expect(page).to have_field field_for(field, locale)
else
expect(page).to have_field field_for(field, locale), with: with
end
else
textarea_type = textarea_fields[field]
if textarea_type == :markdownit
click_link class: "fullscreen-toggle"
expect(page).to have_field field_for(field, locale), with: with
click_link class: "fullscreen-toggle"
elsif textarea_type == :ckeditor
within("div.js-globalize-attribute[data-locale='#{locale}'] .ckeditor [id$='#{field}']") do
within_frame(textarea_fields.keys.index(field)) { expect(page).to have_content with }
end
end
end
end
# FIXME: button texts should be consistent. Right now, buttons don't
# even share the same colour.
def update_button_text
@@ -422,7 +366,3 @@ def update_button_text
"Save changes"
end
end
def front_end_path_to_visit?(path)
path[/admin|managment|valuation/].blank?
end

View File

@@ -9,6 +9,7 @@ module CommonActions
include Polls
include Proposals
include Tags
include Translations
include Users
include Verifications
include Votes

View File

@@ -0,0 +1,63 @@
module Translations
def text_for(field, locale)
I18n.with_locale(locale) do
"#{translatable_class.human_attribute_name(field)} #{language_texts[locale]}"
end
end
def field_for(field, locale, visible: true)
if translatable_class.name == "I18nContent"
"contents_content_#{translatable.key}values_#{field}_#{locale}"
else
within(".translatable-fields[data-locale='#{locale}']") do
find("input[id$='_#{field}'], textarea[id$='_#{field}']", visible: visible)[:id]
end
end
end
def fill_in_field(field, locale, with:)
if input_fields.include?(field)
fill_in field_for(field, locale), with: with
else
fill_in_textarea(field, textarea_fields[field], locale, with: with)
end
end
def fill_in_textarea(field, textarea_type, locale, with:)
if textarea_type == :markdownit
click_link class: "fullscreen-toggle"
fill_in field_for(field, locale), with: with
click_link class: "fullscreen-toggle"
elsif textarea_type == :ckeditor
fill_in_ckeditor field_for(field, locale, visible: false), with: with
end
end
def expect_page_to_have_translatable_field(field, locale, with:)
if input_fields.include?(field)
if translatable_class.name == "I18nContent" && with.blank?
expect(page).to have_field field_for(field, locale)
else
expect(page).to have_field field_for(field, locale), with: with
end
else
textarea_type = textarea_fields[field]
if textarea_type == :markdownit
click_link class: "fullscreen-toggle"
expect(page).to have_field field_for(field, locale), with: with
click_link class: "fullscreen-toggle"
elsif textarea_type == :ckeditor
within("div.js-globalize-attribute[data-locale='#{locale}'] .ckeditor [id$='#{field}']") do
# Wait longer for iframe initialization
expect(page).to have_selector "iframe.cke_wysiwyg_frame", wait: 5
within_frame(textarea_fields.keys.index(field)) { expect(page).to have_content with }
end
end
end
end
def front_end_path_to_visit?(path)
path[/admin|managment|valuation/].blank?
end
end