Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/collectiveidea/audited in…
Browse files Browse the repository at this point in the history
…to re-fix-honoring-of-except-with-comment-required
  • Loading branch information
jeffdill2 committed Jul 9, 2018
2 parents 52c9bdb + 250cacc commit c9dd57d
Show file tree
Hide file tree
Showing 26 changed files with 413 additions and 173 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
.ruby-version
.rvmrc
.yardoc
coverage/
doc/
Gemfile.lock
gemfiles/*.lock
Expand Down
25 changes: 25 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.3
Exclude:
- lib/generators/audited/templates/**/*
- vendor/bundle/**/*
- gemfiles/vendor/bundle/**/*

Bundler/OrderedGems:
Enabled: false

Gemspec/OrderedDependencies:
Enabled: false

Layout:
Enabled: false

Metrics:
Enabled: false

Naming:
Enabled: false

Style:
Enabled: false
33 changes: 7 additions & 26 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: ruby
cache: bundler
rvm:
- 2.1
- 2.2.9
- 2.3.6
- 2.4.3
- 2.5.0
- 2.3.7
- 2.4.4
- 2.5.1
- ruby-head
env:
- DB=SQLITE
Expand All @@ -18,34 +16,17 @@ before_install:
- "travis_retry gem update --system"
- "travis_retry gem install bundler"
gemfile:
- gemfiles/rails40.gemfile
- gemfiles/rails41.gemfile
- gemfiles/rails42.gemfile
- gemfiles/rails50.gemfile
- gemfiles/rails51.gemfile
- gemfiles/rails52.gemfile
matrix:
include:
- rvm: 2.3.7
script: bundle exec rubocop --parallel
env: DB=rubocop # make travis build display nicer
allow_failures:
- rvm: ruby-head
exclude:
- rvm: 2.1
gemfile: gemfiles/rails50.gemfile
- rvm: 2.1
gemfile: gemfiles/rails51.gemfile
- rvm: 2.1
gemfile: gemfiles/rails52.gemfile
- rvm: 2.4.3
gemfile: gemfiles/rails40.gemfile
- rvm: 2.4.3
gemfile: gemfiles/rails41.gemfile
- rvm: 2.5.0
gemfile: gemfiles/rails40.gemfile
- rvm: 2.5.0
gemfile: gemfiles/rails41.gemfile
- rvm: ruby-head
gemfile: gemfiles/rails40.gemfile
- rvm: ruby-head
gemfile: gemfiles/rails41.gemfile
fast_finish: true
branches:
only:
Expand Down
13 changes: 1 addition & 12 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
appraise 'rails40' do
gem 'rails', '~> 4.0.0'
gem 'protected_attributes'
gem 'test-unit'
end

appraise 'rails41' do
gem 'rails', '~> 4.1.0'
gem 'protected_attributes'
end

appraise 'rails42' do
gem 'rails', '~> 4.2.0'
gem 'protected_attributes'
Expand All @@ -23,6 +12,6 @@ appraise 'rails51' do
end

appraise 'rails52' do
gem 'rails', '>= 5.2.0.rc1', '< 5.3'
gem 'rails', '>= 5.2.0', '< 5.3'
gem 'mysql2', '~> 0.4.4'
end
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@

Breaking changes

