rocket_pants 1.5.5 → 1.6.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rocket_pants/controller/respondable.rb +30 -1
- metadata +3 -3
@@ -2,6 +2,19 @@ module RocketPants
|
|
2
2
|
module Respondable
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
SerializerWrapper = Struct.new(:serializer, :object) do
|
6
|
+
|
7
|
+
def serializable_hash(options = {})
|
8
|
+
instance = serializer.new(object, options)
|
9
|
+
if instance.respond_to?(:serializable_hash)
|
10
|
+
instance.serializable_hash
|
11
|
+
else
|
12
|
+
instance.as_json options
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
5
18
|
def self.pagination_type(object)
|
6
19
|
if object.respond_to?(:total_entries)
|
7
20
|
:will_paginate
|
@@ -41,6 +54,8 @@ module RocketPants
|
|
41
54
|
end
|
42
55
|
|
43
56
|
def self.normalise_object(object, options = {})
|
57
|
+
# First, prepare the object for serialization.
|
58
|
+
object = normalise_to_serializer object, options
|
44
59
|
# Convert the object using a standard grape-like lookup chain.
|
45
60
|
if object.is_a?(Array) || object.is_a?(Set)
|
46
61
|
object.map { |o| normalise_object o, options }
|
@@ -53,12 +68,26 @@ module RocketPants
|
|
53
68
|
end
|
54
69
|
end
|
55
70
|
|
71
|
+
def self.normalise_to_serializer(object, options)
|
72
|
+
serializer = options.delete(:serializer)
|
73
|
+
serializer = object.active_model_serializer if object.respond_to?(:active_model_serializer) && serializer.nil?
|
74
|
+
return object unless serializer
|
75
|
+
SerializerWrapper.new serializer, object
|
76
|
+
end
|
77
|
+
|
56
78
|
RENDERING_OPTIONS = [:status, :content_type]
|
57
79
|
|
58
80
|
private
|
59
81
|
|
60
82
|
def normalise_object(object, options = {})
|
61
|
-
Respondable.normalise_object object, options.except(*RENDERING_OPTIONS)
|
83
|
+
Respondable.normalise_object object, options.except(*RENDERING_OPTIONS).reverse_merge(default_serializer_options)
|
84
|
+
end
|
85
|
+
|
86
|
+
def default_serializer_options
|
87
|
+
{
|
88
|
+
:url_options => url_options,
|
89
|
+
:root => false
|
90
|
+
}
|
62
91
|
end
|
63
92
|
|
64
93
|
# Given a json object or encoded json, will encode it
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocket_pants
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.6.0.rc1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Darcy Laycock
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|