Skip to content

Commit

Permalink
close #346; cleanup code for 'user_for_paper_trail' default methods, …
Browse files Browse the repository at this point in the history
…add automated test coverage for various return values
  • Loading branch information
batter committed Mar 20, 2014
1 parent ab04c3c commit a0b93ab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 3.0.2 (Unreleased)

- [#346](https://github.com/airblade/paper_trail/pull/346) - `user_for_paper_trail` method should accomodate different types
for return values from `current_user` method.
- `PaperTrail::Cleaner.clean_versions!` should group versions by `PaperTrail.timestamp_field` when deciding which ones to
keep / destroy, instead of always grouping by the `created_at` field.

Expand Down
11 changes: 4 additions & 7 deletions lib/paper_trail/frameworks/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ def self.included(base)
# Override this method in your controller to call a different
# method, e.g. `current_person`, or anything you like.
def user_for_paper_trail
if defined?(current_user)
begin
current_user.try(:id)
rescue NoMethodError
current_user
end
end
return unless defined?(current_user)
ActiveSupport::VERSION::MAJOR >= 4 ? current_user.try!(:id) : current_user.try(:id)
rescue NoMethodError
current_user
end

# Returns any information about the controller or request that you
Expand Down
7 changes: 6 additions & 1 deletion lib/paper_trail/frameworks/sinatra.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_support/core_ext/object' # provides the `try` method

module PaperTrail
module Sinatra

Expand All @@ -15,7 +17,10 @@ def self.registered(app)
# 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.try(:id) if defined?(current_user)
return unless defined?(current_user)
ActiveSupport::VERSION::MAJOR >= 4 ? current_user.try!(:id) : current_user.try(:id)
rescue NoMethodError
current_user
end

private
Expand Down
13 changes: 13 additions & 0 deletions spec/requests/articles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@
PaperTrail.should be_enabled_for_controller
end
end

with_versioning do
let(:article) { Article.last }

context "`current_user` method returns a `String`" do
it "should set that value as the `whodunnit`" do
expect { post articles_path(valid_params) }.to change(PaperTrail::Version, :count).by(1)
article.title.should == 'Doh'
PaperTrail.whodunnit.should == 'foobar'
article.versions.last.whodunnit.should == 'foobar'
end
end
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ def create
end
head :ok
end

def current_user
'foobar'
end
end

0 comments on commit a0b93ab

Please sign in to comment.