-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Sinatra; decouple Rails. close #119
- Loading branch information
Ben Atkins
committed
Aug 13, 2013
1 parent
703cf23
commit 557b14d
Showing
9 changed files
with
237 additions
and
85 deletions.
There are no files selected for viewing
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module PaperTrail | ||
module Rails | ||
module Controller | ||
|
||
def self.included(base) | ||
if defined?(ActionController) && base == ActionController::Base | ||
base.before_filter :set_paper_trail_enabled_for_controller | ||
base.before_filter :set_paper_trail_whodunnit, :set_paper_trail_controller_info | ||
end | ||
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 | ||
current_user if defined?(current_user) | ||
end | ||
|
||
# Returns any information about the controller or request that you | ||
# want PaperTrail to store alongside any changes that occur. By | ||
# default this returns an empty hash. | ||
# | ||
# Override this method in your controller to return a hash of any | ||
# information you need. The hash's keys must correspond to columns | ||
# in your `versions` table, so don't forget to add any new columns | ||
# you need. | ||
# | ||
# For example: | ||
# | ||
# {:ip => request.remote_ip, :user_agent => request.user_agent} | ||
# | ||
# The columns `ip` and `user_agent` must exist in your `versions` # table. | ||
# | ||
# Use the `:meta` option to `PaperTrail::Model::ClassMethods.has_paper_trail` | ||
# to store any extra model-level data you need. | ||
def info_for_paper_trail | ||
{} | ||
end | ||
|
||
# Returns `true` (default) or `false` depending on whether PaperTrail should | ||
# be active for the current request. | ||
# | ||
# Override this method in your controller to specify when PaperTrail should | ||
# be off. | ||
def paper_trail_enabled_for_controller | ||
true | ||
end | ||
|
||
private | ||
|
||
# Tells PaperTrail whether versions should be saved in the current request. | ||
def set_paper_trail_enabled_for_controller | ||
::PaperTrail.enabled_for_controller = paper_trail_enabled_for_controller | ||
end | ||
|
||
# Tells PaperTrail who is responsible for any changes that occur. | ||
def set_paper_trail_whodunnit | ||
::PaperTrail.whodunnit = user_for_paper_trail if paper_trail_enabled_for_controller | ||
end | ||
|
||
# DEPRECATED: please use `set_paper_trail_whodunnit` instead. | ||
def set_whodunnit | ||
logger.warn '[PaperTrail]: the `set_whodunnit` controller method has been deprecated. Please rename to `set_paper_trail_whodunnit`.' | ||
set_paper_trail_whodunnit | ||
end | ||
|
||
# Tells PaperTrail any information from the controller you want | ||
# to store alongside any changes that occur. | ||
def set_paper_trail_controller_info | ||
::PaperTrail.controller_info = info_for_paper_trail if paper_trail_enabled_for_controller | ||
end | ||
|
||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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.helpers PaperTrail::Sinatra | ||
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 | ||
current_user if defined?(current_user) | ||
end | ||
|
||
private | ||
|
||
# Tells PaperTrail who is responsible for any changes that occur. | ||
def set_paper_trail_whodunnit | ||
::PaperTrail.whodunnit = user_for_paper_trail if ::PaperTrail.enabled? | ||
end | ||
|
||
end | ||
end | ||
|
||
if defined?(Sinatra) | ||
module Sinatra | ||
register PaperTrail::Sinatra | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require 'test_helper' | ||
require 'sinatra/base' | ||
|
||
# --- Tests for modular `Sinatra::Base` style ---- | ||
class BaseApp < Sinatra::Base | ||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: File.expand_path('../../dummy/db/test.sqlite3', __FILE__)) | ||
register PaperTrail::Sinatra | ||
|
||
get '/test' do | ||
w = Widget.create!(name: 'foo') | ||
'Hello' | ||
end | ||
|
||
def current_user | ||
'foobar' | ||
end | ||
end | ||
|
||
class PaperTrailModularSinatraTest < ActiveSupport::TestCase | ||
include Rack::Test::Methods | ||
|
||
test 'baseline' do | ||
assert_nil Widget.first | ||
assert_nil Widget.create.versions.first.whodunnit | ||
end | ||
|
||
context "`PaperTrail::Sinatra` in a `Sinatra::Base` application" do | ||
|
||
def app | ||
@app ||= BaseApp | ||
end | ||
|
||
should "sets the `user_for_paper_trail` from the `current_user` method" do | ||
get '/test' | ||
assert_equal 'Hello', last_response.body | ||
widget = Widget.first | ||
assert_not_nil widget | ||
assert_equal 1, widget.versions.size | ||
assert_equal 'foobar', widget.versions.first.whodunnit | ||
end | ||
|
||
end | ||
end | ||
|
||
# --- Tests for non-modular `Sinatra::Application` style ---- | ||
class Sinatra::Application | ||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: File.expand_path('../../dummy/db/test.sqlite3', __FILE__)) | ||
|
||
get '/test' do | ||
w = Widget.create!(name: 'foo') | ||
'Hello' | ||
end | ||
|
||
def current_user | ||
'foobar' | ||
end | ||
end | ||
|
||
class PaperTrailSinatraTest < ActiveSupport::TestCase | ||
include Rack::Test::Methods | ||
|
||
def app | ||
@app ||= Sinatra::Application | ||
end | ||
|
||
test 'baseline' do | ||
assert_nil Widget.first | ||
assert_nil Widget.create.versions.first.whodunnit | ||
end | ||
|
||
context "`PaperTrail::Sinatra` in a `Sinatra::Application` application" do | ||
|
||
def app | ||
@app ||= BaseApp | ||
end | ||
|
||
should "sets the `user_for_paper_trail` from the `current_user` method" do | ||
get '/test' | ||
assert_equal 'Hello', last_response.body | ||
widget = Widget.first | ||
assert_not_nil widget | ||
assert_equal 1, widget.versions.size | ||
assert_equal 'foobar', widget.versions.first.whodunnit | ||
end | ||
|
||
end | ||
end | ||
|