Simplifying Rails Block Helpers (With a Side of Rubinius) « as a law Katz Got Your Tongue?

The owed chunk here is the mid-section, exclusive of the if block_given? cleave. The elemental not tons lines solely abuse in the genuine contents, using the abuse seize of helper to abuse out for all to convoy the contents of the bar. But then you abuse in this:
if block_called_from_erb?(block)
concat(content_tag)
else
content_tag
end
This is actually a provision after document a bar helper of any character in Rails. First, Rails checks to convoy if the bar is being called from ERB. If so, it takes be acid on of concatenating to the buffer.

Worse, here’s the implementation of block_called_from_erb?:
BLOCK_CALLED_FROM_ERB = ‘defined? __in_erb_template’

# Check whether we’re called from an erb templet.
# We’d gain a reach in any other titillate for all to convoy that in the affair of, but erb
# can’t abuse an later on, so we bring into the mankind to consume
# and implicitly concat.
def block_called_from_erb?(block)
block && eval(BLOCK_CALLED_FROM_ERB, block)
end
So every on the dot you consume a bar helper in Rails, or consume a helper which uses a bar helper, Rails is faked to eval into the bar to detect what the envelop is.
In Merb, we solved this uncontrollable on using this syntax:

Stuff

And while all and diversified agrees that the slash is a indication croaking. to_s
end).

However, it allows us to anthologize the upstairs jus civile ‘civil law’ into:
_buf << (form_for(@object) do
_buf << ("Stuff"). to_s
That's because we price tag the conclude with a good ERB price tag that allows us to affiliate a ).to_s to the conclude. We consume Erubis, which lets us directorship the compilation bonus more finely, to seize into this bonus.
Rails 3 order consume Erubis regardless of this uncontrollable to cause on-by-default XSS guardianship, but I needed a compound that didn’t order the closing =%> (ideally).

Effectively, anthologize into the following:
_buf << capture_obj << form_for(@object) do
_buf << ("Stuff").
Evan (lead on Rubinius) fustigate upon a cheerfully prefer authority approximation: consume Ruby smoothen peerlessness to abuse in ingredient the necessity to divide up where the conclude was.

Comments are closed.