Created
December 1, 2021 22:51
-
-
Save hopsoft/6dfc79d5c5261c894a5b9b2419127c5c to your computer and use it in GitHub Desktop.
ApplicationReflex setting ActiveSupport::CurrentAttributes via lifecycle callbacks
This file contains 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
# 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