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_or_options) ⇒ 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, &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_feature(favorites, FavoriteMod, &block)
end
<%- user.each_favorite do |favorite| %>
<%= favorite.special_feature %>
<%- end -%>
17 18 19 20 21 22 23 |
# File 'lib/characterize/feature_controls.rb', line 17 def each_with_features(collection, *mods, &block) collection.lazy.each do |obj| obj.cast_as(*mods) block.call(obj) obj.uncast(mods.size) end end |
#with(method_name, *tag_name_or_options) ⇒ 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
Examples:
<%= user.with(:favorites, :p, class: 'favorites-details') %>
<%- user.with(:favorites) do %>
<p class="favorites-details">
My Favorite Things: <%= user.favorites.join(', ') %>
</p>
<%- end -%>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/characterize/feature_controls.rb', line 40 def with(method_name, *) value = self.public_send(method_name) if with_conditions(method_name, value) if block_given? yield else tag_name, = * view.content_tag(tag_name, value, ) end else without_option = .last.fetch(:without){ '' } if without_option.respond_to?(:call) without(method_name, &without_option) else view.concat(without_option) end end end |
#with_conditions(method_name, computed_value) ⇒ Object
Used to override behavior of with for the case of special attributes
79 80 81 |
# File 'lib/characterize/feature_controls.rb', line 79 def with_conditions(method_name, computed_value) !computed_value.nil? && computed_value != '' && computed_value != false end |
#without(method_name, &block) ⇒ Object
Conditionally render content for the object when the attribute is NOT present.
Pass in a method name and a block to render.
Examples:
<%- user.without(:favorites) do %>
<p class="favorites-details none">
There are no favorites here.
</p>
<%- end -%>
71 72 73 74 75 76 |
# File 'lib/characterize/feature_controls.rb', line 71 def without(method_name, &block) value = self.public_send(method_name) if !with_conditions(method_name, value) yield end end |