audited_change_set 0.0.3 → 0.0.4

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.3
1
+ 0.0.4
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{audited_change_set}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
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"]
@@ -33,6 +33,8 @@ module AuditedChangeSet
33
33
  class Field
34
34
  include Hooks
35
35
 
36
+ @@display_methods = %w[field_name name to_s]
37
+
36
38
  attr_reader :name, :old_value, :new_value
37
39
 
38
40
  def initialize(change_type, name, new_val, old_val=nil)
@@ -42,7 +44,12 @@ module AuditedChangeSet
42
44
  end
43
45
 
44
46
  def transform_value(val)
45
- hook(:transform_value, val) || (association_field? ? get_associated_object(val).to_s : val.to_s)
47
+ hook(:transform_value, val) || (association_field? ? associated_value(val) : val.to_s)
48
+ end
49
+
50
+ def associated_value(val)
51
+ object = get_associated_object(val)
52
+ object.send(display_method(object))
46
53
  end
47
54
 
48
55
  def association_class
@@ -72,6 +79,11 @@ module AuditedChangeSet
72
79
  def name_without_id
73
80
  name.chomp "_id"
74
81
  end
82
+
83
+ private
84
+ def display_method(object)
85
+ @@display_methods.detect { |m| object.respond_to?(m) }
86
+ end
75
87
  end
76
88
 
77
89
  class << self
@@ -205,38 +205,59 @@ module AuditedChangeSet
205
205
  end
206
206
 
207
207
  context "field is an association" do
208
+ context "finds associated object" do
209
+ before :each do
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
+ end
208
213
 
209
- before :each do
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
- end
214
+ it "for new value" do
215
+ changes = { "person_id" => "1"}
216
+ yielded_fields ={"person_id" => ["", "to_s_ified"]}
213
217
 
214
- it "uses associated object for new value" do
215
- changes = { "person_id" => "1"}
216
- yielded_fields ={"person_id" => ["", "to_s_ified"]}
218
+ audit.stub(:[]).and_return(changes)
219
+ Change.new(audit).should yield_these(yielded_fields)
220
+ end
217
221
 
218
- audit.stub(:action).and_return("create")
219
- audit.stub(:[]).and_return(changes)
220
- Change.new(audit).should yield_these(yielded_fields)
221
- end
222
+ it "for old value" do
223
+ changes = { "person_id" => ["1", nil]}
224
+ yielded_fields ={"person_id" => ["to_s_ified", ""]}
222
225
 
223
- it "uses associated object for old value" do
224
- changes = { "person_id" => ["1", nil]}
225
- yielded_fields ={"person_id" => ["to_s_ified", ""]}
226
+ audit.stub(:[]).and_return(changes)
227
+ Change.new(audit).should yield_these(yielded_fields)
228
+ end
226
229
 
227
- audit.stub(:action).and_return("create")
228
- audit.stub(:[]).and_return(changes)
229
- Change.new(audit).should yield_these(yielded_fields)
230
+ it "by reflecting on belongs_to association" do
231
+ changes = { "parent_id" => ["1", nil]}
232
+ yielded_fields ={"parent_id" => ["to_s_ified", ""]}
233
+
234
+ audit.stub(:[]).and_return(changes)
235
+ Change.new(audit).should yield_these(yielded_fields)
236
+ end
230
237
  end
231
238
 
232
- it "reflects on association for belongs_to" do
233
- changes = { "parent_id" => ["1", nil]}
234
- yielded_fields ={"parent_id" => ["to_s_ified", ""]}
239
+ context "display" do
240
+ before :each do
241
+ Person.stub(:find_by_id).with("1").and_return(double(Person, :to_s => 'to_sified'))
242
+ Person.stub(:find_by_id).with("2").and_return(double(Person, :name => 'name_method', :to_s => 'to_sified'))
243
+ Person.stub(:find_by_id).with("3").and_return(double(Person, :field_name => 'field_name_method', :name => 'name_method'))
244
+ end
235
245
 
236
- audit.stub(:action).and_return("create")
237
- audit.stub(:[]).and_return(changes)
246
+ it "uses name before to_s" do
247
+ changes = { "parent_id" => ["1", "2"]}
248
+ yielded_fields = {"parent_id" => ["to_sified", "name_method"]}
249
+ audit.stub(:[]).and_return(changes)
238
250
 
239
- Change.new(audit).should yield_these(yielded_fields)
251
+ Change.new(audit).should yield_these(yielded_fields)
252
+ end
253
+
254
+ it "uses field name before name" do
255
+ changes = { "parent_id" => ["3", "2"]}
256
+ yielded_fields = {"parent_id" => ["field_name_method", "name_method"]}
257
+ audit.stub(:[]).and_return(changes)
258
+
259
+ Change.new(audit).should yield_these(yielded_fields)
260
+ end
240
261
  end
241
262
  end
242
263
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Chelimsky