komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components

Home Page:http://komponent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

block_given? from component module

Spone opened this issue · comments

When calling block_given? from a method in a component module, it returns false, even if the component is called with a block.

If I call block_given? from the partial, it works as expected.

Is there a way to improve this?

Module:

module TestComponent
  extend ComponentHelper

  def modifiers
    @modifiers ||= ""
    @modifiers += " has-block" if block_given? # this doesn't work
    @modifiers
  end

Partial:

.test(class=modifiers)
  - if block_given?
    .test-content= yield

Rendering the component with a block:

= c "test"
  p This is a test

You're really not calling your helper with a block, so it's normal block_given? returns false.

We could add a new method that returns what you want though. We would just have to

  • detect the block in the component helper (like here),
  • save that in the context instance variables
  • and add a method in ComponentHelper (for example block_given_to_component?).

Yes, it's the normal behavior. Nonetheless, it would be a good idea to be able to check if the component has been called with a block.

block_given_to_component? sounds like a good name.