Skip to content

Commit

Permalink
Extend more implementatios with capextensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Paco Guzman authored and nando committed Mar 5, 2010
1 parent 1fcf98f commit 5701c87
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 50 deletions.
4 changes: 4 additions & 0 deletions features/support/capybara/capextensions.rb
Expand Up @@ -121,6 +121,10 @@ def have_selector(path, options = {})
options[:text] = content unless content.nil?
have_css(path, options)
end

def contain(content)
have_text(content)
end
end

World(Capextensions)
83 changes: 33 additions & 50 deletions lib/mundo_pepino/implementations_api.rb
Expand Up @@ -22,13 +22,13 @@ def names_for_simple_creation(model, number, name_or_names, options = {})
[base_hash] * number
end
end

def field_for(model=nil, field = nil)
model_name = "#{model && model.name+'::'}"
if field
"#{model_name}#{field}".to_field || field.to_field
else
"#{model_name}name".to_field || 'name'.to_field ||
"#{model_name}name".to_field || 'name'.to_field ||
# The use of "nombre" the name field mapping is deprecated
"#{model_name}nombre".to_field || 'nombre'.to_field || :name
end
Expand All @@ -37,16 +37,16 @@ def field_for(model=nil, field = nil)
def shouldify(should_or_not)
should_or_not =~ /^(#{MundoPepino::Matchers::Bites._should_})$/i ? :should : :should_not
end

def not_shouldify(should_or_not)
shouldify(should_or_not) == :should ? :should_not : :should
end

# Cucumber::Model::Table's hashes traduciendo nombres de campo
def translated_hashes(step_table, options = {})
base_hash = base_hash_for(options)
header = step_table[0].map do |raw_field|
field_for(options[:model], raw_field) || raw_field
header = step_table[0].map do |raw_field|
field_for(options[:model], raw_field) || raw_field
end
step_table[1..-1].map do |row|
h = base_hash.dup
Expand All @@ -57,14 +57,14 @@ def translated_hashes(step_table, options = {})
h
end
end
def base_hash_for(options)

def base_hash_for(options)
if options[:parent]
# polymorphic associations
if options[:polymorphic_as]
{ "#{options[:polymorphic_as]}_id" => options[:parent].id,
"#{options[:polymorphic_as]}_type" => options[:parent].class.name }
else
else
field_prefix = options[:parent_field] || options[:parent].class.name.underscore
if options[:through]
{:through => {"model" => eval(options[:through].to_s.classify),
Expand All @@ -77,21 +77,21 @@ def base_hash_for(options)
{}
end
end

def convert_to_model(raw_model)
raw_model.to_unquoted.to_model || raise(ModelNotMapped.new(raw_model))
end

def convert_to_field(raw_field, model = nil)
unless raw_field.nil?
unless raw_field.nil?
if field = field_for(model, raw_field.to_unquoted)
field
else
raise MundoPepino::FieldNotMapped.new(raw_field)
raise MundoPepino::FieldNotMapped.new(raw_field)
end
end
end

def last_mentioned_children(field_raw)
if child_model = field_raw.to_model
parent_model = last_mentioned.mr_model
Expand All @@ -107,7 +107,7 @@ def last_mentioned_children(field_raw)
def last_mentioned_should_have_n_children(field, number)
last_mentioned_children(field).size.should == number.to_number
end

def last_mentioned_should_have_value(raw_field, valor)
res = last_mentioned
if child_model = raw_field.to_model
Expand All @@ -120,12 +120,12 @@ def last_mentioned_should_have_value(raw_field, valor)
raise MundoPepino::FieldNotMapped.new(raw_field)
end
end

def last_mentioned_should_have_child(field_raw, name)
children = last_mentioned_children(field_raw)
child = if children.any?
model = children.first.class
model.send("find_by_#{field_for(model)}", name)
model.send("find_by_#{field_for(model)}", name)
end
children.detect {|c| c.id == child.id}.should_not be_nil
end
Expand All @@ -142,15 +142,15 @@ def find_field_and_do(action, raw_field, options = nil)

def find_field_and_do_with_capybara(action, raw_field, options = nil)
do_with_capybara action, raw_field.to_unquoted.to_translated, options # a pelo (localización vía labels)
rescue Capybara::ElementNotFound
rescue Capybara::ElementNotFound
field = convert_to_field(raw_field, last_mentioned_model)
begin
do_with_capybara action, field.to_s, options # campo traducido tal cual...
rescue Capybara::ElementNotFound
#TODO cucumber -p capybara_es_ES features/es_ES/tenemos-en-bbdd-registros.feature:50 # Scenario: dos campos has_many del mismo modelo
# se busca un campo "name" tenemos "for=session_name" y el texto del label es 'Name' capybara es casesensitive
raise $! if field.to_s.capitalize == field.to_s
do_with_capybara action, field.to_s.capitalize, options
do_with_capybara action, field.to_s.capitalize, options
end
end

Expand All @@ -165,12 +165,12 @@ def do_with_capybara(action, field, options)
self.send action, field
end
end

def find_field_and_do_with_webrat(action, raw_field, options = nil)
do_with_webrat action, raw_field.to_unquoted.to_translated, options # a pelo (localización vía labels)
rescue Webrat::NotFoundError
field = convert_to_field(raw_field, last_mentioned_model)
begin
begin
do_with_webrat action, field, options # campo traducido tal cual...
rescue Webrat::NotFoundError
if singular = last_mentioned_singular # traducido y con el modelo por delante
Expand All @@ -180,7 +180,7 @@ def find_field_and_do_with_webrat(action, raw_field, options = nil)
end
end
end

def do_with_webrat(action, field, options)
if options
if options[:path] and options[:content_type]
Expand All @@ -192,10 +192,10 @@ def do_with_webrat(action, field, options)
self.send action, field
end
end

def parent_options(parent, child_model, child_field)
options = {:parent => parent}
if child_field and
if child_field and
parent_field = "#{child_model.name}::#{child_field}".to_field
options[:parent_field] = parent_field
elsif reflections = parent.class.reflect_on_association(child_model.table_name.to_sym)
Expand Down Expand Up @@ -246,13 +246,8 @@ def nested_field_id(parent_resource, nested_model, nested_field, nested_name)

def nested_field_id_prefix(parent_resource, resource)
preprefix = nested_field_prefix_prefix(parent_resource.mr_model, resource.class)
if defined?(Webrat)
html = response.body
else
html = body
end

Nokogiri::HTML.parse(html).xpath(
# We can use response with capybara due to capextensions
Nokogiri::HTML.parse(response.body).xpath(
"//input[@type='hidden' and @value=#{resource.id}]"
).each do |input|
return "#{preprefix}_#{$1}_" if input.attributes['id'].to_s =~ /#{preprefix}_([0-9]+)_id/
Expand All @@ -262,14 +257,9 @@ def nested_field_id_prefix(parent_resource, resource)
def new_nested_field_id(parent_resource, model, field)
preprefix = nested_field_prefix_prefix(parent_resource.mr_model, model)
(0..MAX_NESTED_RESOURCES).each do |index|
if defined?(Webrat)
if Nokogiri::HTML.parse(response.body).css("##{preprefix}_#{index}_id").empty?
return "#{preprefix}_#{index}_#{field}"
end
else
if Nokogiri::HTML.parse(body).css("##{preprefix}_#{index}_id").empty?
return "#{preprefix}_#{index}_#{field}"
end
# We can use response with capybara due to capextensions
if Nokogiri::HTML.parse(response.body).css("##{preprefix}_#{index}_id").empty?
return "#{preprefix}_#{index}_#{field}"
end
end
# TODO: raise too many nested resources
Expand All @@ -280,18 +270,11 @@ def nested_field_prefix_prefix(parent_model, child_model)
end

def should_or_not_contain_text(params)
if defined?(Webrat)
response.send(
shouldify(params[:should]),
contain(params[:text].to_unquoted.to_translated.to_regexp))
else
#TODO capybara README recommends have_content
page.send(
shouldify(params[:should]),
have_text(params[:text].to_unquoted.to_translated.to_regexp)
)
end
# We can use response and contain with capybara due to capextensions
response.send(
shouldify(params[:should]),
contain(params[:text].to_unquoted.to_translated.to_regexp))
end
end
end
end

0 comments on commit 5701c87

Please sign in to comment.