lukewendling-expressive_record 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -4,4 +4,8 @@ init release. still working out the bugs.
4
4
 
5
5
  2008-11-17 - 0.2.0
6
6
  ==========
7
- ready to play.
7
+ ready to play.
8
+
9
+ 2008-11-24 - 0.3.0
10
+ ==========
11
+ before_update callback now works as expected in subclasses
@@ -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.2.0') do |p|
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"
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{expressive_record}
5
- s.version = "0.2.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-18}
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"]
@@ -3,19 +3,20 @@ module ExpressiveRecord
3
3
  base.extend ClassMethods
4
4
  end
5
5
 
6
- # convention: the has_many association is named, i.e. ticket_changes, for model Ticket
7
- def expressify(has_many_assoc = nil)
8
- ActiveRecord::Base.transaction do
9
- self.changes.each do |attr, values|
10
- valuefied = valuefy(attr, values)
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])
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
- end
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 do |record|
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.2.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-18 00:00:00 -08:00
12
+ date: 2008-11-24 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15