lukewendling-expressive_record 0.2.0 → 0.3.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 +14 -0
- data/Rakefile +1 -1
- data/expressive_record.gemspec +2 -2
- data/lib/expressive_record.rb +13 -14
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -42,6 +42,20 @@ express_changes adds a before_update callback to your model that inserts changes
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
If using a before_update callback in subclasses, be sure to call 'super' as needed:
|
46
|
+
|
47
|
+
class A
|
48
|
+
# a before_update callback is auto-magically added
|
49
|
+
express_changes
|
50
|
+
end
|
51
|
+
|
52
|
+
class A < B
|
53
|
+
def before_update
|
54
|
+
self.status = 'New' # do this before auditing
|
55
|
+
super # now insert changes into audit log
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
45
59
|
== Limitations
|
46
60
|
* 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
61
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('expressive_record', '0.
|
5
|
+
Echoe.new('expressive_record', '0.3.0') do |p|
|
6
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"
|
data/expressive_record.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{expressive_record}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.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-
|
9
|
+
s.date = %q{2008-11-24}
|
10
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"]
|
data/lib/expressive_record.rb
CHANGED
@@ -3,19 +3,20 @@ module ExpressiveRecord
|
|
3
3
|
base.extend ClassMethods
|
4
4
|
end
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
private
|
7
|
+
|
8
|
+
# convention: the has_many association is named, i.e. ticket_changes, for model Ticket
|
9
|
+
def expressify(has_many_assoc = nil)
|
10
|
+
ActiveRecord::Base.transaction do
|
11
|
+
self.changes.each do |attr, values|
|
12
|
+
valuefied = valuefy(attr, values)
|
13
|
+
assoc_name = has_many_assoc || "#{self.class.to_s.downcase}_changes"
|
14
|
+
assoc = self.send(assoc_name.to_sym)
|
15
|
+
assoc.create(:attribute_type => attr, :old_value => valuefied[0], :new_value => valuefied[1])
|
16
|
+
end
|
14
17
|
end
|
15
18
|
end
|
16
|
-
|
17
|
-
|
18
|
-
private
|
19
|
+
|
19
20
|
# find association's meaningful value (i.e. user.name instead of user id)
|
20
21
|
def valuefy(attr, values, assoc_attr = :name)
|
21
22
|
# TODO: is there a better way to determine if an attr is an association
|
@@ -29,9 +30,7 @@ module ExpressiveRecord
|
|
29
30
|
|
30
31
|
module ClassMethods
|
31
32
|
def express_changes(has_many_assoc = nil)
|
32
|
-
before_update
|
33
|
-
record.expressify has_many_assoc
|
34
|
-
end
|
33
|
+
define_method(:before_update) { expressify has_many_assoc }
|
35
34
|
end
|
36
35
|
end
|
37
36
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luke Wendling
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-24 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|