8000 update form builder to only use return val if it's a string · activeadmin/activeadmin@0cad913 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cad913

Browse files
committed
update form builder to only use return val if it's a string
This closes #2192 by updating the fix for #887 to simply drop any return value that isn't a string.
1 parent c2154c1 commit 0cad913

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

lib/active_admin/form_builder.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@ def input_class_by_trying(as)
161161
end
162162

163163
# This method calls the block it's passed (in our case, the `f.inputs` block)
164-
# and wraps the resulting HTML in a fieldset. If your block happens to return
165-
# nil (but it otherwise built the form correctly), the below override passes
164+
# and wraps the resulting HTML in a fieldset. If your block doesn't have a
165+
# valid return value but it was otherwise built correctly, we instead use
166166
# the most recent part of the Active Admin form buffer.
167167
def field_set_and_list_wrapping(*args, &block)
168-
block_given? ? super{ yield || form_buffers.last } : super
168+
block_given? ? super{
169+
(val = yield).is_a?(String) ? val : form_buffers.last
170+
} : super
169171
end
170172

171173
private

spec/unit/form_builder_spec.rb

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,23 @@ def build_form(options = {}, form_object = Post.new, &block)
437437
end
438438
end
439439

440-
{
441-
"input :title, :as => :string" => /id="post_title"/,
442-
"input :title, :as => :text" => /id="post_title"/,
443-
"input :created_at, :as => :time_select" => /id="post_created_at_2i"/,
444-
"input :created_at, :as => :datetime_select" => /id="post_created_at_2i"/,
445-
"input :created_at, :as => :date_select" => /id="post_created_at_2i"/,
440+
{ # Testing that the same input can be used multiple times
441+
"f.input :title, :as => :string" => /id="post_title"/,
442+
"f.input :title, :as => :text" => /id="post_title"/,
443+
"f.input :created_at, :as => :time_select" => /id="post_created_at_2i"/,
444+
"f.input :created_at, :as => :datetime_select" => /id="post_created_at_2i"/,
445+
"f.input :created_at, :as => :date_select" => /id="post_created_at_2i"/,
446+
# Testing that return values don't screw up the form
447+
"f.input :title; nil" => /id="post_title"/,
448+
"f.input :title; []" => /id="post_title"/,
449+
"[:title].each{ |r| f.input r }" => /id="post_title"/,
450+
"[:title].map { |r| f.input r }" => /id="post_title"/,
446451
}.each do |source, regex|
447-
it "should properly buffer #{source}" do
452+
it "should properly buffer `#{source}`" do
448453
body = build_form do |f|
449454
f.inputs do
450-
f.instance_eval(source)
451-
f.instance_eval(source)
455+
eval source
456+
eval source
452457
end
453458
end
454459
body.scan(regex).size.should == 2
@@ -470,19 +475,4 @@ def build_form(options = {}, form_object = Post.new, &block)
470475
end
471476
end
472477

473-
describe "inputs block with nil return value" do
474-
let :body do
475-
build_form do |f|
476-
f.inputs do
477-
f.input :title
478-
nil
479-
end
480-
end
481-
end
482-
483-
it " 5087 ;should generate a single input field" do
484-
body.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" })
485-
end
486-
end
487-
488478
end

0 commit comments

Comments
 (0)
0