Sha256: 6e995b50fa7a53ce6a104f3f90669af8211a579ce9cde96f3c10da83c1ea0c22

Contents?: true

Size: 1.26 KB

Versions: 59

Compression:

Stored size: 1.26 KB

Contents

# ActsAsTokened
#
# Implements rails 5 has_secure_token
# Extends the find() method to work with tokens instead of ids. Prevents enumeration of this resource.

module ActsAsTokened
  extend ActiveSupport::Concern

  module ActiveRecord
    def acts_as_tokened(options = nil)
      include ::ActsAsTokened
    end
  end

  included do
    has_secure_token  # Will always be 24-digits long

    extend FinderMethods
  end

  module ClassMethods
    def relation
      super.tap { |relation| relation.extend(FinderMethods) }
    end
  end

  module FinderMethods
    def find(*args)
      return super unless args.length == 1
      return super if block_given?
      return find_by_id(args.first) if @_effective_reloading

      find_by_token(args.first) || raise(::ActiveRecord::RecordNotFound.new("Couldn't find #{name} with 'token'=#{args.first}"))
    end
  end

  # Instance Methods
  def to_param
    token
  end

  def to_global_id(**params)
    GlobalID.new(URI::GID.build(app: Rails.application.config.global_id.app, model_name: model_name, model_id: to_param, params: params))
  end

  def reload(options = nil)
    self.class.instance_variable_set(:@_effective_reloading, true)
    retval = super
    self.class.instance_variable_set(:@_effective_reloading, nil)
    retval
  end

end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
effective_resources-1.3.6 app/models/concerns/acts_as_tokened.rb
effective_resources-1.3.5 app/models/concerns/acts_as_tokened.rb
effective_resources-1.3.4 app/models/concerns/acts_as_tokened.rb
effective_resources-1.3.3 app/models/concerns/acts_as_tokened.rb
effective_resources-1.3.2 app/models/concerns/acts_as_tokened.rb
effective_resources-1.3.1 app/models/concerns/acts_as_tokened.rb
effective_resources-1.3.0 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.16 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.15 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.14 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.12 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.11 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.10 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.9 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.8 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.6 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.5 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.4 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.3 app/models/concerns/acts_as_tokened.rb
effective_resources-1.2.2 app/models/concerns/acts_as_tokened.rb