Access events through collections

This commit is contained in:
Jorge Manrubia
2025-04-24 08:48:43 +02:00
parent 54f4bd5144
commit 965100df51
7 changed files with 41 additions and 12 deletions
+2 -11
View File
@@ -19,21 +19,12 @@ module EventsTimeline
end
def events_for_activity_day
user_events.where(created_at: @activity_day.all_day)
Current.user.accessible_events.where(created_at: @activity_day.all_day)
.group_by { |event| [ event.created_at.hour, helpers.event_column(event) ] }
.map { |hour_col, events| [ hour_col, events.uniq { |e| e.id } ] }
end
def latest_event_before_activity_day
user_events.where(created_at: ...@activity_day.beginning_of_day).chronologically.last
end
def user_events
Event.where(collection: collection_filter).where(eventable: user_cards)
end
def user_cards
Current.user.accessible_cards
.published_or_drafted_by(Current.user)
Current.user.accessible_events.where(created_at: ...@activity_day.beginning_of_day).chronologically.last
end
end
+1 -1
View File
@@ -1,6 +1,6 @@
class TerminalsController < ApplicationController
def show
@events = Event.where(eventable: Current.user.accessible_cards, creator: Current.user).chronologically.reverse_order.limit(20)
@events = Current.user.accessible_events.chronologically.reverse_order.limit(20)
end
def edit
+1
View File
@@ -5,6 +5,7 @@ class Collection < ApplicationRecord
has_many :cards, dependent: :destroy
has_many :tags, -> { distinct }, through: :cards
has_many :events
scope :alphabetically, -> { order("lower(name)") }
end
+1
View File
@@ -5,6 +5,7 @@ module User::Accessor
has_many :accesses, dependent: :destroy
has_many :collections, through: :accesses
has_many :accessible_cards, through: :collections, source: :cards
has_many :accessible_events, through: :collections, source: :events
after_create_commit :grant_access_to_collections
end
@@ -0,0 +1,14 @@
class MakeEventsEventable < ActiveRecord::Migration[8.1]
def change
add_reference :events, :eventable, polymorphic: true, index: true
execute <<~SQL
update events set eventable_type = 'Card', eventable_id = card_id;
SQL
change_column_null :events, :eventable_id, false
change_column_null :events, :eventable_type, false
remove_reference :events, :card
end
end
@@ -0,0 +1,5 @@
class MakeNotificationsSourceIdNotNullable < ActiveRecord::Migration[8.1]
def change
change_column_null :notifications, :source_id, false
end
end
@@ -0,0 +1,17 @@
class AddCollectionIdToEvents < ActiveRecord::Migration[8.1]
def change
add_reference :events, :collection, foreign_key: true, index: true
execute <<~SQL
UPDATE events
SET collection_id = (
SELECT cards.collection_id
FROM cards
WHERE cards.id = events.eventable_id
)
WHERE eventable_type = 'Card'
SQL
change_column_null :events, :collection_id, false
end
end