Sha256: c633369632e76c0301c3b14b8d92cd41a1a87572053841520e68f25350cafd1b

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

require "active_support/core_ext/object" # provides the `try` method

module PaperTrail
  # Extensions to `Sinatra`.
  module Sinatra
    # Register this module inside your Sinatra application to gain access to
    # controller-level methods used by PaperTrail.
    def self.registered(app)
      app.use RequestStore::Middleware
      app.helpers self
      app.before { set_paper_trail_whodunnit }
    end

    protected

    # Returns the user who is responsible for any changes that occur.
    # By default this calls `current_user` and returns the result.
    #
    # Override this method in your controller to call a different
    # method, e.g. `current_person`, or anything you like.
    def user_for_paper_trail
      return unless defined?(current_user)
      ActiveSupport::VERSION::MAJOR >= 4 ? current_user.try!(:id) : current_user.try(:id)
    rescue NoMethodError
      current_user
    end

    private

    # Tells PaperTrail who is responsible for any changes that occur.
    def set_paper_trail_whodunnit
      @set_paper_trail_whodunnit_called = true
      ::PaperTrail.whodunnit = user_for_paper_trail if ::PaperTrail.enabled?
    end
  end
end

if defined?(::Sinatra)
  ::Sinatra.register(::PaperTrail::Sinatra)
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paper_trail-5.2.3/lib/paper_trail/frameworks/sinatra.rb
paper_trail-6.0.2 lib/paper_trail/frameworks/sinatra.rb
paper_trail-6.0.1 lib/paper_trail/frameworks/sinatra.rb
paper_trail-6.0.0 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.2.3 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.2.2 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.2.1 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.2.0 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.1.1 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.1.0 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.0.1 lib/paper_trail/frameworks/sinatra.rb
paper_trail-5.0.0 lib/paper_trail/frameworks/sinatra.rb