lukewendling-expressive_record 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
1
  2008-11-17 - 0.1.0
2
2
  ==========
3
- init release. still working out the bugs.
3
+ init release. still working out the bugs.
4
+
5
+ 2008-11-17 - 0.2.0
6
+ ==========
7
+ ready to play.
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
- sudo gem install lukewendling-expressive_record --source http://gems.github.com
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 ["status_id" => {"111", "222"}], which isn't especially helpful when rolling your own ticket history mechanism.
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 Records turns foreign key-based changes into meaningful terms.
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 ExpressRecord
22
+ include ExpressiveRecord
23
+
24
+ has_many :ticket_changes
19
25
 
20
- expressify_changes
26
+ # pass in the name of your audit table (optional)
27
+ express_changes :ticket_changes
21
28
 
22
29
  end
23
30
 
24
- expressify_changes uses a before_update callback to register changes in a separate log table, by convention, that has the following schema:
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.1.0') do |p|
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"
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{expressive_record}
5
- s.version = "0.1.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-17}
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
@@ -2,12 +2,15 @@ module ExpressiveRecord
2
2
  def self.included(base)
3
3
  base.extend ClassMethods
4
4
  end
5
-
6
- def express_changes
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
- ticket_changes.create(:attribute_type => attr, :old_value => valuefied[0], :new_value => valuefied[1])
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 expressify_changes
31
+ def express_changes(has_many_assoc = nil)
29
32
  before_update do |record|
30
- record.express_changes
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.1.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-17 00:00:00 -08:00
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