Module: Helperful::ContentHelper
- Defined in:
- lib/helperful/content_helper.rb
Overview
Content Helper
Provides a set of helpers for capturing and working with your page content in a more effective way.
Requires
The following requirements are mandatory for this module. Including this helper will automatically include them unless already included.
-
ActionView::Helpers::CaptureHelper
Class Method Summary collapse
Instance Method Summary collapse
-
#content_for_with_has(*args, &block) ⇒ Object
Despite its name, this method is a transparent replacement for the original
content_forRails helper. -
#has_content!(name) ⇒ Object
Flags
nameto ensurehas_content?will work as expected. -
#has_content?(name) ⇒ Boolean
Returns
trueifnamehas any content, in other workds if content_for(name) has ever been called.
Class Method Details
.included(base) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/helperful/content_helper.rb', line 35 def self.included(base) base.class_eval do base.included_modules.include?(ActionView::Helpers::CaptureHelper) || include(ActionView::Helpers::CaptureHelper) alias_method_chain :content_for, :has end end |
Instance Method Details
#content_for_with_has(*args, &block) ⇒ Object
Despite its name, this method is a transparent replacement for the original content_for Rails helper.
It forwards any request to the original Rails helper, but before it keep tracks of every request to ensure has_content? will work as expected.
Behind this original name
For those of you interested in, this name arises from the need to chain it with alias_method_chain, following Rails best practice for extending original core methods.
99 100 101 102 |
# File 'lib/helperful/content_helper.rb', line 99 def content_for_with_has(*args, &block) # :nodoc: has_content!(args.first) content_for_without_has(*args, &block) end |
#has_content!(name) ⇒ Object
Flags name to ensure has_content? will work as expected. You would probably never call this method directly. In fact, you are encouraged to not do so.
80 81 82 83 |
# File 'lib/helperful/content_helper.rb', line 80 def has_content!(name) # :nodoc: @has_content ||= {} @has_content[name.to_s] = true end |
#has_content?(name) ⇒ Boolean
Returns true if name has any content, in other workds if content_for(name) has ever been called.
Important
Some important details to keep in mind.
Due to the way content_for stores data, it doesn’t matter whether name is a Symbol or a String. They are treated in the same way. The following calls are equivalent.
has_content? :foo # => true
has_content? "foo" # => true
This method doesn’t make any difference between an empty, blank or nil content. It always returns true if content_for has been called with name, it doesn’t matter which content is stored. A call to has_content? will always return true in any of the following cases:
content_for :name, nil
content_for :name, ''
content_for :name, []
content_for :name, 'A real content'
content_for :name do
"nothing here"
end
72 73 74 75 |
# File 'lib/helperful/content_helper.rb', line 72 def has_content?(name) @has_content ||= {} @has_content.key?(name.to_s) end |