- removed block support for `Audit.reconstruct_attributes`
[#437](https://github.com/collectiveidea/audited/pull/437)
- removed `audited_columns`, `non_audited_columns`, `auditing_enabled=` instance methods,
use class methods instead
[#424](https://github.com/collectiveidea/audited/pull/424)
- removed rails 4.1 and 4.0 support
[#431](https://github.com/collectiveidea/audited/pull/431)

Added

- Add ability to globally disable auditing
[#426](https://github.com/collectiveidea/audited/pull/426)
- Add `own_and_associated_audits` method to auditable models
[#428](https://github.com/collectiveidea/audited/pull/428)
- Ability to nest `as_user` within itself
[#450](https://github.com/collectiveidea/audited/pull/450)
- Private methods can now be used for conditional auditing
[#454](https://github.com/collectiveidea/audited/pull/454)

Changed

- Add version to `auditable_index`
[#427](https://github.com/collectiveidea/audited/pull/427)

Fixed

- Ensure enum changes are stored consistently
[#429](https://github.com/collectiveidea/audited/pull/429)

## 4.7.1 (2018-04-10)

Breaking changes

- None

Added
Expand All @@ -18,6 +51,7 @@ Fixed

- Ensure that `except` option jibes with `comment_required: true`
[#425](https://github.com/collectiveidea/audited/pull/425)
- Allow use with Rails 5.2 final

## 4.7.0 (2018-03-14)

Expand Down
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.svg)](http://travis-ci.org/collectiveidea/audited) [![Dependency Status](https://gemnasium.com/collectiveidea/audited.svg)](https://gemnasium.com/collectiveidea/audited)[![Code Climate](https://codeclimate.com/github/collectiveidea/audited.svg)](https://codeclimate.com/github/collectiveidea/audited) [![Security](https://hakiri.io/github/collectiveidea/audited/master.svg)](https://hakiri.io/github/collectiveidea/audited/master)
Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.svg)](http://travis-ci.org/collectiveidea/audited) [![Code Climate](https://codeclimate.com/github/collectiveidea/audited.svg)](https://codeclimate.com/github/collectiveidea/audited) [![Security](https://hakiri.io/github/collectiveidea/audited/master.svg)](https://hakiri.io/github/collectiveidea/audited/master)
=======

**Audited** (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited can also record who made those changes, save comments and associate models related to the changes.

Audited currently (4.x) works with Rails 5.1, 5.0 and 4.2. It may work with 4.1 and 4.0, but this is not guaranteed.
Audited currently (4.x) works with Rails 5.2, 5.1, 5.0 and 4.2.

For Rails 3, use gem version 3.0 or see the [3.0-stable branch](https://github.com/collectiveidea/audited/tree/3.0-stable).

## Supported Rubies

Audited supports and is [tested against](http://travis-ci.org/collectiveidea/audited) the following Ruby versions:

* 2.1.10
* 2.2.9
* 2.3.6
* 2.4.3
* 2.5.0
* 2.3.7
* 2.4.4
* 2.5.1

Audited may work just fine with a Ruby version not listed above, but we can't guarantee that it will. If you'd like to maintain a Ruby that isn't listed, please let us know with a [pull request](https://github.com/collectiveidea/audited/pulls).

Expand Down Expand Up @@ -193,7 +191,7 @@ Outside of a request, Audited can still record the user with the `as_user` metho

```ruby
Audited.audit_class.as_user(User.find(1)) do
post.update_attribute!(title: "Hello, world!")
post.update_attributes!(title: "Hello, world!")
end
post.audits.last.user # => #<User id: 1>
```
Expand Down Expand Up @@ -255,6 +253,11 @@ user.audits.last.associated # => #<Company name: "Collective Idea">
company.associated_audits.last.auditable # => #<User name: "Steve Richert">
```

You can access records' own audits and associated audits in one go:
```ruby
company.own_and_associated_audits
```

### Conditional auditing

If you want to audit only under specific conditions, you can provide conditional options (similar to ActiveModel callbacks) that will ensure your model is only audited for these conditions.
Expand Down Expand Up @@ -312,6 +315,12 @@ To disable auditing on an entire model:
User.auditing_enabled = false
```

To disable auditing on all models:

```ruby
Audited.auditing_enabled = false
```

### Custom `Audit` model

If you want to extend or modify the audit model, create a new class that
Expand Down
10 changes: 6 additions & 4 deletions audited.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require "audited/version"
Gem::Specification.new do |gem|
gem.name = 'audited'
gem.version = Audited::VERSION
gem.platform = Gem::Platform::RUBY

gem.authors = ['Brandon Keepers', 'Kenneth Kalmer', 'Daniel Morrison', 'Brian Ryckbost', 'Steve Richert', 'Ryan Glover']
gem.email = '[email protected]'
Expand All @@ -15,13 +14,16 @@ Gem::Specification.new do |gem|
gem.license = 'MIT'

gem.files = `git ls-files`.split($\).reject{|f| f =~ /(\.gemspec)/ }
gem.require_paths = ['lib']

gem.add_dependency 'activerecord', '>= 4.0', '< 5.2'
gem.required_ruby_version = '>= 2.3.0'

gem.add_dependency 'activerecord', '>= 4.2', '< 5.3'

gem.add_development_dependency 'appraisal'
gem.add_development_dependency 'rails', '>= 4.0', '< 5.2'
gem.add_development_dependency 'rails', '>= 4.2', '< 5.3'
gem.add_development_dependency 'rubocop', '~> 0.54.0'
gem.add_development_dependency 'rspec-rails', '~> 3.5'
gem.add_development_dependency 'single_cov'

# JRuby support for the test ENV
if defined?(JRUBY_VERSION)
Expand Down
9 changes: 0 additions & 9 deletions gemfiles/rails40.gemfile

This file was deleted.

8 changes: 0 additions & 8 deletions gemfiles/rails41.gemfile

This file was deleted.

2 changes: 1 addition & 1 deletion gemfiles/rails52.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source "https://rubygems.org"

gem "rails", ">= 5.2.0.rc1", "< 5.3"
gem "rails", ">= 5.2.0", "< 5.3"
gem "mysql2", "~> 0.4.4"

gemspec name: "audited", path: "../"
3 changes: 2 additions & 1 deletion lib/audited.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Audited
class << self
attr_accessor :ignored_attributes, :current_user_method, :max_audits
attr_accessor :ignored_attributes, :current_user_method, :max_audits, :auditing_enabled
attr_writer :audit_class

def audit_class
Expand All @@ -21,6 +21,7 @@ def config
@ignored_attributes = %w(lock_version created_at updated_at created_on updated_on)

@current_user_method = :current_user
@auditing_enabled = true
end

require 'audited/auditor'
Expand Down
37 changes: 17 additions & 20 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,18 @@ def old_attributes

# Allows user to undo changes
def undo
model = self.auditable_type.constantize
if action == 'create'
case action
when 'create'
# destroys a newly created record
model.find(auditable_id).destroy!
elsif action == 'destroy'
auditable.destroy!
when 'destroy'
# creates a new record with the destroyed record attributes
model.create(audited_changes)
else
auditable_type.constantize.create!(audited_changes)
when 'update'
# changes back attributes
audited_object = model.find(auditable_id)
self.audited_changes.each do |k, v|
audited_object[k] = v[0]
end
audited_object.save
auditable.update_attributes!(audited_changes.transform_values(&:first))
else
raise StandardError, "invalid action given #{action}"
end
end

Expand Down Expand Up @@ -132,21 +130,20 @@ def self.audited_classes
# All audits made during the block called will be recorded as made
# by +user+. This method is hopefully threadsafe, making it ideal
# for background operations that require audit information.
def self.as_user(user, &block)
def self.as_user(user)
last_audited_user = ::Audited.store[:audited_user]
::Audited.store[:audited_user] = user
yield
ensure
::Audited.store[:audited_user] = nil
::Audited.store[:audited_user] = last_audited_user
end

# @private
def self.reconstruct_attributes(audits)
attributes = {}
result = audits.collect do |audit|
attributes.merge!(audit.new_attributes)[:version] = audit.version
yield attributes if block_given?
audits.each_with_object({}) do |audit, all|
all.merge!(audit.new_attributes)
all[:version] = audit.version
end
block_given? ? result : attributes
end

# @private
Expand All @@ -164,14 +161,14 @@ def self.assign_revision_attributes(record, attributes)
end

# use created_at as timestamp cache key
def self.collection_cache_key(collection = all, timestamp_column = :created_at)
def self.collection_cache_key(collection = all, *)
super(collection, :created_at)
end

private

def set_version_number
max = self.class.auditable_finder(auditable_id, auditable_type).descending.first.try(:version) || 0
max = self.class.auditable_finder(auditable_id, auditable_type).maximum(:version) || 0
self.version = max + 1
end

Expand Down
Loading

0 comments on commit c9dd57d

Please sign in to comment.