mongoid-serializer 0.0.1 → 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.
- checksums.yaml +4 -4
- data/README.md +16 -1
- data/lib/mongoid/serializer.rb +35 -0
- data/lib/mongoid/serializer/state_tracking.rb +3 -0
- data/lib/mongoid/serializer/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2657c7992af64ad72f4fe27e78596692d0a4d80
|
4
|
+
data.tar.gz: 30cd54dd1c9a6c1c06b6ebafa50b54fee8521567
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3208f6fe803c491399f8cb16f5b1be59aa60f49f36835ccd2bbeb0966713df06a2a7e854e0f009c65bfe08ce8d5fb4208828cf0f6f1c038465388385b6348ab4
|
7
|
+
data.tar.gz: 60eb85ebdd6caf5de102d823a9255d5cfd1ae46a4f93ab0a9f9a51cdcc403fe2d7a55c66d26a4dead11912767179e982569659662c0e7804d168bc81b2432bc3
|
data/README.md
CHANGED
@@ -21,14 +21,29 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
$ gem install mongoid-serializer
|
23
23
|
|
24
|
+
If the gem isn't activated automatically, you can add configuration in an initializer file, f.ex `mongoid_config.rb`
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# mongoid_config.rb
|
28
|
+
Mongoid::Serializer.configure!
|
29
|
+
```
|
30
|
+
|
24
31
|
## Usage
|
25
32
|
|
26
33
|
```ruby
|
27
|
-
class UserSerializer < MongoidSerializer
|
34
|
+
class UserSerializer < ActiveModel::MongoidSerializer
|
28
35
|
attributes :id, :first_name, :last_name, :quote, :age
|
29
36
|
end
|
30
37
|
```
|
31
38
|
|
39
|
+
Additionally this gem includes a `ActiveModel::StateTracking` module. This modules provides the methods `created?` and `deleted?` for use with any Mongoid Document.
|
40
|
+
|
41
|
+
You can either include this module in models of your choice, or include it into `Mongoid::Document` to make it part of every model.
|
42
|
+
|
43
|
+
For convenience, a method `Mongoid::Serializer.enable_state_tracking!` is made available to add state tracking to all Documents.
|
44
|
+
|
45
|
+
`ActiveModel::MongoidSerializer` is configured to try to hook into the `StateTracking` methods and set the JSON `status:` key to either `201` or `204`, to reflect created or deleted state back to the client.
|
46
|
+
|
32
47
|
## Contributing
|
33
48
|
|
34
49
|
1. Fork it
|
data/lib/mongoid/serializer.rb
CHANGED
@@ -6,6 +6,10 @@ module Mongoid
|
|
6
6
|
Mongoid::Document.send(:include, ActiveModel::SerializerSupport)
|
7
7
|
Mongoid::Criteria.delegate(:active_model_serializer, to: :to_a)
|
8
8
|
end
|
9
|
+
|
10
|
+
def self.enable_state_tracking!
|
11
|
+
require "mongoid/serializer/state_tracking"
|
12
|
+
end
|
9
13
|
end
|
10
14
|
end
|
11
15
|
|
@@ -18,5 +22,36 @@ module ActiveModel
|
|
18
22
|
def id
|
19
23
|
object._id
|
20
24
|
end
|
25
|
+
|
26
|
+
def status
|
27
|
+
return 204 if object.respond_to?(:destroyed?) && object.destroyed?
|
28
|
+
return 201 if object.respond_to?(:created?) && object.created?
|
29
|
+
end
|
21
30
|
end
|
22
31
|
end
|
32
|
+
|
33
|
+
module ActiveModel::StateTracking
|
34
|
+
extend ActiveSupport::Concern
|
35
|
+
|
36
|
+
included do
|
37
|
+
after_create do
|
38
|
+
@created = true
|
39
|
+
end
|
40
|
+
|
41
|
+
after_update do
|
42
|
+
@created = false
|
43
|
+
end
|
44
|
+
|
45
|
+
after_destroy do
|
46
|
+
@destroyed = true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def created?
|
51
|
+
@created == true
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroyed?
|
55
|
+
@destroyed == true
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kristian Mandrup
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
67
|
- lib/mongoid/serializer.rb
|
68
|
+
- lib/mongoid/serializer/state_tracking.rb
|
68
69
|
- lib/mongoid/serializer/version.rb
|
69
70
|
- mongoid-serializer.gemspec
|
70
71
|
homepage: https://github.com/kristianmandrup/mongoid-serializer
|