lukewendling-expressive_record 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +5 -1
- data/README.rdoc +21 -8
- data/Rakefile +2 -2
- data/expressive_record.gemspec +4 -4
- data/lib/expressive_record.rb +10 -6
- metadata +4 -4
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -1,27 +1,34 @@
|
|
1
1
|
== Expressive Record
|
2
2
|
|
3
|
-
Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes (http://dev.rubyonrails.org/changeset/9127)
|
3
|
+
Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes (http://dev.rubyonrails.org/changeset/9127) to help with rolling your own history/audit logger.
|
4
4
|
|
5
5
|
== Install
|
6
6
|
|
7
|
-
|
7
|
+
script/plugin install git://github.com/lukewendling/expressive_record.git
|
8
|
+
or
|
9
|
+
sudo gem install lukewendling-expressive_record --source http://gems.github.com
|
10
|
+
|
11
|
+
create a migration for the audit table (see USAGE)
|
8
12
|
|
9
13
|
== Usage
|
10
14
|
|
11
15
|
ActiveRecord 2.1 introduces dirty changes; the ability to query a model object to track attribute changes before the record is saved using #changes, #changed, etc.
|
12
16
|
|
13
|
-
However, belongs_to associations are cached simply as foreign key changes, so Ticket#changes yields something like
|
17
|
+
However, belongs_to associations are cached simply as foreign key changes, so Ticket#changes yields something like {"status_id" => ["111", "222"]}, which isn't especially helpful when rolling your own audit logger.
|
14
18
|
|
15
|
-
Expressive
|
19
|
+
Expressive Record turns association (foreign key) changes into meaningful terms when the changes are saved to your audit table.
|
16
20
|
|
17
21
|
class Ticket < ActiveRecord::Base
|
18
|
-
include
|
22
|
+
include ExpressiveRecord
|
23
|
+
|
24
|
+
has_many :ticket_changes
|
19
25
|
|
20
|
-
|
26
|
+
# pass in the name of your audit table (optional)
|
27
|
+
express_changes :ticket_changes
|
21
28
|
|
22
29
|
end
|
23
30
|
|
24
|
-
|
31
|
+
express_changes adds a before_update callback to your model that inserts changes into an audit table with the following schema:
|
25
32
|
|
26
33
|
class CreateTicketChanges < ActiveRecord::Migration
|
27
34
|
def self.up
|
@@ -33,4 +40,10 @@ expressify_changes uses a before_update callback to register changes in a separa
|
|
33
40
|
t.timestamps
|
34
41
|
end
|
35
42
|
end
|
36
|
-
end
|
43
|
+
end
|
44
|
+
|
45
|
+
== Limitations
|
46
|
+
* the current implementation assumes that all belongs_to foreign keys use Rails conventional names, like user_id or status_id. if you are connecting to a legacy db with fk's like customerID or whatever, this won't work.
|
47
|
+
|
48
|
+
== TODO
|
49
|
+
* add migration generator to create conventionally-named log table (i.e. ticket_changes) to schema
|
data/Rakefile
CHANGED
@@ -2,8 +2,8 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('expressive_record', '0.
|
6
|
-
p.description = "Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes"
|
5
|
+
Echoe.new('expressive_record', '0.2.0') do |p|
|
6
|
+
p.description = "Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes to help with rolling your own history/audit logger."
|
7
7
|
p.url = "http://github.com/lukewendling/expressive_record"
|
8
8
|
p.author = "Luke Wendling"
|
9
9
|
p.email = "luke@lukewendling.com"
|
data/expressive_record.gemspec
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{expressive_record}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.2.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Luke Wendling"]
|
9
|
-
s.date = %q{2008-11-
|
10
|
-
s.description = %q{Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes}
|
9
|
+
s.date = %q{2008-11-18}
|
10
|
+
s.description = %q{Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes to help with rolling your own history/audit logger.}
|
11
11
|
s.email = %q{luke@lukewendling.com}
|
12
12
|
s.extra_rdoc_files = ["CHANGELOG", "lib/expressive_record.rb", "README.rdoc"]
|
13
13
|
s.files = ["CHANGELOG", "expressive_record.gemspec", "init.rb", "lib/expressive_record.rb", "Manifest", "Rakefile", "README.rdoc"]
|
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
s.rubyforge_project = %q{expressive_record}
|
19
19
|
s.rubygems_version = %q{1.3.1}
|
20
|
-
s.summary = %q{Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes}
|
20
|
+
s.summary = %q{Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes to help with rolling your own history/audit logger.}
|
21
21
|
|
22
22
|
if s.respond_to? :specification_version then
|
23
23
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/lib/expressive_record.rb
CHANGED
@@ -2,12 +2,15 @@ module ExpressiveRecord
|
|
2
2
|
def self.included(base)
|
3
3
|
base.extend ClassMethods
|
4
4
|
end
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
# convention: the has_many association is named, i.e. ticket_changes, for model Ticket
|
7
|
+
def expressify(has_many_assoc = nil)
|
7
8
|
ActiveRecord::Base.transaction do
|
8
9
|
self.changes.each do |attr, values|
|
9
10
|
valuefied = valuefy(attr, values)
|
10
|
-
|
11
|
+
assoc_name = has_many_assoc || "#{self.class.to_s.downcase}_changes"
|
12
|
+
assoc = self.send(assoc_name.to_sym)
|
13
|
+
assoc.create(:attribute_type => attr, :old_value => valuefied[0], :new_value => valuefied[1])
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
@@ -25,9 +28,10 @@ module ExpressiveRecord
|
|
25
28
|
end
|
26
29
|
|
27
30
|
module ClassMethods
|
28
|
-
def
|
31
|
+
def express_changes(has_many_assoc = nil)
|
29
32
|
before_update do |record|
|
30
|
-
record.
|
33
|
+
record.expressify has_many_assoc
|
31
34
|
end
|
32
35
|
end
|
33
|
-
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lukewendling-expressive_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luke Wendling
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-18 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes
|
16
|
+
description: Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes to help with rolling your own history/audit logger.
|
17
17
|
email: luke@lukewendling.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -61,6 +61,6 @@ rubyforge_project: expressive_record
|
|
61
61
|
rubygems_version: 1.2.0
|
62
62
|
signing_key:
|
63
63
|
specification_version: 2
|
64
|
-
summary: Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes
|
64
|
+
summary: Add meaningful names for belongs_to associations to Rails 2.1 model object dirty changes to help with rolling your own history/audit logger.
|
65
65
|
test_files: []
|
66
66
|
|