Code Monkey home page Code Monkey logo

Comments (1)

DaanVanVugt avatar DaanVanVugt commented on July 24, 2024

I have created a solution using ActiveSupport::CurrentAttributes instead, fearing thread-safety issues with the approach mentioned above.

# app/models/current.rb
class Current < ActiveSupport::CurrentAttributes
  attribute :signed_global_id_expires_at
end
# app/controllers/application_controller.rb
  before_action :set_sgid_expiry

  # Set a default expiry for SGIDs which means that any ids generated within this request will have the same value
  # and can thus be matched on the frontend
  def set_sgid_expiry
    Current.signed_global_id_expires_at = 1.week.from_now
  end
# config/initializers/monkey_patches.rb
# Require all Ruby files in the core_extensions directory
Dir[Rails.root.join('lib', 'patches', '*.rb')].each { |f| require f }

SignedGlobalID.prepend Patches::SignedGlobalID
# lib/patches/signed_global_id.rb
module Patches
  module SignedGlobalID
    def pick_expiration(options)
      return options[:expires_at] if options.key?(:expires_at)
      return options[:expires_in].from_now if options.key?(:expires_in)

      Current.signed_global_id_expires_at || self.class.expires_in&.from_now
    end
  end
end

which succesfully generates the same signed ID on two subsequent calls.

from globalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.