Skip to content

Apply custom behaviour for first/last slot #1092

Answered by BigBigDoudou
BigBigDoudou asked this question in Q&A
Discussion options

You must be logged in to vote

I found the solution and it was surprisingly easy, I just needed an attr_writer:

class BreadcrumbComponent
  renders_many :items, "BreadcrumbItemComponent"

  def call
    tag.ul do
      items.last.active = true
      items.each { |item| concat(item) }
    end
  end

  class BreadcrumbItemComponent
      attr_writer :active

      def initialize(item)
        @item = item
      end

     def call
       html_class = +"breadcrumb"
       html_class << " active" if @active
        tag.li(@item, class: html_class)
      end
  end
end

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@BigBigDoudou
Comment options

Comment options

You must be logged in to vote
1 reply
@Spone
Comment options

Answer selected by BigBigDoudou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants