WIP: field_with methods used id, search
This commit is contained in:
parent
6413cdee82
commit
2c5238c5f5
@ -42,6 +42,9 @@ describe "Form Fields" do
|
|||||||
it "can be found by field_with method" do
|
it "can be found by field_with method" do
|
||||||
name_field = form.field_with("name")
|
name_field = form.field_with("name")
|
||||||
name_field.name.should eq "name"
|
name_field.name.should eq "name"
|
||||||
|
|
||||||
|
email_field = form.field_with({"id" => "emailID"})
|
||||||
|
email_field.name.should eq "email"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can be found by fields_with method" do
|
it "can be found by fields_with method" do
|
||||||
|
@ -51,15 +51,25 @@ class MechanizeCr::Form
|
|||||||
|
|
||||||
def {{plural.id}}_with(criteria, &block)
|
def {{plural.id}}_with(criteria, &block)
|
||||||
value = Hash(String,String).new
|
value = Hash(String,String).new
|
||||||
if String === criteria
|
if criteria.is_a?(String)
|
||||||
value = {"name" => criteria}
|
criteria = {"name" => criteria}
|
||||||
else
|
else
|
||||||
# TODO
|
# TODO
|
||||||
# when args whose type isn't String is given
|
# when args whose type isn't String is given
|
||||||
|
criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h|
|
||||||
|
case k = k.to_s
|
||||||
|
when "id"
|
||||||
|
h["id"] = v
|
||||||
|
when "class"
|
||||||
|
h["class"] = v
|
||||||
|
else
|
||||||
|
h[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
f = {{plural.id}}.select do |elm|
|
f = {{plural.id}}.select do |elm|
|
||||||
value.all? do |k,v|
|
criteria.all? do |k,v|
|
||||||
v === elm.name
|
v === elm.node.fetch(k,"x")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
yield f
|
yield f
|
||||||
@ -68,7 +78,8 @@ class MechanizeCr::Form
|
|||||||
|
|
||||||
def {{singular.id}}_with(criteria)
|
def {{singular.id}}_with(criteria)
|
||||||
f = {{plural.id}}_with(criteria)
|
f = {{plural.id}}_with(criteria)
|
||||||
raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.empty?
|
# TODO: Write correct error message.
|
||||||
|
raise ElementNotFoundError.new(:{{singular.id}}, "") if f.empty?
|
||||||
f.first
|
f.first
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
Loading…
Reference in New Issue
Block a user