Module: Characterize::FeatureControls
- Defined in:
- lib/characterize/feature_controls.rb
Instance Method Summary collapse
-
#each_with_features(collection, *mods, &block) ⇒ Object
Enumerate a collection with the given modules and block casting each object with the modules and uncasting the object afterward.
-
#with(method_name, tag_name = nil, **options, &block) ⇒ Object
Conditionally render content for the object.
-
#with_conditions(_method_name, computed_value) ⇒ Object
Used to override behavior of with for the case of special attributes.
-
#without(method_name, tag_name = nil, value: nil, **options, &block) ⇒ Object
Conditionally render content for the object when the attribute is NOT present.
Instance Method Details
#each_with_features(collection, *mods, &block) ⇒ Object
Enumerate a collection with the given modules and block casting each object with the modules and uncasting the object afterward.
Examples:
def each_favorite(&block)
each_with_features(favorites, FavoriteMod, &block)
end
<%- user.each_favorite do |favorite| %>
<%= favorite.special_feature %>
<%- end -%>
16 17 18 |
# File 'lib/characterize/feature_controls.rb', line 16 def each_with_features(collection, *mods, &block) Casting::Enum.enum(collection, *mods).each(&block) end |
#with(method_name, tag_name = nil, **options, &block) ⇒ Object
Conditionally render content for the object.
Pass in a method name for content and either of:
1. options for Rails' content_tag
2. a block to render
The value of the method call will be yielded to the block
Examples:
<%= user.with(:favorites, :p, class: 'favorites-details') %>
<%- user.with(:favorites) do |favorites| %>
<p class="favorites-details">
My Favorite Things: <%= favorites.join(', ') %>
</p>
<%- end -%>
<%= user.with(:favorites, :p, class: "whatever", without: "Oops! No favorites!")
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/characterize/feature_controls.rb', line 38 def with(method_name, tag_name = nil, **, &block) without_option = .delete(:without) method_value = public_send(method_name) display_value = method_value.then do |method_value| if with_conditions(method_name, method_value) method_value else without_option end end capture_content = block || ->(_method_value = nil) do __view__.concat(__view__.content_tag(tag_name, display_value, )) end if block __view__.capture { capture_content.call(method_value) } else __view__.capture { capture_content.call } end end |
#with_conditions(_method_name, computed_value) ⇒ Object
Used to override behavior of with for the case of special attributes
101 102 103 |
# File 'lib/characterize/feature_controls.rb', line 101 def with_conditions(_method_name, computed_value) !computed_value.nil? && computed_value != "" && computed_value != false end |
#without(method_name, tag_name = nil, value: nil, **options, &block) ⇒ Object
Conditionally render content for the object when the attribute is NOT present.
Pass in a method name and a block to render.
The value of the method call will be yielded to the block
Examples:
<%- user.without(:favorites) do |favorites| %>
<p class="favorites-details none">
There are no favorites here. You had <%= favorites %>
</p>
<%- end -%>
<% user.without(:favorites, :p, value: 'No favorites!')
<% user.without(:favorites, :p, value: "You should have favorites", with: "You DO have favorites!")
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/characterize/feature_controls.rb', line 77 def without(method_name, tag_name = nil, value: nil, **, &block) with_option = .delete(:with) method_value = public_send(method_name) display_value = method_value.then do |method_value| if with_conditions(method_name, method_value) with_option else value end end capture_content = (block || ->(_method_value = nil) do __view__.concat(__view__.content_tag(tag_name, display_value, )) end) if block __view__.capture { capture_content.call(method_value) } else __view__.capture { capture_content.call } end end |