audited_change_set 0.0.3 → 0.0.4
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/VERSION +1 -1
- data/audited_change_set.gemspec +1 -1
- data/lib/audited_change_set/change.rb +13 -1
- data/spec/audited_change_set/change_spec.rb +44 -23
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/audited_change_set.gemspec
CHANGED
@@ -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.
|
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? ?
|
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
|
-
|
210
|
-
|
211
|
-
|
212
|
-
end
|
214
|
+
it "for new value" do
|
215
|
+
changes = { "person_id" => "1"}
|
216
|
+
yielded_fields ={"person_id" => ["", "to_s_ified"]}
|
213
217
|
|
214
|
-
|
215
|
-
|
216
|
-
|
218
|
+
audit.stub(:[]).and_return(changes)
|
219
|
+
Change.new(audit).should yield_these(yielded_fields)
|
220
|
+
end
|
217
221
|
|
218
|
-
|
219
|
-
|
220
|
-
|
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
|
-
|
224
|
-
|
225
|
-
|
226
|
+
audit.stub(:[]).and_return(changes)
|
227
|
+
Change.new(audit).should yield_these(yielded_fields)
|
228
|
+
end
|
226
229
|
|
227
|
-
|
228
|
-
|
229
|
-
|
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
|
-
|
233
|
-
|
234
|
-
|
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
|
-
|
237
|
-
|
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
|
-
|
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
|
|