changeling 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -99,10 +99,11 @@ Properties of Loglings (history objects):
99
99
  log = @post.history.first
100
100
 
101
101
  log.klass # class of the object that the Logling is tracking.
102
- => "posts"
102
+ => Post
103
103
 
104
104
  log.oid # the ID of the object that the Logling is tracking.
105
- => "1"
105
+ # Note: integer type IDs will be integers. Non-integer types (such as Mongo's IDs) will be represented as a string
106
+ => 1
106
107
 
107
108
  log.before # what the before state of the object was.
108
109
  => {"title" => "Old Title"}
@@ -117,7 +118,7 @@ log.modified_at # what time these changes were made.
117
118
  => Sat, 08 Sep 2012 10:21:46 UTC +00:00
118
119
 
119
120
  log.as_json # JSON representation of the changes.
120
- => {:modifications=>{"title" => ["Old Title", "New Title"], :modified_at => Sat, 08 Sep 2012 10:21:46 UTC +00:00}
121
+ => {:class => Post, :oid => 1, :modifications=> { "title" => ["Old Title", "New Title"] }, :modified_at => Sat, 08 Sep 2012 10:21:46 UTC +00:00}
121
122
  ```
122
123
 
123
124
  ## Testing
@@ -38,17 +38,16 @@ module Changeling
38
38
  end
39
39
 
40
40
  def klassify(object)
41
- object.class.to_s.underscore
41
+ object.class
42
42
  end
43
43
 
44
44
  def records_for(object, length = nil)
45
45
  self.tire.index.refresh
46
-
47
46
  results = self.search do
48
47
  query do
49
48
  filtered do
50
49
  query { all }
51
- filter :terms, :klass => [Logling.klassify(object)]
50
+ filter :terms, :klass => [Logling.klassify(object).to_s.underscore]
52
51
  filter :terms, :oid => [object.id.to_s]
53
52
  end
54
53
  end
@@ -66,18 +65,27 @@ module Changeling
66
65
 
67
66
  def to_indexed_json
68
67
  {
69
- :klass => self.klass,
70
- :oid => self.oid,
68
+ :klass => self.klass.to_s.underscore,
69
+ :oid => self.oid.to_s,
71
70
  :modifications => self.modifications.to_json,
72
71
  :modified_at => self.modified_at
73
72
  }.to_json
74
73
  end
75
74
 
75
+ def as_json
76
+ {
77
+ :class => self.klass,
78
+ :oid => self.oid,
79
+ :modifications => self.modifications,
80
+ :modified_at => self.modified_at
81
+ }
82
+ end
83
+
76
84
  def initialize(object)
77
85
  if object.class == Hash
78
86
  changes = JSON.parse(object['modifications'])
79
- self.klass = object['klass']
80
- self.oid = object['oid']
87
+ self.klass = object['klass'].camelize.constantize
88
+ self.oid = object['oid'].to_i.to_s == object['oid'] ? object['oid'].to_i : object['oid']
81
89
  self.modifications = changes
82
90
 
83
91
  self.before, self.after = Logling.parse_changes(changes)
@@ -90,7 +98,7 @@ module Changeling
90
98
  changes.delete("updated_at")
91
99
 
92
100
  self.klass = Logling.klassify(object)
93
- self.oid = object.id.to_s
101
+ self.oid = object.id
94
102
  self.modifications = changes
95
103
 
96
104
  self.before, self.after = Logling.parse_changes(changes)
@@ -1,3 +1,3 @@
1
1
  module Changeling
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -35,7 +35,7 @@ describe Changeling::Models::Logling do
35
35
  context "when passed object is a ElasticSearch response hash" do
36
36
  before(:each) do
37
37
  @object = {
38
- "klass"=>"blog_post",
38
+ "klass"=>"BlogPost",
39
39
  "oid"=>"50b8355f7a93d04908000001",
40
40
  "modifications"=>"{\"public\":[true,false]}",
41
41
  "modified_at"=>"2012-11-29T20:26:07-08:00",
@@ -56,13 +56,18 @@ describe Changeling::Models::Logling do
56
56
  end
57
57
 
58
58
  it "should set klass as the returned klass value" do
59
- @logling.klass.should == @object['klass']
59
+ @logling.klass.should == @object['klass'].constantize
60
60
  end
61
61
 
62
62
  it "should set oid as the returned oid value" do
63
63
  @logling.oid.should == @object['oid']
64
64
  end
65
65
 
66
+ it "should convert oid to an integer if it's supposed to be an integer" do
67
+ @object["oid"] = "1"
68
+ @klass.new(@object).oid.should == @object["oid"].to_i
69
+ end
70
+
66
71
  it "should set the modifications as the incoming changes parameter" do
67
72
  @logling.modifications.should == @changes
68
73
  end
@@ -86,8 +91,8 @@ describe Changeling::Models::Logling do
86
91
  @logling.klass.should == @klass.klassify(@object)
87
92
  end
88
93
 
89
- it "should set oid as the stringified object's ID" do
90
- @logling.oid.should == @object.id.to_s
94
+ it "should set oid as the object's ID" do
95
+ @logling.oid.should == @object.id
91
96
  end
92
97
 
93
98
  it "should set the modifications as the incoming changes parameter" do
@@ -144,8 +149,8 @@ describe Changeling::Models::Logling do
144
149
  end
145
150
 
146
151
  describe ".klassify" do
147
- it "should return the underscored version of the objects class as a string" do
148
- @klass.klassify(@object).should == @object.class.to_s.underscore
152
+ it "should return the object's class" do
153
+ @klass.klassify(@object).should == @object.class
149
154
  end
150
155
  end
151
156
 
@@ -204,6 +209,32 @@ describe Changeling::Models::Logling do
204
209
  end
205
210
  end
206
211
 
212
+ describe ".as_json" do
213
+ before(:each) do
214
+ @json = @logling.as_json
215
+ end
216
+
217
+ it "should include the object's klass attribute" do
218
+ @json[:class].should == @klass.klassify(@object)
219
+ end
220
+
221
+ it "should include the object's oid attribute" do
222
+ @json[:oid].should == @object.id
223
+ end
224
+
225
+ it "should include the object's modifications attribute" do
226
+ @json[:modifications].should == @changes
227
+ end
228
+
229
+ it "should include the object's modified_at attribute" do
230
+ @json[:modified_at].should == @logling.modified_at
231
+ end
232
+
233
+ after(:each) do
234
+ @logling.to_indexed_json
235
+ end
236
+ end
237
+
207
238
  describe ".save" do
208
239
  it "should update the ElasticSearch index" do
209
240
  @logling.should_receive(:_run_save_callbacks)
@@ -22,7 +22,7 @@ describe Changeling::Probeling do
22
22
  end
23
23
 
24
24
  describe ".all_history" do
25
- it "should query Logling with it's pluralized class name, and it's own ID" do
25
+ it "should query Logling with it's class name, and it's own ID" do
26
26
  @klass.should_receive(:records_for).with(@object)
27
27
  @object.all_history
28
28
  end
@@ -33,7 +33,7 @@ describe Changeling::Probeling do
33
33
  end
34
34
 
35
35
  describe ".history" do
36
- it "should query Logling with it's pluralized class name, and it's own ID, and a default number of loglings to return" do
36
+ it "should query Logling with it's class name, and it's own ID, and a default number of loglings to return" do
37
37
  @klass.should_receive(:records_for).with(@object, 10)
38
38
  @object.history
39
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: changeling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tire
16
- requirement: &70134819579460 !ruby/object:Gem::Requirement
16
+ requirement: &70333793166600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70134819579460
24
+ version_requirements: *70333793166600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &70134819577920 !ruby/object:Gem::Requirement
27
+ requirement: &70333793164960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70134819577920
35
+ version_requirements: *70333793164960
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mongoid
38
- requirement: &70134819577040 !ruby/object:Gem::Requirement
38
+ requirement: &70333793164040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 3.0.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70134819577040
46
+ version_requirements: *70333793164040
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activerecord
49
- requirement: &70134819575880 !ruby/object:Gem::Requirement
49
+ requirement: &70333793163020 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - =
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 3.2.7
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70134819575880
57
+ version_requirements: *70333793163020
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: debugger
60
- requirement: &70134819590880 !ruby/object:Gem::Requirement
60
+ requirement: &70333793178260 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70134819590880
68
+ version_requirements: *70333793178260
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
- requirement: &70134819589240 !ruby/object:Gem::Requirement
71
+ requirement: &70333793176620 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70134819589240
79
+ version_requirements: *70333793176620
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &70134819587220 !ruby/object:Gem::Requirement
82
+ requirement: &70333793174580 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70134819587220
90
+ version_requirements: *70333793174580
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: bson_ext
93
- requirement: &70134819586580 !ruby/object:Gem::Requirement
93
+ requirement: &70333793173940 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70134819586580
101
+ version_requirements: *70333793173940
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: database_cleaner
104
- requirement: &70134819585940 !ruby/object:Gem::Requirement
104
+ requirement: &70333793173400 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70134819585940
112
+ version_requirements: *70333793173400
113
113
  description: A simple, yet flexible solution to tracking changes made to objects in
114
114
  your database.
115
115
  email: