10000 refactor · activeadmin/activeadmin@a1cfdb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit a1cfdb6

Browse files
committed
refactor
1 parent f7e94ce commit a1cfdb6

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

lib/active_admin/form_builder.rb

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,31 @@ def initialize(*args)
99
end
1010

1111
def inputs(*args, &block)
12-
# Store that we are creating inputs without a block
13-
@inputs_with_block = block_given? ? true : false
14-
content = with_new_form_buffer { super }
15-
form_buffers.last << content.html_safe
12+
@inputs_with_block = block_given?
13+
form_buffers.last << with_new_form_buffer{ super }
1614
end
1715

18-
# The input method returns a properly formatted string for
19-
# its contents, so we want to skip the internal buffering
20-
# while building up its contents
16+
# If this `input` call is inside a `inputs` block, add the content
17+
# to the form buffer. Else, return it directly.
2118
def input(method, *args)
22-
content = with_new_form_buffer { super }
23-
return content.html_safe unless @inputs_with_block
24-
form_buffers.last << content.html_safe
19+
content = with_new_form_buffer{ super }
20+
@inputs_with_block ? form_buffers.last << content : content
2521
end
2622

27-
def cancel_link(url = nil, html_options = {}, li_attributes = {})
28-
li_attributes[:class] ||= "cancel"
29-
url ||= {:action => "index"}
30-
form_buffers.last << template.content_tag(:li, (template.link_to I18n.t('active_admin.cancel'), url, html_options), li_attributes)
23+
def cancel_link(url = {:action => "index"}, html_options = {}, li_attrs = {})
24+
li_attrs[:class] ||= "cancel"
25+
li_content = template.link_to I18n.t('active_admin.cancel'), url, html_options
26+
form_buffers.last << template.content_tag(:li, li_content, li_attrs)
3127
end
3228

3329
def actions(*args, &block)
34-
content = with_new_form_buffer do
35-
block_given? ? super : super { commit_action_with_cancel_link }
30+
form_buffers.last << with_new_form_buffer do
31+
block_given? ? super : super{ commit_action_with_cancel_link }
3632
end
37-
form_buffers.last << content.html_safe
3833
end
3934

4035
def action(*args)
41-
content = with_new_form_buffer { super }
42-
form_buffers.last << content.html_safe
36+
form_buffers.last << with_new_form_buffer{ super }
4337
end
4438

4539
def commit_action_with_cancel_link
@@ -76,16 +70,16 @@ def has_many(association, options = {}, &block)
7670
form_buffers.last << template.content_tag(:h3, object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1))
7771
inputs options, &form_block
7872

79-
js = js_for_has_many(association, form_block, template)
80-
form_buffers.last << js.html_safe
73+
form_buffers.last << js_for_has_many(association, form_block, template)
8174
end
8275
end
83-
form_buffers.last << content.html_safe
76+
form_buffers.last << content
8477
end
8578

79+
# BTW: the fact that this *could* return nil might currently be causing problems
8680
def semantic_errors(*args)
87-
content = with_new_form_buffer { super }
88-
form_buffers.last << content.html_safe unless content.nil?
81+
content = with_new_form_buffer{ super }
82+
form_buffers.last << content if content
8983
end
9084

9185
# These methods are deprecated and removed from Formtastic, however are
@@ -164,7 +158,7 @@ def input_class_by_trying(as)
164158

165159
def with_new_form_buffer
166160
form_buffers << "".html_safe
167-
return_value = yield
161+
return_value = yield.html_safe
168162
form_buffers.pop
169163
return_value
170164
end
@@ -187,9 +181,7 @@ def js_for_has_many(association, form_block, template)
187181
text = I18n.t 'active_admin.has_many_new', :model => association_human_name
188182
onclick = "$(this).before('#{js}'.replace(/#{placeholder}/g, new Date().getTime())); return false;"
189183

190-
template.link_to text, "#",
191-
:onclick => onclick,
192-
:class => "button"
184+
template.link_to(text, "#", :onclick => onclick, :class => "button").html_safe
193185
end
194186

195187
end

0 commit comments

Comments
 (0)
0