Skip to content

Commit 100362e

Browse files
committed
New docs with YARD (and various cleanups while documenting)
1 parent f37b8fe commit 100362e

File tree

10 files changed

+89
-54
lines changed

10 files changed

+89
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pkg
66
*.swp
77
.bundle
88
tmp/*
9+
.yardoc

.yardopts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--no-private
2+
--title acts_as_audited
3+
--exclude lib/generators

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ gem "rails", "3.0.1"
44
gem "sqlite3-ruby"
55
gem "rspec-rails", "2.0.1"
66
gem "rcov"
7+
gem "yard"
8+
gem "jeweler"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ GEM
7979
treetop (1.4.8)
8080
polyglot (>= 0.3.1)
8181
tzinfo (0.3.23)
82+
yard (0.6.1)
8283

8384
PLATFORMS
8485
ruby
@@ -88,3 +89,4 @@ DEPENDENCIES
8889
rcov
8990
rspec-rails (= 2.0.1)
9091
sqlite3-ruby
92+
yard

README.rdoc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
+acts_as_audited+ is an ActiveRecord extension that logs all changes to your
44
models in an audits table, with optional revision comments. +acts_as_audited+
55
has been updated to work with Rails 3, to use it with older version of Rails,
6-
please see the rails2 branch.
6+
please see the <tt>rails2</tt> branch.
77

88
== Installation
99

1010
In <tt>Gemfile</tt>:
1111

12-
gem "acts_as_audited"
12+
gem "acts_as_audited", "2.0.0.rc1"
1313

1414
In your application root, run:
1515

@@ -29,6 +29,14 @@ Generate the migration:
2929

3030
$ rake db:migrate
3131

32+
== Upgrading
33+
34+
Upgrading to Rails 3, or even between point releases of +acts_as_audited+, might require alterations to the audits table. After every upgrade please run the following generator:
35+
36+
$ rails g acts_as_audited:upgrade
37+
38+
The upgrade generator will only generate migrations that are missing, or no migrations at all if you are up to date.
39+
3240
== Usage
3341

3442
Declare +acts_as_audited+ on your models:
@@ -59,17 +67,17 @@ If your model declares +attr_accessible+ after +acts_as_audited+, you need to se
5967

6068
== Compatability
6169

62-
+acts_as_audited+ works with Rails 3.0.0 or later.
70+
+acts_as_audited+ works with Rails 3.0.0 or later. For older versions of Rails, please see the <tt>rails2</tt> branch.
6371

6472
== Getting Help
6573

66-
Join the mailing list for getting help or offering suggestions:
67-
http://groups.google.com/group/acts_as_audited
74+
Review the documentation at http://rdoc.info/github/collectiveidea/acts_as_audited
75+
76+
Join the mailing list for getting help or offering suggestions - http://groups.google.com/group/acts_as_audited
6877

6978
== Contributing
7079

71-
Contributions are always welcome. Checkout the latest code on GitHub:
72-
http://github.com/collectiveidea/acts_as_audited
80+
Contributions are always welcome. Checkout the latest code on GitHub - http://github.com/collectiveidea/acts_as_audited
7381

7482
Please include tests with your patches. There are a few gems required to run the tests:
7583

@@ -79,5 +87,4 @@ Make sure the tests pass against the version of Rails specified in the Gemfile
7987

8088
$ rake spec test
8189

82-
Please report bugs or feature suggestions on GitHub:
83-
http://github.com/collectiveidea/acts_as_audited/issues
90+
Please report bugs or feature suggestions on GitHub - http://github.com/collectiveidea/acts_as_audited/issues

Rakefile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'rake'
22
require 'rspec/core/rake_task'
33
require 'rake/testtask'
4-
require 'rake/rdoctask'
54

65
$:.unshift File.expand_path('../lib', __FILE__)
76

@@ -28,7 +27,7 @@ begin
2827
end
2928
# Jeweler::GemcutterTasks.new
3029
rescue LoadError
31-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
30+
puts "Jeweler (or a dependency) not available. Install it with: bundle install"
3231
end
3332

3433
RSpec::Core::RakeTask.new do |t|
@@ -53,11 +52,10 @@ end
5352

5453
task :test => :check_dependencies
5554

56-
desc 'Generate documentation for the acts_as_audited plugin.'
57-
Rake::RDocTask.new(:rdoc) do |rdoc|
58-
rdoc.rdoc_dir = 'doc'
59-
rdoc.title = 'acts_as_audited'
60-
rdoc.options << '--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
61-
rdoc.rdoc_files.include('README.rdoc')
62-
rdoc.rdoc_files.include('lib/**/*.rb')
55+
begin
56+
require 'yard'
57+
YARD::Rake::YardocTask.new
58+
rescue LoadError
59+
puts "YARD (or a dependency) not available. Install it with: bundle install"
6360
end
61+

lib/acts_as_audited.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
require 'active_record'
2323

24+
# To get started, please review ActsAsAudited::Auditor::ClassMethods#acts_as_audited
2425
module ActsAsAudited
2526
VERSION = '2.0.0.rc1'
2627

27-
# The method to be called to return the current user for logging in the audits. :current_user by default
2828
mattr_accessor :current_user_method
29+
# The method to be called to return the current user for logging in the audits.
2930
@@current_user_method = :current_user
31+
3032
end
3133

3234
require 'acts_as_audited/auditor'

lib/acts_as_audited/audit.rb

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# * <tt>user</tt>: the user that performed the change; a string or an ActiveRecord model
77
# * <tt>action</tt>: one of create, update, or delete
88
# * <tt>audited_changes</tt>: a serialized hash of all the changes
9+
# * <tt>comment</tt>: a comment set with the audit
910
# * <tt>created_at</tt>: Time that the change was performed
1011
#
1112
class Audit < ActiveRecord::Base
@@ -19,23 +20,51 @@ class Audit < ActiveRecord::Base
1920
cattr_accessor :audited_class_names
2021
self.audited_class_names = Set.new
2122

