mongoid-serializer 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -8
- data/lib/mongoid/serializer.rb +9 -34
- data/lib/mongoid/serializer/version.rb +1 -1
- data/mongoid-serializer.gemspec +1 -0
- metadata +16 -3
- data/lib/mongoid/serializer/state_tracking.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17785512c923879da4e67fa785a4dae5aa88e131
|
4
|
+
data.tar.gz: c96bb37b19a0c70b068758fa7cdc820098abcd16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87fbbbdcdb6d35093e53ffaa6c5661a1140f84264e83052df5c457bf12d2ad981b03fe75a58d738d479aa335da4e93d97d581df76e073824c169acbb529947a4
|
7
|
+
data.tar.gz: fbd5b1c71e490aeb22f8deedcdfc9b577fdb5ef0bad7a486c6d541e8fdb24f0f772d6db39bebdb086d35420a4ac04df7a2c0bf1511cd00bb4f73be6326bf2500
|
data/README.md
CHANGED
@@ -21,18 +21,16 @@ 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
24
|
|
26
|
-
|
27
|
-
|
28
|
-
Mongoid::Serializer.configure
|
29
|
-
```
|
25
|
+
## Config
|
26
|
+
|
27
|
+
`Mongoid::Serializer.configure!`
|
30
28
|
|
31
29
|
## Usage
|
32
30
|
|
33
31
|
```ruby
|
34
32
|
class UserSerializer < ActiveModel::MongoidSerializer
|
35
|
-
attributes :id, :first_name, :last_name, :quote, :age
|
33
|
+
attributes :id, :first_name, :last_name, :quote, :age, http_status
|
36
34
|
end
|
37
35
|
```
|
38
36
|
|
@@ -40,9 +38,9 @@ Additionally this gem includes a `ActiveModel::StateTracking` module. This modul
|
|
40
38
|
|
41
39
|
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
40
|
|
43
|
-
|
41
|
+
`ActiveModel::StatusSerializer` is configured to try to hook into the `StateTracking` methods from [active_model-shaz](https://github.com/kristianmandrup/active_model-shaz) and set the JSON `http_status` key to either `201` or `204`, to reflect created or deleted state back to the client.
|
44
42
|
|
45
|
-
`ActiveModel::MongoidSerializer`
|
43
|
+
`ActiveModel::MongoidSerializer` extends `ActiveModel::StatusSerializer` and sets the `id` attribute to point to `_id` to better work with what the usual client side APIs expect (f.ex for use with ember-data).
|
46
44
|
|
47
45
|
## Contributing
|
48
46
|
|
data/lib/mongoid/serializer.rb
CHANGED
@@ -6,52 +6,27 @@ 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
|
13
9
|
end
|
14
10
|
end
|
15
11
|
|
16
|
-
if defined?(Mongoid)
|
17
|
-
Mongoid::Serializer.configure!
|
18
|
-
end
|
19
12
|
|
20
13
|
module ActiveModel
|
21
|
-
class
|
22
|
-
def
|
23
|
-
object._id
|
24
|
-
end
|
25
|
-
|
26
|
-
def status
|
14
|
+
class StatusSerializer < Serializer
|
15
|
+
def http_status
|
27
16
|
return 204 if object.respond_to?(:destroyed?) && object.destroyed?
|
28
17
|
return 201 if object.respond_to?(:created?) && object.created?
|
29
18
|
end
|
30
19
|
end
|
31
20
|
end
|
32
21
|
|
33
|
-
module ActiveModel
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
22
|
+
module ActiveModel
|
23
|
+
class MongoidSerializer < StatusSerializer
|
24
|
+
def id
|
25
|
+
object._id
|
47
26
|
end
|
48
27
|
end
|
28
|
+
end
|
49
29
|
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
def destroyed?
|
55
|
-
@destroyed == true
|
56
|
-
end
|
30
|
+
if defined?(Mongoid)
|
31
|
+
Mongoid::Serializer.configure!
|
57
32
|
end
|
data/mongoid-serializer.gemspec
CHANGED
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.4
|
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-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: active_model-shaz
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -65,7 +79,6 @@ files:
|
|
65
79
|
- README.md
|
66
80
|
- Rakefile
|
67
81
|
- lib/mongoid/serializer.rb
|
68
|
-
- lib/mongoid/serializer/state_tracking.rb
|
69
82
|
- lib/mongoid/serializer/version.rb
|
70
83
|
- mongoid-serializer.gemspec
|
71
84
|
homepage: https://github.com/kristianmandrup/mongoid-serializer
|