Created
October 19, 2022 22:22
-
-
Save Caleb-T-Owens/3dd7947b54998ffc5690141c9fff1ce5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| = @turbo.frame do | |
| - if @closed | |
| = link_to 'Open', @turbo.send(:open) | |
| - else | |
| .board-list | |
| = link_to 'Close', @turbo.send(:close) | |
| %p Boards: | |
| - @boards.each do |board| | |
| = link_to board.name, board | |
| %hr | |
| = link_to 'Create new Board', new_board_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BoardListComponent < ViewComponent::Base | |
| def initialize(boards: [], closed: false, turbo:) | |
| @boards = boards | |
| @closed = closed | |
| @turbo = turbo | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| = render TurboBoardListComponent.render(current_user: current_user, board_ids: @boards.pluck(:id)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TurboBaseComponent | |
| include Turbo::StreamsHelper | |
| include Turbo::FramesHelper | |
| include ActionView::Helpers::TagHelper | |
| include ActionView::Context | |
| MESSAGES = [] | |
| attr_reader :context, :current_user | |
| def self.render(options = {}) | |
| instance = self.new(**options.slice(:context, :current_user)) | |
| instance.setup **options.except(:context, :current_user) | |
| instance.render | |
| end | |
| def initialize(context: {}, current_user:) | |
| @context = context | |
| @current_user = current_user | |
| end | |
| def frame(&block) | |
| content_tag(:turbo_frame, capture(&block), id: frame_tag) | |
| end | |
| def send(message) | |
| raise "#{message} is not a valid message" unless self.class::MESSAGES.include?(message) | |
| Rails.application.routes.url_helpers.turbo_path({ | |
| message: message, | |
| context: context, | |
| component: self.class.name | |
| }) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TurboBoardListComponent < TurboBaseComponent | |
| MESSAGES = %i[open close] | |
| def frame_tag | |
| # There should only ever be one board list per page so a constant ID is fine | |
| 'board_list' | |
| end | |
| def setup(board_ids:) | |
| context['board_open'] = 'open' | |
| context['board_ids'] = board_ids | |
| end | |
| def update(message) | |
| context['board_open'] = message == :close ? 'closed' : 'open' | |
| end | |
| def render | |
| if context['board_open'] == 'closed' | |
| BoardListComponent.new(turbo: self, closed: true) | |
| else | |
| BoardListComponent.new(turbo: self, boards: current_user.boards.where(id: context['board_ids'])) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment