Skip to content

Commit

Permalink
Add real-time updates for board views
Browse files Browse the repository at this point in the history
  • Loading branch information
piowit committed Jul 18, 2024
1 parent 0ab2840 commit a0aacac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/models/board_column.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
class BoardColumn < ApplicationRecord
include ActionView::RecordIdentifier

belongs_to :board
has_many :cards, dependent: :nullify

validates :name, presence: true

broadcasts_to ->(board_column) { "board_#{board_column.board_id}" },
target: ->(board_column) { "columns_board_#{board_column.board.id}" },
inserts_by: :append
end
13 changes: 13 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@ class Card < ApplicationRecord
acts_as_list scope: :board_column

delegate :board, to: :board_column

after_commit :touch_affected_board_columns

private

def touch_affected_board_columns
if previous_changes[:board_column_id].present?
board.board_columns.find_by(id: previous_changes[:board_column_id]&.first)&.touch
board.board_columns.find_by(id: previous_changes[:board_column_id]&.last)&.touch
else
board_column.touch
end
end
end
2 changes: 2 additions & 0 deletions app/views/boards/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= turbo_stream_from dom_id(@board) %>
<div class="board-header w-100 px-3">
<div class="row">
<div class="d-flex justify-content-between">
Expand All @@ -17,6 +18,7 @@
<% @board_columns.each do |board_column| %>
<%= render partial: 'board_columns/board_column', locals: { board_column: board_column } %>
<% end %>
<%= turbo_frame_tag dom_id(@board, 'columns') %>
<%= turbo_frame_tag dom_id(BoardColumn.new) %>
</div>
</div>

0 comments on commit a0aacac

Please sign in to comment.