22-
def self.audited_classes
23-
self.audited_class_names.map(&:constantize)
24-
end
23+
class << self
24+
25+
# Returns the list of classes that are being audited
26+
def audited_classes
27+
audited_class_names.map(&:constantize)
28+
end
29+
30+
# All audits made during the block called will be recorded as made
31+
# by +user+. This method is hopefully threadsafe, making it ideal
32+
# for background operations that require audit information.
33+
def as_user(user, &block)
34+
Thread.current[:acts_as_audited_user] = user
35+
36+
yield
2537

26-
# All audits made during the block called will be recorded as made
27-
# by +user+. This method is hopefully threadsafe, making it ideal
28-
# for background operations that require audit information.
29-
def self.as_user(user, &block)
30-
Thread.current[:acts_as_audited_user] = user
38+
Thread.current[:acts_as_audited_user] = nil
39+
end
40+
41+
# @private
42+
def reconstruct_attributes(audits)
43+
attributes = {}
44+
result = audits.collect do |audit|
45+
attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
46+
yield attributes if block_given?
47+
end
48+
block_given? ? result : attributes
49+
end
3150

32-
yield
51+
# @private
52+
def assign_revision_attributes(record, attributes)
53+
attributes.each do |attr, val|
54+
if record.respond_to?("#{attr}=")
55+
record.attributes.has_key?(attr.to_s) ?
56+
record[attr] = val :
57+
record.send("#{attr}=", val)
58+
end
59+
end
60+
record
61+
end
3362

34-
Thread.current[:acts_as_audited_user] = nil
3563
end
3664

3765
# Allows user to be set to either a string or an ActiveRecord object
38-
def user_as_string=(user) #:nodoc:
66+
# @private
67+
def user_as_string=(user)
3968
# reset both either way
4069
self.user_as_model = self.username = nil
4170
user.is_a?(ActiveRecord::Base) ?
@@ -45,19 +74,23 @@ def user_as_string=(user) #:nodoc:
4574
alias_method :user_as_model=, :user=
4675
alias_method :user=, :user_as_string=
4776

48-
def user_as_string #:nodoc:
77+
# @private
78+
def user_as_string
4979
self.user_as_model || self.username
5080
end
5181
alias_method :user_as_model, :user
5282
alias_method :user, :user_as_string
5383

84+
# Return an instantance of what the object looked like at this revision. If
85+
# the object has been destroyed, this will be a new record.
5486
def revision
5587
clazz = auditable_type.constantize
5688
( clazz.find_by_id(auditable_id) || clazz.new ).tap do |m|
5789
Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:version => version}))
5890
end
5991
end
6092

93+
# Return all audits older than the current one.
6194
def ancestors
6295
self.class.find(:all, :order => 'version',
6396
:conditions => ['auditable_id = ? and auditable_type = ? and version <= ?',
@@ -80,26 +113,6 @@ def old_attributes
80113
end
81114
end
82115

83-
def self.reconstruct_attributes(audits)
84-
attributes = {}
85-
result = audits.collect do |audit|
86-
attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
87-
yield attributes if block_given?
88-
end
89-
block_given? ? result : attributes
90-
end
91-
92-
def self.assign_revision_attributes(record, attributes)
93-
attributes.each do |attr, val|
94-
if record.respond_to?("#{attr}=")
95-
record.attributes.has_key?(attr.to_s) ?
96-
record[attr] = val :
97-
record.send("#{attr}=", val)
98-
end
99-
end
100-
record
101-
end
102-
103116
private
104117

105118
def set_version_number

lib/acts_as_audited/audit_sweeper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
class AuditSweeper < ActionController::Caching::Sweeper #:nodoc:
1+
# @private
2+
class AuditSweeper < ActionController::Caching::Sweeper
23
observe Audit
34

45
def before_create(audit)

lib/acts_as_audited/auditor.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module ActsAsAudited
1414
module Auditor #:nodoc:
1515
CALLBACKS = [:audit_create, :audit_update, :audit_destroy]
1616

17-
def self.included(base) # :nodoc:
17+
# @private
18+
def self.included(base)
1819
base.extend ClassMethods
1920
end
2021

@@ -126,11 +127,13 @@ def revision(version)
126127
revision_with Audit.reconstruct_attributes(audits_to(version))
127128
end
128129

130+
# Find the oldest revision recorded prior to the date/time provided.
129131
def revision_at(date_or_time)
130132
audits = self.audits.find(:all, :conditions => ["created_at <= ?", date_or_time])
131133
revision_with Audit.reconstruct_attributes(audits) unless audits.empty?
132134
end
133135

136+
# List of attributes that are audited.
134137
def audited_attributes
135138
attributes.except(*non_audited_columns)
136139
end
@@ -235,17 +238,20 @@ def without_auditing(&block)
235238
block.call.tap { enable_auditing if auditing_was_enabled }
236239
end
237240

241+
# Disable auditing.
238242
def disable_auditing
239243
write_inheritable_attribute :auditing_enabled, false
240244
end
241245

246+
# Enable auditing.
242247
def enable_auditing
243248
write_inheritable_attribute :auditing_enabled, true
244249
end
245250

246251
# All audit operations during the block are recorded as being
247252
# made by +user+. This is not model specific, the method is a
248-
# convenience wrapper around #Audit.as_user.
253+
# convenience wrapper around
254+
# @see Audit#as_user.
249255
def audit_as( user, &block )
250256
Audit.as_user( user, &block )
251257
end

0 commit comments

Comments
 (0)