audited_change_set 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{audited_change_set}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Chelimsky", "Brian Tatnall", "Nate Jackson", "Corey Haines"]
12
- s.date = %q{2010-05-13}
12
+ s.date = %q{2010-05-19}
13
13
  s.description = %q{change_set for acts_as_audited}
14
14
  s.email = %q{dchelimsky@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -40,9 +40,9 @@ Gem::Specification.new do |s|
40
40
  s.rubygems_version = %q{1.3.6}
41
41
  s.summary = %q{change_set for acts_as_audited}
42
42
  s.test_files = [
43
- "spec/audited_change_set/change_set_spec.rb",
43
+ "spec/db/schema.rb",
44
44
  "spec/audited_change_set/change_spec.rb",
45
- "spec/db/schema.rb",
45
+ "spec/audited_change_set/change_set_spec.rb",
46
46
  "spec/spec_helper.rb"
47
47
  ]
48
48
 
@@ -35,7 +35,8 @@ module AuditedChangeSet
35
35
 
36
36
  attr_reader :name, :old_value, :new_value
37
37
 
38
- def initialize(name, new_val, old_val=nil)
38
+ def initialize(change_type, name, new_val, old_val=nil)
39
+ @change_type = change_type
39
40
  @name = name.to_s
40
41
  @new_value, @old_value = [new_val, old_val].map {|val| transform_value(val) }
41
42
  end
@@ -46,11 +47,20 @@ module AuditedChangeSet
46
47
 
47
48
  def association_class
48
49
  @association_class ||= begin
49
- name.to_s =~ /(.*)_id$/
50
- $1.camelize.constantize
50
+ if reflection && class_name = reflection.options[:class_name]
51
+ class_name.constantize
52
+ else
53
+ name.to_s =~ /(.*)_id$/
54
+ $1.camelize.constantize
55
+ end
51
56
  end
52
57
  end
53
58
 
59
+ def reflection
60
+ change_class = @change_type.constantize
61
+ @reflection ||= change_class.reflect_on_association(name_without_id.to_sym) if change_class.respond_to?(:reflect_on_association)
62
+ end
63
+
54
64
  def get_associated_object(id)
55
65
  hook(:get_associated_object, id) || association_class.find_by_id(id)
56
66
  end
@@ -58,6 +68,10 @@ module AuditedChangeSet
58
68
  def association_field?
59
69
  name.ends_with? "_id"
60
70
  end
71
+
72
+ def name_without_id
73
+ name.chomp "_id"
74
+ end
61
75
  end
62
76
 
63
77
  class << self
@@ -85,7 +99,7 @@ module AuditedChangeSet
85
99
  end
86
100
 
87
101
  def create_field(name, changes)
88
- Field.new(name,*[changes].flatten.reverse)
102
+ Field.new(@audit.auditable_type, name, *[changes].flatten.reverse)
89
103
  end
90
104
 
91
105
  delegate :id, :to => :@audit
@@ -1,3 +1,4 @@
1
+ require "rubygems"
1
2
  require "active_record"
2
3
  require "active_record/base"
3
4
  require "acts_as_audited"
@@ -35,7 +35,7 @@ module AuditedChangeSet
35
35
  end
36
36
 
37
37
  context "change_set for an auditable model" do
38
- let(:auditable_model) { stub(AuditableModel, :name => 'irrelevant') }
38
+ let(:auditable_model) { stub(Person, :name => 'irrelevant') }
39
39
  let(:change_set) { ChangeSet.new(auditable_model) }
40
40
 
41
41
  describe "#each" do
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  module AuditedChangeSet
4
4
  describe Change do
5
5
 
6
- let(:audit) { double(Audit) }
6
+ let(:audit) { double(Audit, :auditable_type => "Person") }
7
7
 
8
8
  describe "::for_audits" do
9
9
  let(:audits) {[double(Audit), double(Audit)]}
@@ -207,13 +207,13 @@ module AuditedChangeSet
207
207
  context "field is an association" do
208
208
 
209
209
  before :each do
210
- AuditableModel.stub(:find_by_id).and_return(nil)
211
- AuditableModel.stub(:find_by_id).with("1").and_return(double(AuditableModel, :to_s => 'to_s_ified'))
210
+ Person.stub(:find_by_id).and_return(nil)
211
+ Person.stub(:find_by_id).with("1").and_return(double(Person, :to_s => 'to_s_ified'))
212
212
  end
213
213
 
214
214
  it "uses associated object for new value" do
215
- changes = { "auditable_model_id" => "1"}
216
- yielded_fields ={"auditable_model_id" => ["", "to_s_ified"]}
215
+ changes = { "person_id" => "1"}
216
+ yielded_fields ={"person_id" => ["", "to_s_ified"]}
217
217
 
218
218
  audit.stub(:action).and_return("create")
219
219
  audit.stub(:[]).and_return(changes)
@@ -221,13 +221,23 @@ module AuditedChangeSet
221
221
  end
222
222
 
223
223
  it "uses associated object for old value" do
224
- changes = { "auditable_model_id" => ["1", nil]}
225
- yielded_fields ={"auditable_model_id" => ["to_s_ified", ""]}
224
+ changes = { "person_id" => ["1", nil]}
225
+ yielded_fields ={"person_id" => ["to_s_ified", ""]}
226
226
 
227
227
  audit.stub(:action).and_return("create")
228
228
  audit.stub(:[]).and_return(changes)
229
229
  Change.new(audit).should yield_these(yielded_fields)
230
230
  end
231
+
232
+ it "reflects on association for belongs_to" do
233
+ changes = { "parent_id" => ["1", nil]}
234
+ yielded_fields ={"parent_id" => ["to_s_ified", ""]}
235
+
236
+ audit.stub(:action).and_return("create")
237
+ audit.stub(:[]).and_return(changes)
238
+
239
+ Change.new(audit).should yield_these(yielded_fields)
240
+ end
231
241
  end
232
242
 
233
243
  context "fields are specified" do
@@ -251,19 +261,19 @@ module AuditedChangeSet
251
261
 
252
262
  it "uses only the relevant fields that are associations" do
253
263
  models = [
254
- double(AuditableModel, :to_s => "more revenue"),
255
- double(AuditableModel, :to_s => "less cost")
264
+ double(Person, :to_s => "more revenue"),
265
+ double(Person, :to_s => "less cost")
256
266
  ]
257
- AuditableModel.stub(:find_by_id) do |options|
267
+ Person.stub(:find_by_id) do |options|
258
268
  models.shift
259
269
  end
260
270
 
261
- changes = { "title" => "irrelevant", "auditable_model_id" => ["1", "2"]}
262
- yielded_fields ={"auditable_model_id" => ["less cost", "more revenue"]}
271
+ changes = { "title" => "irrelevant", "person_id" => ["1", "2"]}
272
+ yielded_fields ={"person_id" => ["less cost", "more revenue"]}
263
273
 
264
274
  audit.stub(:action).and_return("create")
265
275
  audit.stub(:[]).and_return(changes)
266
- Change.new(audit, ["auditable_model_id"]).should yield_these(yielded_fields)
276
+ Change.new(audit, ["person_id"]).should yield_these(yielded_fields)
267
277
  end
268
278
  end
269
279
  end
@@ -280,7 +290,7 @@ module AuditedChangeSet
280
290
  yielded_args << block_arg
281
291
  end
282
292
 
283
- field_class.new("anything", "new", "old")
293
+ field_class.new("Person", "anything", "new", "old")
284
294
  yielded_args.should == ["new", "old"]
285
295
  end
286
296
 
@@ -290,7 +300,7 @@ module AuditedChangeSet
290
300
  "#{block_arg} modified by callback"
291
301
  end
292
302
 
293
- field = field_class.new("anything", "new value", "old value")
303
+ field = field_class.new("Person", "anything", "new value", "old value")
294
304
 
295
305
  field.new_value.should == "new value modified by callback"
296
306
  field.old_value.should == "old value modified by callback"
@@ -303,7 +313,7 @@ module AuditedChangeSet
303
313
  nil
304
314
  end
305
315
 
306
- field = field_class.new("anything", "new value", "old value")
316
+ field = field_class.new("Person", "anything", "new value", "old value")
307
317
 
308
318
  field.new_value.should == "new value"
309
319
  field.old_value.should == "old value"
@@ -319,7 +329,7 @@ module AuditedChangeSet
319
329
  yielded_args << block_arg
320
330
  end
321
331
 
322
- field_class.new("audited_model_id", 37, 42)
332
+ field_class.new("Person", "anything_id", 37, 42)
323
333
  yielded_args.should == [37, 42]
324
334
  end
325
335
 
@@ -329,7 +339,7 @@ module AuditedChangeSet
329
339
  "#{block_arg} returned by callback"
330
340
  end
331
341
 
332
- field = field_class.new("anything_id", "new value", "old value")
342
+ field = field_class.new("Person", "anything_id", "new value", "old value")
333
343
 
334
344
  field.new_value.should == "new value returned by callback"
335
345
  field.old_value.should == "old value returned by callback"
@@ -339,12 +349,12 @@ module AuditedChangeSet
339
349
  context "given the callback returns nil" do
340
350
  it "uses the default strategy to find the associated object" do
341
351
  returned_object = Object.new
342
- AuditableModel.stub(:find_by_id).with(37) { returned_object }
352
+ Person.stub(:find_by_id).with(37) { returned_object }
343
353
  field_class::hook(:get_associated_object) do |block_arg|
344
354
  nil
345
355
  end
346
356
 
347
- field = field_class.new("auditable_model_id", 37)
357
+ field = field_class.new("Person", "person_id", 37)
348
358
  field.new_value.should == returned_object.to_s
349
359
  end
350
360
  end
data/spec/db/schema.rb CHANGED
@@ -11,5 +11,7 @@ ActiveRecord::Schema.define(:version => 0) do
11
11
  t.column :created_at, :datetime
12
12
  end
13
13
 
14
- create_table :auditable_models do; end
14
+ create_table :people do |t|
15
+ t.integer :parent_id
16
+ end
15
17
  end
data/spec/spec_helper.rb CHANGED
@@ -11,5 +11,6 @@ ActiveRecord::Base.establish_connection(
11
11
  ActiveRecord::Migration.verbose = false
12
12
  load(File.dirname(__FILE__) + "/db/schema.rb")
13
13
 
14
- class AuditableModel < ActiveRecord::Base; end
15
-
14
+ class Person < ActiveRecord::Base
15
+ belongs_to :parent, :class_name => name, :foreign_key => "parent_id"
16
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Chelimsky
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-05-13 00:00:00 -05:00
20
+ date: 2010-05-19 00:00:00 -05:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -121,7 +121,7 @@ signing_key:
121
121
  specification_version: 3
122
122
  summary: change_set for acts_as_audited
123
123
  test_files:
124
- - spec/audited_change_set/change_set_spec.rb
125
- - spec/audited_change_set/change_spec.rb
126
124
  - spec/db/schema.rb
125
+ - spec/audited_change_set/change_spec.rb
126
+ - spec/audited_change_set/change_set_spec.rb
127
127
  - spec/spec_helper.rb