1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
paper-trail-gem--paper_trail/lib/paper_trail/frameworks/sinatra.rb
2016-03-05 17:11:08 -05:00

37 lines
1.2 KiB
Ruby

require "active_support/core_ext/object" # provides the `try` method
module PaperTrail
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
::Sinatra.register PaperTrail::Sinatra if defined?(::Sinatra)
end