Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created December 1, 2021 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopsoft/6dfc79d5c5261c894a5b9b2419127c5c to your computer and use it in GitHub Desktop.
Save hopsoft/6dfc79d5c5261c894a5b9b2419127c5c to your computer and use it in GitHub Desktop.
ApplicationReflex setting ActiveSupport::CurrentAttributes via lifecycle callbacks
# frozen_string_literal: true
class ApplicationReflex < StimulusReflex::Reflex
# Put application-wide Reflex behavior and callbacks in this file.
#
# Example:
#
# # If your ActionCable connection is: `identified_by :current_user`
# delegate :current_user, to: :connection
#
# Learn more at: https://docs.stimulusreflex.com/rtfm/reflex-classes
around_reflex :set_time_zone
before_reflex :load_current
after_reflex :save_current
private
def load_current
Current.user = connection.current_user
Current.team = connection.current_team
Current.user.reload_ui_disposition
end
def save_current
Current.task_filter.active = false if Current.user.inactive?
Current.ui_disposition.save
cable_ready.dispatch_event name: "timesheet:stimulus-reflex:after"
end
def set_time_zone
tz = Current.ui_disposition&.timezone || ActiveSupport::TimeZone["UTC"]
Time.use_zone(tz) { yield }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment