active_model_serializers_rails_2.3 0.9.0.pre1 → 0.9.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -3
- data/lib/active_model/array_serializer.rb +21 -5
- data/lib/active_model/default_serializer.rb +12 -3
- data/lib/active_model/serializable.rb +9 -5
- data/lib/active_model/serializer.rb +35 -23
- data/lib/active_model/serializer/associations.rb +39 -14
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
- data/lib/active_model/serializer/railtie.rb +1 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +18 -2
- data/test/integration/active_record/active_record_test.rb +5 -5
- data/test/unit/active_model/serializer/has_many_test.rb +13 -1
- data/test/unit/active_model/serializer/has_one_test.rb +17 -3
- data/test/unit/active_model/serializer/scope_test.rb +28 -0
- metadata +16 -11
- checksums.yaml +0 -7
- data/lib/active_model/as_json_overrides.rb +0 -6
data/README.md
CHANGED
@@ -10,9 +10,17 @@ This is a backport of the ActiveModel::Serializers gem to be compatible with
|
|
10
10
|
Rails 2.3. The following features have been removed:
|
11
11
|
|
12
12
|
* Controller `render` method support. This means you'll need to instantiate
|
13
|
-
the serializer yourself and pass it to `render`. E.g. `render :json => FooSerializer.new(foo)`
|
14
|
-
* `current_user` and such support. Might be baked in later.
|
13
|
+
the serializer yourself and pass it to `render`. E.g. `render :json => FooSerializer.new(foo, scope: user)`
|
15
14
|
* Generators are gone as well.
|
15
|
+
* Probably won't be supported for a long amount of time. Please do your best to upgrade
|
16
|
+
to rails 3.2+ as soon as possible. Feel free to open an issue whenever I run
|
17
|
+
too far behind upstream's master branch.
|
18
|
+
|
19
|
+
## Rails 2-3- 0.9.0
|
20
|
+
|
21
|
+
**Rails 2-3 is under development, there are some incompatible changes with the current stable release.**
|
22
|
+
|
23
|
+
If you want to read the stable documentation visit [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md)
|
16
24
|
|
17
25
|
## Purpose
|
18
26
|
|
@@ -33,7 +41,7 @@ The easiest way to install `ActiveModel::Serializers` is to add it to your
|
|
33
41
|
`Gemfile`:
|
34
42
|
|
35
43
|
```ruby
|
36
|
-
gem "active_model_serializers_rails2.3"
|
44
|
+
gem "active_model_serializers_rails2.3", git: "https://github.com/fivetanley/active_model_serializers", branch: 'rails-2.3'
|
37
45
|
```
|
38
46
|
|
39
47
|
Then, install it on the command line:
|
@@ -14,9 +14,7 @@ module ActiveModel
|
|
14
14
|
|
15
15
|
def initialize(object, options={})
|
16
16
|
@object = object
|
17
|
-
@root = options
|
18
|
-
@root = self.class._root if @root.nil?
|
19
|
-
@root = options[:resource_name] if @root.nil?
|
17
|
+
@root = options.fetch(:root, self.class._root)
|
20
18
|
@meta_key = options[:meta_key] || :meta
|
21
19
|
@meta = options[@meta_key]
|
22
20
|
@each_serializer = options[:each_serializer]
|
@@ -24,12 +22,30 @@ module ActiveModel
|
|
24
22
|
end
|
25
23
|
attr_accessor :object, :root, :meta_key, :meta
|
26
24
|
|
25
|
+
def json_key
|
26
|
+
if root.nil?
|
27
|
+
@options[:resource_name]
|
28
|
+
else
|
29
|
+
root
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def serializer_for(item)
|
34
|
+
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
35
|
+
serializer_class.new(item, @options)
|
36
|
+
end
|
37
|
+
|
27
38
|
def serializable_array
|
28
39
|
@object.map do |item|
|
29
|
-
|
30
|
-
serializer.new(item, @options).serializable_object
|
40
|
+
serializer_for(item).serializable_object
|
31
41
|
end
|
32
42
|
end
|
33
43
|
alias_method :serializable_object, :serializable_array
|
44
|
+
|
45
|
+
def embedded_in_root_associations
|
46
|
+
@object.each_with_object({}) do |item, hash|
|
47
|
+
hash.merge!(serializer_for(item).embedded_in_root_associations)
|
48
|
+
end
|
49
|
+
end
|
34
50
|
end
|
35
51
|
end
|
@@ -1,18 +1,27 @@
|
|
1
|
+
require 'active_model/serializable'
|
2
|
+
|
1
3
|
module ActiveModel
|
2
4
|
# DefaultSerializer
|
3
5
|
#
|
4
6
|
# Provides a constant interface for all items
|
5
7
|
class DefaultSerializer
|
8
|
+
include ActiveModel::Serializable
|
9
|
+
|
6
10
|
attr_reader :object
|
7
11
|
|
8
12
|
def initialize(object, options=nil)
|
9
13
|
@object = object
|
10
14
|
end
|
11
15
|
|
12
|
-
def
|
16
|
+
def as_json(options={})
|
13
17
|
return nil if @object.nil?
|
14
|
-
@object.
|
18
|
+
if @object.is_a?(Struct)
|
19
|
+
Hash[@object.members.zip(@object.values)]
|
20
|
+
else
|
21
|
+
@object.as_json
|
22
|
+
end
|
15
23
|
end
|
16
|
-
alias
|
24
|
+
alias serializable_hash as_json
|
25
|
+
alias serializable_object as_json
|
17
26
|
end
|
18
27
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ActiveModel
|
2
2
|
module Serializable
|
3
3
|
def as_json(options={})
|
4
|
-
if root = options
|
4
|
+
if root = options.fetch(:root, json_key)
|
5
5
|
hash = { root => serializable_object }
|
6
6
|
hash.merge!(serializable_data)
|
7
7
|
hash
|
@@ -11,11 +11,15 @@ module ActiveModel
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def serializable_data
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
embedded_in_root_associations.tap do |hash|
|
15
|
+
if respond_to?(:meta) && meta
|
16
|
+
hash[meta_key] = meta
|
17
|
+
end
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
def embedded_in_root_associations
|
22
|
+
{}
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
@@ -15,6 +15,7 @@ end
|
|
15
15
|
module ActiveModel
|
16
16
|
class Serializer
|
17
17
|
include Serializable
|
18
|
+
extend ActiveSupport::Inflector
|
18
19
|
|
19
20
|
@mutex = Mutex.new
|
20
21
|
|
@@ -23,6 +24,24 @@ module ActiveModel
|
|
23
24
|
base._attributes = []
|
24
25
|
base._associations = {}
|
25
26
|
end
|
27
|
+
def const_regexp(camel_cased_word) #:nodoc:
|
28
|
+
parts = camel_cased_word.split("::")
|
29
|
+
last = parts.pop
|
30
|
+
|
31
|
+
parts.reverse.inject(last) do |acc, part|
|
32
|
+
part.empty? ? acc : "#{part}(::#{acc})?"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
def safe_constantize(camel_cased_word)
|
36
|
+
begin
|
37
|
+
constantize(camel_cased_word)
|
38
|
+
rescue NameError => e
|
39
|
+
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
|
40
|
+
e.name.to_s == camel_cased_word.to_s
|
41
|
+
rescue ArgumentError => e
|
42
|
+
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
|
43
|
+
end
|
44
|
+
end
|
26
45
|
|
27
46
|
def setup
|
28
47
|
@mutex.synchronize do
|
@@ -61,7 +80,7 @@ end
|
|
61
80
|
if resource.respond_to?(:to_ary)
|
62
81
|
ArraySerializer
|
63
82
|
else
|
64
|
-
"#{resource.class.name}Serializer"
|
83
|
+
safe_constantize "#{resource.class.name}Serializer"
|
65
84
|
end
|
66
85
|
end
|
67
86
|
end
|
@@ -110,17 +129,18 @@ end
|
|
110
129
|
def initialize(object, options={})
|
111
130
|
@object = object
|
112
131
|
@scope = options[:scope]
|
113
|
-
|
132
|
+
@root = options.fetch(:root, self.class._root)
|
114
133
|
@meta_key = options[:meta_key] || :meta
|
115
134
|
@meta = options[@meta_key]
|
116
135
|
end
|
117
|
-
attr_accessor :object, :scope, :meta_key, :meta
|
118
|
-
attr_reader :root
|
136
|
+
attr_accessor :object, :scope, :meta_key, :meta, :root
|
119
137
|
|
120
|
-
def
|
121
|
-
|
122
|
-
|
123
|
-
|
138
|
+
def json_key
|
139
|
+
if root == true || root.nil?
|
140
|
+
self.class.root_name
|
141
|
+
else
|
142
|
+
root
|
143
|
+
end
|
124
144
|
end
|
125
145
|
|
126
146
|
def attributes
|
@@ -137,7 +157,8 @@ end
|
|
137
157
|
if association.embed_ids?
|
138
158
|
hash[association.key] = serialize_ids association
|
139
159
|
elsif association.embed_objects?
|
140
|
-
|
160
|
+
associated_data = send(association.name)
|
161
|
+
hash[association.embedded_key] = serialize(association, associated_data)
|
141
162
|
end
|
142
163
|
end
|
143
164
|
end
|
@@ -147,31 +168,21 @@ end
|
|
147
168
|
keys
|
148
169
|
end
|
149
170
|
|
150
|
-
def serializable_data
|
151
|
-
embedded_in_root_associations.merge!(super)
|
152
|
-
end
|
153
|
-
|
154
171
|
def embedded_in_root_associations
|
155
172
|
associations = self.class._associations
|
156
173
|
included_associations = filter(associations.keys)
|
157
174
|
associations.each_with_object({}) do |(name, association), hash|
|
158
175
|
if included_associations.include? name
|
159
176
|
if association.embed_in_root?
|
160
|
-
|
177
|
+
associated_data = Array(send(association.name))
|
178
|
+
hash[association.root_key] = serialize(association, associated_data)
|
161
179
|
end
|
162
180
|
end
|
163
181
|
end
|
164
182
|
end
|
165
183
|
|
166
|
-
def serialize(association)
|
167
|
-
|
168
|
-
if associated_data.respond_to?(:to_ary) &&
|
169
|
-
!(association.serializer_class &&
|
170
|
-
association.serializer_class <= ArraySerializer)
|
171
|
-
associated_data.map { |elem| association.build_serializer(elem).serializable_hash }
|
172
|
-
else
|
173
|
-
association.build_serializer(associated_data).serializable_object
|
174
|
-
end
|
184
|
+
def serialize(association, object)
|
185
|
+
association.build_serializer(object, scope: scope).serializable_object
|
175
186
|
end
|
176
187
|
|
177
188
|
def serialize_ids(association)
|
@@ -190,4 +201,5 @@ end
|
|
190
201
|
end
|
191
202
|
alias_method :serializable_object, :serializable_hash
|
192
203
|
end
|
204
|
+
|
193
205
|
end
|
@@ -19,40 +19,65 @@ module ActiveModel
|
|
19
19
|
@key = options[:key]
|
20
20
|
@embedded_key = options[:root] || name
|
21
21
|
|
22
|
-
|
22
|
+
serializer = @options[:serializer]
|
23
|
+
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
23
24
|
end
|
24
25
|
|
25
|
-
attr_reader :name, :embed_ids, :embed_objects
|
26
|
-
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :options
|
26
|
+
attr_reader :name, :embed_ids, :embed_objects
|
27
|
+
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_class, :options
|
27
28
|
alias embed_ids? embed_ids
|
28
29
|
alias embed_objects? embed_objects
|
29
30
|
alias embed_in_root? embed_in_root
|
30
31
|
|
31
|
-
def serializer_class=(serializer)
|
32
|
-
@serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer
|
33
|
-
end
|
34
|
-
|
35
32
|
def embed=(embed)
|
36
33
|
@embed_ids = embed == :id || embed == :ids
|
37
34
|
@embed_objects = embed == :object || embed == :objects
|
38
35
|
end
|
39
36
|
|
40
|
-
def build_serializer(object)
|
41
|
-
@serializer_class
|
42
|
-
|
37
|
+
def build_serializer(object, options = {})
|
38
|
+
@serializer_class.new(object, options.merge(@options))
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def use_array_serializer!
|
44
|
+
@options.merge!(each_serializer: @serializer_class)
|
45
|
+
@serializer_class = ArraySerializer
|
43
46
|
end
|
44
47
|
|
45
48
|
class HasOne < Association
|
46
|
-
def initialize(*args)
|
49
|
+
def initialize(name, *args)
|
50
|
+
super
|
51
|
+
@root_key = @embedded_key.to_s.pluralize
|
52
|
+
@key ||= "#{name}_id"
|
53
|
+
end
|
54
|
+
|
55
|
+
def build_serializer(object, options = {})
|
56
|
+
if object.respond_to?(:to_ary)
|
57
|
+
use_array_serializer!
|
58
|
+
else
|
59
|
+
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
60
|
+
end
|
61
|
+
|
47
62
|
super
|
48
|
-
@key ||= "#{name}_id"
|
49
63
|
end
|
50
64
|
end
|
51
65
|
|
52
66
|
class HasMany < Association
|
53
|
-
def initialize(*args)
|
67
|
+
def initialize(name, *args)
|
68
|
+
super
|
69
|
+
@root_key = @embedded_key
|
70
|
+
@key ||= "#{name.to_s.singularize}_ids"
|
71
|
+
end
|
72
|
+
|
73
|
+
def build_serializer(object, options = {})
|
74
|
+
if @serializer_class && !(@serializer_class <= ArraySerializer)
|
75
|
+
use_array_serializer!
|
76
|
+
else
|
77
|
+
@serializer_class ||= ArraySerializer
|
78
|
+
end
|
79
|
+
|
54
80
|
super
|
55
|
-
@key ||= "#{name.singularize}_ids"
|
56
81
|
end
|
57
82
|
end
|
58
83
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
module Generators
|
6
|
+
class ScaffoldControllerGenerator
|
7
|
+
if Rails::VERSION::MAJOR >= 4
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
hook_for :serializer, default: true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_file_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
7
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
|
8
|
+
|
9
|
+
# GET <%= route_url %>
|
10
|
+
# GET <%= route_url %>.json
|
11
|
+
def index
|
12
|
+
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
|
13
|
+
|
14
|
+
respond_to do |format|
|
15
|
+
format.html # index.html.erb
|
16
|
+
format.json { render json: <%= "@#{plural_table_name}" %> }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# GET <%= route_url %>/1
|
21
|
+
# GET <%= route_url %>/1.json
|
22
|
+
def show
|
23
|
+
respond_to do |format|
|
24
|
+
format.html # show.html.erb
|
25
|
+
format.json { render json: <%= "@#{singular_table_name}" %> }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# GET <%= route_url %>/new
|
30
|
+
def new
|
31
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
|
32
|
+
end
|
33
|
+
|
34
|
+
# GET <%= route_url %>/1/edit
|
35
|
+
def edit
|
36
|
+
end
|
37
|
+
|
38
|
+
# POST <%= route_url %>
|
39
|
+
# POST <%= route_url %>.json
|
40
|
+
def create
|
41
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
42
|
+
|
43
|
+
respond_to do |format|
|
44
|
+
if @<%= orm_instance.save %>
|
45
|
+
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> }
|
46
|
+
format.json { render json: <%= "@#{singular_table_name}" %>, status: :created }
|
47
|
+
else
|
48
|
+
format.html { render action: 'new' }
|
49
|
+
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# PATCH/PUT <%= route_url %>/1
|
55
|
+
# PATCH/PUT <%= route_url %>/1.json
|
56
|
+
def update
|
57
|
+
respond_to do |format|
|
58
|
+
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
59
|
+
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
|
60
|
+
format.json { head :no_content }
|
61
|
+
else
|
62
|
+
format.html { render action: 'edit' }
|
63
|
+
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# DELETE <%= route_url %>/1
|
69
|
+
# DELETE <%= route_url %>/1.json
|
70
|
+
def destroy
|
71
|
+
@<%= orm_instance.destroy %>
|
72
|
+
respond_to do |format|
|
73
|
+
format.html { redirect_to <%= index_helper %>_url }
|
74
|
+
format.json { head :no_content }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
# Use callbacks to share common setup or constraints between actions.
|
80
|
+
def set_<%= singular_table_name %>
|
81
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
82
|
+
end
|
83
|
+
|
84
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
85
|
+
def <%= "#{singular_table_name}_params" %>
|
86
|
+
<%- if attributes_names.empty? -%>
|
87
|
+
params[<%= ":#{singular_table_name}" %>]
|
88
|
+
<%- else -%>
|
89
|
+
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
90
|
+
<%- end -%>
|
91
|
+
end
|
92
|
+
end
|
93
|
+
<% end -%>
|
@@ -3,6 +3,7 @@ module ActiveModel
|
|
3
3
|
initializer 'generators' do |app|
|
4
4
|
require 'rails/generators'
|
5
5
|
require 'active_model/serializer/generators/serializer/serializer_generator'
|
6
|
+
require 'active_model/serializer/generators/serializer/scaffold_controller_generator'
|
6
7
|
Rails::Generators.configure!(app.config.generators)
|
7
8
|
require 'active_model/serializer/generators/resource_override'
|
8
9
|
end
|
@@ -1,10 +1,26 @@
|
|
1
1
|
require 'active_support'
|
2
|
-
require '
|
2
|
+
require 'active_record'
|
3
3
|
require 'active_model/serializer'
|
4
4
|
require 'active_model/serializer_support'
|
5
5
|
require 'active_model/serializer/version'
|
6
|
-
require 'active_model/serializer/railtie' if defined?(Rails)
|
6
|
+
require 'active_model/serializer/railtie' if defined?(Rails) && defined?(Rails::Railtie)
|
7
7
|
|
8
|
+
ActiveModel::Errors = ActiveRecord::Errors #fix for authlogic gem
|
9
|
+
|
10
|
+
module ActiveSupport
|
11
|
+
module Inflector
|
12
|
+
def safe_constantize(camel_cased_word)
|
13
|
+
begin
|
14
|
+
constantize(camel_cased_word)
|
15
|
+
rescue NameError => e
|
16
|
+
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
|
17
|
+
e.name.to_s == camel_cased_word.to_s
|
18
|
+
rescue ArgumentError => e
|
19
|
+
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
8
24
|
begin
|
9
25
|
require 'action_controller'
|
10
26
|
require 'action_controller/serialization'
|
@@ -51,24 +51,24 @@ module ActiveModel
|
|
51
51
|
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'short' }, { name: 'whiny' }] },
|
52
52
|
{ body: 'i liked it', ar_tags: [{:name=>"short"}, {:name=>"happy"}] }],
|
53
53
|
ar_tags: [{ name: 'short' }, { name: 'whiny' }, { name: 'happy' }],
|
54
|
-
|
54
|
+
'ar_sections' => [{ 'name' => 'ruby' }]
|
55
55
|
}, post_serializer.as_json)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
private
|
60
60
|
|
61
|
-
def embed(
|
62
|
-
old_assocs = Hash[
|
61
|
+
def embed(serializer_class, options = {})
|
62
|
+
old_assocs = Hash[serializer_class._associations.to_a.map { |(name, association)| [name, association.dup] }]
|
63
63
|
|
64
|
-
|
64
|
+
serializer_class._associations.each_value do |association|
|
65
65
|
association.embed = options[:embed]
|
66
66
|
association.embed_in_root = options[:embed_in_root]
|
67
67
|
end
|
68
68
|
|
69
69
|
yield
|
70
70
|
ensure
|
71
|
-
|
71
|
+
serializer_class._associations = old_assocs
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
@@ -131,7 +131,7 @@ module ActiveModel
|
|
131
131
|
}, @post_serializer.as_json)
|
132
132
|
end
|
133
133
|
|
134
|
-
def
|
134
|
+
def test_associations_embedding_ids_using_a_given_array_serializer
|
135
135
|
@association.embed = :ids
|
136
136
|
@association.embed_in_root = true
|
137
137
|
@association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
|
@@ -145,6 +145,18 @@ module ActiveModel
|
|
145
145
|
comments: { my_content: ['fake'] }
|
146
146
|
}, @post_serializer.as_json)
|
147
147
|
end
|
148
|
+
|
149
|
+
def test_associations_embedding_objects_using_a_given_array_serializer
|
150
|
+
@association.serializer_class = Class.new(ActiveModel::ArraySerializer) do
|
151
|
+
def serializable_object
|
152
|
+
{ my_content: ['fake'] }
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
assert_equal({
|
157
|
+
'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
|
158
|
+
}, @post_serializer.as_json)
|
159
|
+
end
|
148
160
|
end
|
149
161
|
end
|
150
162
|
end
|
@@ -112,11 +112,11 @@ module ActiveModel
|
|
112
112
|
|
113
113
|
assert_equal({
|
114
114
|
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
115
|
-
|
115
|
+
'profiles' => [{ name: 'N1', description: 'D1' }]
|
116
116
|
}, @user_serializer.as_json)
|
117
117
|
end
|
118
118
|
|
119
|
-
def
|
119
|
+
def test_associations_embedding_ids_using_a_given_serializer
|
120
120
|
@association.embed = :ids
|
121
121
|
@association.embed_in_root = true
|
122
122
|
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
@@ -129,7 +129,21 @@ module ActiveModel
|
|
129
129
|
|
130
130
|
assert_equal({
|
131
131
|
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
132
|
-
|
132
|
+
'profiles' => [{ name: 'fake' }]
|
133
|
+
}, @user_serializer.as_json)
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_associations_embedding_objects_using_a_given_serializer
|
137
|
+
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
138
|
+
def name
|
139
|
+
'fake'
|
140
|
+
end
|
141
|
+
|
142
|
+
attributes :name
|
143
|
+
end
|
144
|
+
|
145
|
+
assert_equal({
|
146
|
+
'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'fake' } }
|
133
147
|
}, @user_serializer.as_json)
|
134
148
|
end
|
135
149
|
end
|
@@ -17,5 +17,33 @@ module ActiveModel
|
|
17
17
|
'user'
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
class NestedScopeTest < ActiveSupport::TestCase
|
22
|
+
def setup
|
23
|
+
@association = UserSerializer._associations[:profile]
|
24
|
+
@old_association = @association.dup
|
25
|
+
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
26
|
+
@user_serializer = UserSerializer.new(@user, scope: 'user')
|
27
|
+
end
|
28
|
+
|
29
|
+
def teardown
|
30
|
+
UserSerializer._associations[:profile] = @old_association
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_scope_passed_through
|
34
|
+
@association.serializer_class = Class.new(ActiveModel::Serializer) do
|
35
|
+
def name
|
36
|
+
scope
|
37
|
+
end
|
38
|
+
|
39
|
+
attributes :name
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal({
|
43
|
+
name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' }
|
44
|
+
}, @user_serializer.serializable_hash)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
20
48
|
end
|
21
49
|
end
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_serializers_rails_2.3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.0.
|
4
|
+
version: 0.9.0.pre2
|
5
|
+
prerelease: 6
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- José Valim
|
@@ -10,11 +11,12 @@ authors:
|
|
10
11
|
autorequire:
|
11
12
|
bindir: bin
|
12
13
|
cert_chain: []
|
13
|
-
date: 2013-11-
|
14
|
+
date: 2013-11-25 00:00:00.000000000 Z
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: rails
|
17
18
|
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
18
20
|
requirements:
|
19
21
|
- - ~>
|
20
22
|
- !ruby/object:Gem::Version
|
@@ -22,6 +24,7 @@ dependencies:
|
|
22
24
|
type: :development
|
23
25
|
prerelease: false
|
24
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
25
28
|
requirements:
|
26
29
|
- - ~>
|
27
30
|
- !ruby/object:Gem::Version
|
@@ -41,11 +44,12 @@ files:
|
|
41
44
|
- DESIGN.textile
|
42
45
|
- MIT-LICENSE
|
43
46
|
- lib/active_model/array_serializer.rb
|
44
|
-
- lib/active_model/as_json_overrides.rb
|
45
47
|
- lib/active_model/default_serializer.rb
|
46
48
|
- lib/active_model/serializable.rb
|
47
49
|
- lib/active_model/serializer/associations.rb
|
48
50
|
- lib/active_model/serializer/config.rb
|
51
|
+
- lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb
|
52
|
+
- lib/active_model/serializer/generators/serializer/templates/controller.rb
|
49
53
|
- lib/active_model/serializer/railtie.rb
|
50
54
|
- lib/active_model/serializer/version.rb
|
51
55
|
- lib/active_model/serializer.rb
|
@@ -70,29 +74,29 @@ files:
|
|
70
74
|
- test/unit/active_model/serializer/meta_test.rb
|
71
75
|
- test/unit/active_model/serializer/root_test.rb
|
72
76
|
- test/unit/active_model/serializer/scope_test.rb
|
73
|
-
homepage: https://github.com/
|
74
|
-
licenses:
|
75
|
-
- MIT
|
76
|
-
metadata: {}
|
77
|
+
homepage: https://github.com/rails-api/active_model_serializers
|
78
|
+
licenses: []
|
77
79
|
post_install_message:
|
78
80
|
rdoc_options: []
|
79
81
|
require_paths:
|
80
82
|
- lib
|
81
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
82
85
|
requirements:
|
83
|
-
- - '>='
|
86
|
+
- - ! '>='
|
84
87
|
- !ruby/object:Gem::Version
|
85
88
|
version: 1.9.3
|
86
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
87
91
|
requirements:
|
88
|
-
- - '>'
|
92
|
+
- - ! '>'
|
89
93
|
- !ruby/object:Gem::Version
|
90
94
|
version: 1.3.1
|
91
95
|
requirements: []
|
92
96
|
rubyforge_project:
|
93
|
-
rubygems_version:
|
97
|
+
rubygems_version: 1.8.23
|
94
98
|
signing_key:
|
95
|
-
specification_version:
|
99
|
+
specification_version: 3
|
96
100
|
summary: Bringing consistency and object orientation to model serialization. Works
|
97
101
|
great for client-side MVC frameworks!
|
98
102
|
test_files:
|
@@ -115,3 +119,4 @@ test_files:
|
|
115
119
|
- test/unit/active_model/serializer/meta_test.rb
|
116
120
|
- test/unit/active_model/serializer/root_test.rb
|
117
121
|
- test/unit/active_model/serializer/scope_test.rb
|
122
|
+
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 0bf1627e3e77f67d91a472d7973d66c3a8ca726b
|
4
|
-
data.tar.gz: 905a98aa09e0e84a9ae4b0931f55d39fe1e0b8cd
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 3984baa7ec037bd7e4faf376127623bafac0b8de7fbb08f48e1effab5240968bce85a217104be9c55ad84204f170cc27860aa53b627a7a9eb8d034222a0b9048
|
7
|
-
data.tar.gz: c4607f4816f99fd7d913a881937d0a6814482376e404877bd71bcc1db494b395058a70cb7a177f0fb92774ec56fef586db293cb830cebd9060c518f8dbb5b35c
|