Skip to content

Conversation

marcosvafilho
Copy link
Contributor

Feature: Conditionally render components with a new render? method.

As discussed on the Discord channel, we would like to give developers the optionality to conditionally render components without littering views with conditional statements like if and unless.

This can now get easily done by overriding the render? method inside the component.

Instead of:

# some view

<% if current_user.admin? %>
  <%= Components::AdminComponent.(user: current_user) %>
<% end %>

It's possible to:

# some view

<%= Components::AdminComponent.(user: current_user) %>
# app/matestack/components/admin_component.rb

class AdminComponent < Matestack::Ui::Component
  required :user

  def render?
    context.user.admin?
  end

  def response
    div id: "admin-component" do
      plain "This component should only get rendered for admins"
    end
  end
end

Changes

  • Add the render? method to Matestack::Ui::Core::Base
  • Add a guard clause to escape the initialize method on Matestack::Ui::Core::Base if render? returns false
  • Add spec tests
  • Update docs with reference to this new feature

Copy link
Member

@jonasjabari jonasjabari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea and implementation! Thanks @marcosvafilho

@jonasjabari jonasjabari merged commit fc3d532 into matestack:next_release Jun 10, 2021
@jonasjabari jonasjabari mentioned this pull request Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants