cheap_ams 0.10.5 → 0.10.6
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/.rubocop.yml +49 -0
- data/.rubocop_todo.yml +315 -0
- data/.simplecov +99 -0
- data/.travis.yml +8 -0
- data/Gemfile +10 -1
- data/README.md +4 -16
- data/Rakefile +29 -2
- data/cheap_ams.gemspec +3 -2
- data/docs/README.md +1 -0
- data/docs/general/configuration_options.md +11 -0
- data/lib/action_controller/serialization.rb +2 -2
- data/lib/active_model/serializable_resource.rb +0 -2
- data/lib/active_model/serializer/adapter/fragment_cache.rb +3 -4
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -2
- data/lib/active_model/serializer/adapter/json.rb +0 -1
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +2 -4
- data/lib/active_model/serializer/adapter/json_api.rb +87 -93
- data/lib/active_model/serializer/adapter.rb +4 -4
- data/lib/active_model/serializer/array_serializer.rb +11 -12
- data/lib/active_model/serializer/association.rb +0 -1
- data/lib/active_model/serializer/fieldset.rb +2 -4
- data/lib/active_model/serializer/lint.rb +2 -4
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +0 -25
- data/lib/active_model_serializers.rb +0 -1
- data/lib/generators/serializer/serializer_generator.rb +7 -7
- data/lib/generators/serializer/templates/{serializer.rb → serializer.rb.erb} +0 -0
- data/lib/tasks/rubocop.rake +0 -0
- data/test/action_controller/adapter_selector_test.rb +3 -3
- data/test/action_controller/explicit_serializer_test.rb +9 -9
- data/test/action_controller/json_api/linked_test.rb +28 -28
- data/test/action_controller/json_api/pagination_test.rb +24 -24
- data/test/action_controller/serialization_test.rb +73 -79
- data/test/adapter/fragment_cache_test.rb +2 -2
- data/test/adapter/json/belongs_to_test.rb +4 -4
- data/test/adapter/json/collection_test.rb +15 -15
- data/test/adapter/json/has_many_test.rb +5 -5
- data/test/adapter/json_api/belongs_to_test.rb +37 -37
- data/test/adapter/json_api/collection_test.rb +22 -23
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +4 -4
- data/test/adapter/json_api/has_many_test.rb +25 -25
- data/test/adapter/json_api/has_one_test.rb +10 -10
- data/test/adapter/json_api/json_api_test.rb +6 -7
- data/test/adapter/json_api/linked_test.rb +69 -69
- data/test/adapter/json_api/pagination_links_test.rb +8 -8
- data/test/adapter/json_api/resource_type_config_test.rb +14 -14
- data/test/adapter/json_test.rb +7 -7
- data/test/adapter_test.rb +2 -2
- data/test/array_serializer_test.rb +11 -7
- data/test/capture_warnings.rb +8 -7
- data/test/fixtures/active_record.rb +0 -1
- data/test/fixtures/poro.rb +7 -12
- data/test/generators/scaffold_controller_generator_test.rb +1 -2
- data/test/generators/serializer_generator_test.rb +10 -10
- data/test/lint_test.rb +0 -7
- data/test/serializers/adapter_for_test.rb +0 -1
- data/test/serializers/associations_test.rb +4 -4
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/attributes_test.rb +8 -14
- data/test/serializers/cache_test.rb +8 -7
- data/test/serializers/fieldset_test.rb +3 -4
- data/test/serializers/meta_test.rb +23 -23
- data/test/serializers/root_test.rb +1 -3
- data/test/serializers/serializer_for_test.rb +1 -1
- data/test/support/serialization_testing.rb +13 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +2 -2
- data/test/test_helper.rb +14 -2
- metadata +13 -6
- data/test/serializers/urls_test.rb +0 -26
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'set'
|
2
2
|
module ActiveModel
|
3
3
|
class SerializableResource
|
4
|
-
|
5
4
|
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])
|
6
5
|
|
7
6
|
def initialize(resource, options = {})
|
@@ -79,6 +78,5 @@ module ActiveModel
|
|
79
78
|
ActiveModelSerializers.silence_warnings do
|
80
79
|
attr_reader :resource, :adapter_opts, :serializer_opts
|
81
80
|
end
|
82
|
-
|
83
81
|
end
|
84
82
|
end
|
@@ -2,7 +2,6 @@ module ActiveModel
|
|
2
2
|
class Serializer
|
3
3
|
class Adapter
|
4
4
|
class FragmentCache
|
5
|
-
|
6
5
|
attr_reader :serializer
|
7
6
|
|
8
7
|
def initialize(adapter, serializer, options)
|
@@ -35,7 +34,7 @@ module ActiveModel
|
|
35
34
|
|
36
35
|
def cached_attributes(klass, serializers)
|
37
36
|
attributes = serializer.class._attributes
|
38
|
-
cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject {|attr| klass._cache_except.include?(attr) }
|
37
|
+
cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject { |attr| klass._cache_except.include?(attr) }
|
39
38
|
non_cached_attributes = attributes - cached_attributes
|
40
39
|
|
41
40
|
cached_attributes.each do |attribute|
|
@@ -60,7 +59,7 @@ module ActiveModel
|
|
60
59
|
Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached)
|
61
60
|
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached)
|
62
61
|
|
63
|
-
klass._cache_options
|
62
|
+
klass._cache_options ||= {}
|
64
63
|
klass._cache_options[:key] = klass._cache_key if klass._cache_key
|
65
64
|
|
66
65
|
cached.constantize.cache(klass._cache_options)
|
@@ -68,7 +67,7 @@ module ActiveModel
|
|
68
67
|
cached.constantize.fragmented(serializer)
|
69
68
|
non_cached.constantize.fragmented(serializer)
|
70
69
|
|
71
|
-
serializers = {cached: cached, non_cached: non_cached}
|
70
|
+
serializers = { cached: cached, non_cached: non_cached }
|
72
71
|
cached_attributes(klass, serializers)
|
73
72
|
serializers
|
74
73
|
end
|
@@ -4,19 +4,17 @@ module ActiveModel
|
|
4
4
|
class Adapter
|
5
5
|
class JsonApi < Adapter
|
6
6
|
class FragmentCache
|
7
|
-
|
8
7
|
def fragment_cache(root, cached_hash, non_cached_hash)
|
9
8
|
hash = {}
|
10
9
|
core_cached = cached_hash.first
|
11
10
|
core_non_cached = non_cached_hash.first
|
12
|
-
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] }
|
13
|
-
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] }
|
11
|
+
no_root_cache = cached_hash.delete_if { |key, value| key == core_cached[0] }
|
12
|
+
no_root_non_cache = non_cached_hash.delete_if { |key, value| key == core_non_cached[0] }
|
14
13
|
cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
|
15
14
|
hash = (root) ? { root => cached_resource } : cached_resource
|
16
15
|
|
17
16
|
hash.deep_merge no_root_non_cache.deep_merge no_root_cache
|
18
17
|
end
|
19
|
-
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
@@ -9,7 +9,13 @@ module ActiveModel
|
|
9
9
|
super
|
10
10
|
@hash = { data: [] }
|
11
11
|
|
12
|
-
|
12
|
+
@options[:include] ||= []
|
13
|
+
if @options[:include].is_a?(String)
|
14
|
+
@options[:include] = @options[:include].split(',')
|
15
|
+
end
|
16
|
+
|
17
|
+
fields = options.delete(:fields)
|
18
|
+
if fields
|
13
19
|
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
14
20
|
else
|
15
21
|
@fieldset = options[:fieldset]
|
@@ -31,151 +37,139 @@ module ActiveModel
|
|
31
37
|
|
32
38
|
add_links(options)
|
33
39
|
else
|
34
|
-
|
35
|
-
|
40
|
+
primary_data = primary_data_for(serializer, options)
|
41
|
+
relationships = relationships_for(serializer)
|
42
|
+
included = included_for(serializer)
|
43
|
+
@hash[:data] = primary_data
|
44
|
+
@hash[:data][:relationships] = relationships if relationships.any?
|
45
|
+
@hash[:included] = included if included.any?
|
36
46
|
end
|
37
47
|
@hash
|
38
48
|
end
|
39
49
|
|
40
50
|
def fragment_cache(cached_hash, non_cached_hash)
|
41
51
|
root = false if @options.include?(:include)
|
42
|
-
JsonApi::FragmentCache.new
|
52
|
+
JsonApi::FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
|
43
53
|
end
|
44
54
|
|
45
55
|
private
|
46
56
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
57
|
+
def resource_identifier_type_for(serializer)
|
58
|
+
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
|
59
|
+
serializer.object.class.model_name.singular
|
60
|
+
else
|
61
|
+
serializer.object.class.model_name.plural
|
62
|
+
end
|
51
63
|
end
|
52
64
|
|
53
|
-
def
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s }
|
65
|
+
def resource_identifier_id_for(serializer)
|
66
|
+
if serializer.respond_to?(:id)
|
67
|
+
serializer.id
|
68
|
+
else
|
69
|
+
serializer.object.id
|
59
70
|
end
|
60
71
|
end
|
61
72
|
|
62
|
-
def
|
63
|
-
|
64
|
-
|
65
|
-
serializers = Array(serializers)
|
66
|
-
end
|
67
|
-
resource_path = [parent, resource_name].compact.join('.')
|
68
|
-
if include_assoc?(resource_path)
|
69
|
-
@hash[:included] ||= []
|
73
|
+
def resource_identifier_for(serializer)
|
74
|
+
type = resource_identifier_type_for(serializer)
|
75
|
+
id = resource_identifier_id_for(serializer)
|
70
76
|
|
71
|
-
|
72
|
-
|
77
|
+
{ id: id.to_s, type: type }
|
78
|
+
end
|
73
79
|
|
74
|
-
|
80
|
+
def resource_object_for(serializer, options = {})
|
81
|
+
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
|
75
82
|
|
76
|
-
|
77
|
-
|
83
|
+
cache_check(serializer) do
|
84
|
+
result = resource_identifier_for(serializer)
|
85
|
+
attributes = serializer.attributes(options).except(:id)
|
86
|
+
result[:attributes] = attributes if attributes.any?
|
87
|
+
result
|
78
88
|
end
|
89
|
+
end
|
79
90
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end if include_nested_assoc? resource_path
|
91
|
+
def primary_data_for(serializer, options)
|
92
|
+
if serializer.respond_to?(:each)
|
93
|
+
serializer.map { |s| resource_object_for(s, options) }
|
94
|
+
else
|
95
|
+
resource_object_for(serializer, options)
|
86
96
|
end
|
87
97
|
end
|
88
98
|
|
89
|
-
def
|
99
|
+
def relationship_value_for(serializer, options = {})
|
90
100
|
if serializer.respond_to?(:each)
|
91
|
-
|
92
|
-
serializer.each do |object|
|
93
|
-
result << resource_object_for(object, options)
|
94
|
-
end
|
101
|
+
serializer.map { |s| resource_identifier_for(s) }
|
95
102
|
else
|
96
|
-
|
103
|
+
if options[:virtual_value]
|
104
|
+
options[:virtual_value]
|
105
|
+
elsif serializer && serializer.object
|
106
|
+
resource_identifier_for(serializer)
|
107
|
+
end
|
97
108
|
end
|
98
|
-
result
|
99
109
|
end
|
100
110
|
|
101
|
-
def
|
102
|
-
|
103
|
-
|
111
|
+
def relationships_for(serializer)
|
112
|
+
Hash[serializer.associations.map { |association| [association.key, { data: relationship_value_for(association.serializer, association.options) }] }]
|
113
|
+
end
|
104
114
|
|
105
|
-
|
106
|
-
|
115
|
+
def included_for(serializer)
|
116
|
+
serializer.associations.flat_map { |assoc| _included_for(assoc.key, assoc.serializer) }.uniq
|
117
|
+
end
|
107
118
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
119
|
+
def _included_for(resource_name, serializer, parent = nil)
|
120
|
+
if serializer.respond_to?(:each)
|
121
|
+
serializer.flat_map { |s| _included_for(resource_name, s, parent) }.uniq
|
122
|
+
else
|
123
|
+
return [] unless serializer && serializer.object
|
124
|
+
result = []
|
125
|
+
resource_path = [parent, resource_name].compact.join('.')
|
112
126
|
|
113
|
-
|
127
|
+
if include_assoc?(resource_path)
|
128
|
+
primary_data = primary_data_for(serializer, @options)
|
129
|
+
relationships = relationships_for(serializer)
|
130
|
+
primary_data[:relationships] = relationships if relationships.any?
|
131
|
+
result.push(primary_data)
|
132
|
+
end
|
133
|
+
|
134
|
+
if include_nested_assoc?(resource_path)
|
135
|
+
non_empty_associations = serializer.associations.select(&:serializer)
|
136
|
+
|
137
|
+
non_empty_associations.each do |association|
|
138
|
+
result.concat(_included_for(association.key, association.serializer, resource_path))
|
139
|
+
result.uniq!
|
140
|
+
end
|
141
|
+
end
|
114
142
|
result
|
115
143
|
end
|
116
144
|
end
|
117
145
|
|
118
146
|
def include_assoc?(assoc)
|
119
|
-
return false unless @options[:include]
|
120
147
|
check_assoc("#{assoc}$")
|
121
148
|
end
|
122
149
|
|
123
150
|
def include_nested_assoc?(assoc)
|
124
|
-
return false unless @options[:include]
|
125
151
|
check_assoc("#{assoc}.")
|
126
152
|
end
|
127
153
|
|
128
154
|
def check_assoc(assoc)
|
129
|
-
|
130
|
-
include_opt = include_opt.split(',') if include_opt.is_a?(String)
|
131
|
-
include_opt.any? do |s|
|
132
|
-
s.match(/^#{assoc.gsub('.', '\.')}/)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def add_resource_relationships(attrs, serializer, options = {})
|
137
|
-
options[:add_included] = options.fetch(:add_included, true)
|
138
|
-
|
139
|
-
serializer.associations.each do |association|
|
140
|
-
key = association.key
|
141
|
-
serializer = association.serializer
|
142
|
-
opts = association.options
|
143
|
-
|
144
|
-
attrs[:relationships] ||= {}
|
145
|
-
|
146
|
-
if serializer.respond_to?(:each)
|
147
|
-
add_relationships(attrs, key, serializer)
|
148
|
-
else
|
149
|
-
if opts[:virtual_value]
|
150
|
-
add_relationship(attrs, key, nil, opts[:virtual_value])
|
151
|
-
else
|
152
|
-
add_relationship(attrs, key, serializer)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
if options[:add_included]
|
157
|
-
Array(serializer).each do |s|
|
158
|
-
add_included(key, s)
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
155
|
+
@options[:include].any? { |s| s.match(/^#{assoc.gsub('.', '\.')}/) }
|
162
156
|
end
|
163
157
|
|
164
158
|
def add_links(options)
|
165
159
|
links = @hash.fetch(:meta) { {} }
|
166
|
-
|
167
|
-
@hash[:meta] = add_pagination_links(links,
|
160
|
+
collection = serializer.object
|
161
|
+
@hash[:meta] = add_pagination_links(links, collection, options) if is_paginated?(resources)
|
168
162
|
end
|
169
163
|
|
170
|
-
def add_pagination_links(links,
|
171
|
-
pagination_links = JsonApi::PaginationLinks.new(
|
164
|
+
def add_pagination_links(links, collection, options)
|
165
|
+
pagination_links = JsonApi::PaginationLinks.new(collection, options[:context]).serializable_hash(options)
|
172
166
|
links.update(pagination_links)
|
173
167
|
end
|
174
168
|
|
175
|
-
def
|
176
|
-
|
177
|
-
|
178
|
-
|
169
|
+
def paginated?(collection)
|
170
|
+
collection.respond_to?(:current_page) &&
|
171
|
+
collection.respond_to?(:total_pages) &&
|
172
|
+
collection.respond_to?(:size)
|
179
173
|
end
|
180
174
|
end
|
181
175
|
end
|
@@ -15,7 +15,7 @@ module ActiveModel
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.adapter_class(adapter)
|
18
|
-
adapter_name = adapter.to_s.classify.sub(
|
18
|
+
adapter_name = adapter.to_s.classify.sub('API', 'Api')
|
19
19
|
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
|
20
20
|
end
|
21
21
|
|
@@ -68,12 +68,12 @@ module ActiveModel
|
|
68
68
|
parts = []
|
69
69
|
parts << object_cache_key
|
70
70
|
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
|
71
|
-
parts.join(
|
71
|
+
parts.join('/')
|
72
72
|
end
|
73
73
|
|
74
74
|
def object_cache_key
|
75
75
|
object_time_safe = @cached_serializer.object.updated_at
|
76
|
-
object_time_safe = object_time_safe.strftime(
|
76
|
+
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
77
77
|
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
|
78
78
|
end
|
79
79
|
|
@@ -82,7 +82,7 @@ module ActiveModel
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def meta_key
|
85
|
-
serializer.meta_key ||
|
85
|
+
serializer.meta_key || 'meta'
|
86
86
|
end
|
87
87
|
|
88
88
|
def root
|
@@ -3,23 +3,22 @@ module ActiveModel
|
|
3
3
|
class ArraySerializer
|
4
4
|
NoSerializerError = Class.new(StandardError)
|
5
5
|
include Enumerable
|
6
|
-
delegate :each, to: :@
|
6
|
+
delegate :each, to: :@serializers
|
7
7
|
|
8
|
-
attr_reader :root, :meta, :meta_key
|
8
|
+
attr_reader :object, :root, :meta, :meta_key
|
9
9
|
|
10
|
-
def initialize(
|
10
|
+
def initialize(resources, options = {})
|
11
11
|
@root = options[:root]
|
12
|
-
@
|
13
|
-
@
|
14
|
-
serializer_class = options.fetch(
|
15
|
-
|
16
|
-
|
17
|
-
)
|
12
|
+
@object = resources
|
13
|
+
@serializers = resources.map do |resource|
|
14
|
+
serializer_class = options.fetch(:serializer) {
|
15
|
+
ActiveModel::Serializer.serializer_for(resource)
|
16
|
+
}
|
18
17
|
|
19
18
|
if serializer_class.nil?
|
20
|
-
fail NoSerializerError, "No serializer found for
|
19
|
+
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
|
21
20
|
else
|
22
|
-
serializer_class.new(
|
21
|
+
serializer_class.new(resource, options.except(:serializer))
|
23
22
|
end
|
24
23
|
end
|
25
24
|
@meta = options[:meta]
|
@@ -27,7 +26,7 @@ module ActiveModel
|
|
27
26
|
end
|
28
27
|
|
29
28
|
def json_key
|
30
|
-
key = root || @
|
29
|
+
key = root || @serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
31
30
|
key.try(:pluralize)
|
32
31
|
end
|
33
32
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module ActiveModel
|
2
2
|
class Serializer
|
3
3
|
class Fieldset
|
4
|
-
|
5
4
|
def initialize(fields, root = nil)
|
6
5
|
@root = root
|
7
6
|
@raw_fields = fields
|
@@ -16,7 +15,7 @@ module ActiveModel
|
|
16
15
|
fields[key.to_sym] || fields[key.pluralize.to_sym]
|
17
16
|
end
|
18
17
|
|
19
|
-
|
18
|
+
private
|
20
19
|
|
21
20
|
ActiveModelSerializers.silence_warnings do
|
22
21
|
attr_reader :raw_fields, :root
|
@@ -24,7 +23,7 @@ module ActiveModel
|
|
24
23
|
|
25
24
|
def parsed_fields
|
26
25
|
if raw_fields.is_a?(Hash)
|
27
|
-
raw_fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h}
|
26
|
+
raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h }
|
28
27
|
elsif raw_fields.is_a?(Array)
|
29
28
|
if root.nil?
|
30
29
|
raise ArgumentError, 'The root argument must be specified if the fileds argument is an array.'
|
@@ -36,7 +35,6 @@ module ActiveModel
|
|
36
35
|
{}
|
37
36
|
end
|
38
37
|
end
|
39
|
-
|
40
38
|
end
|
41
39
|
end
|
42
40
|
end
|
@@ -15,7 +15,6 @@ module ActiveModel::Serializer::Lint
|
|
15
15
|
# always return +{}+, and the tests would pass. It is up to you to ensure
|
16
16
|
# that the values are semantically meaningful.
|
17
17
|
module Tests
|
18
|
-
|
19
18
|
# Passes if the object responds to <tt>serializable_hash</tt> and if it takes
|
20
19
|
# zero or one arguments.
|
21
20
|
# Fails otherwise.
|
@@ -23,7 +22,7 @@ module ActiveModel::Serializer::Lint
|
|
23
22
|
# <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
|
24
23
|
# Typically, it is implemented by including ActiveModel::Serialization.
|
25
24
|
def test_serializable_hash
|
26
|
-
assert_respond_to resource, :serializable_hash,
|
25
|
+
assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
|
27
26
|
resource.serializable_hash
|
28
27
|
resource.serializable_hash(nil)
|
29
28
|
end
|
@@ -35,7 +34,7 @@ module ActiveModel::Serializer::Lint
|
|
35
34
|
# <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
|
36
35
|
# Typically, it is implemented by including ActiveModel::Serialization.
|
37
36
|
def test_read_attribute_for_serialization
|
38
|
-
assert_respond_to resource, :read_attribute_for_serialization,
|
37
|
+
assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
|
39
38
|
actual_arity = resource.method(:read_attribute_for_serialization).arity
|
40
39
|
if defined?(::Rubinius)
|
41
40
|
# 1 for def read_attribute_for_serialization(name); end
|
@@ -126,6 +125,5 @@ module ActiveModel::Serializer::Lint
|
|
126
125
|
def assert_instance_of(result, name)
|
127
126
|
assert result.instance_of?(name), "#{result} should be an instance of #{name}"
|
128
127
|
end
|
129
|
-
|
130
128
|
end
|
131
129
|
end
|
@@ -13,7 +13,6 @@ module ActiveModel
|
|
13
13
|
include Configuration
|
14
14
|
include Associations
|
15
15
|
|
16
|
-
|
17
16
|
# Matches
|
18
17
|
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
|
19
18
|
# AND
|
@@ -32,7 +31,6 @@ module ActiveModel
|
|
32
31
|
class << self
|
33
32
|
attr_accessor :_attributes
|
34
33
|
attr_accessor :_attributes_keys
|
35
|
-
attr_accessor :_urls
|
36
34
|
attr_accessor :_cache
|
37
35
|
attr_accessor :_fragmented
|
38
36
|
attr_accessor :_cache_key
|
@@ -45,7 +43,6 @@ module ActiveModel
|
|
45
43
|
def self.inherited(base)
|
46
44
|
base._attributes = self._attributes.try(:dup) || []
|
47
45
|
base._attributes_keys = self._attributes_keys.try(:dup) || {}
|
48
|
-
base._urls = []
|
49
46
|
base._cache_digest = digest_caller_file(caller.first)
|
50
47
|
super
|
51
48
|
end
|
@@ -87,14 +84,6 @@ module ActiveModel
|
|
87
84
|
@_cache_options = (options.empty?) ? nil : options
|
88
85
|
end
|
89
86
|
|
90
|
-
def self.url(attr)
|
91
|
-
@_urls.push attr
|
92
|
-
end
|
93
|
-
|
94
|
-
def self.urls(*attrs)
|
95
|
-
@_urls.concat attrs
|
96
|
-
end
|
97
|
-
|
98
87
|
def self.serializer_for(resource, options = {})
|
99
88
|
if resource.respond_to?(:serializer_class)
|
100
89
|
resource.serializer_class
|
@@ -146,18 +135,6 @@ module ActiveModel
|
|
146
135
|
@root || object.class.model_name.to_s.underscore
|
147
136
|
end
|
148
137
|
|
149
|
-
def id
|
150
|
-
object.id if object
|
151
|
-
end
|
152
|
-
|
153
|
-
def json_api_type
|
154
|
-
if config.jsonapi_resource_type == :plural
|
155
|
-
object.class.model_name.plural
|
156
|
-
else
|
157
|
-
object.class.model_name.singular
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
138
|
def attributes(options = {})
|
162
139
|
attributes =
|
163
140
|
if options[:fields]
|
@@ -166,8 +143,6 @@ module ActiveModel
|
|
166
143
|
self.class._attributes.dup
|
167
144
|
end
|
168
145
|
|
169
|
-
attributes += options[:required_fields] if options[:required_fields]
|
170
|
-
|
171
146
|
attributes.each_with_object({}) do |name, hash|
|
172
147
|
unless self.class._fragmented
|
173
148
|
hash[name] = send(name)
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Rails
|
2
2
|
module Generators
|
3
3
|
class SerializerGenerator < NamedBase
|
4
|
-
source_root File.expand_path(
|
5
|
-
check_class_collision :suffix =>
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
check_class_collision :suffix => 'Serializer'
|
6
6
|
|
7
|
-
argument :attributes, :type => :array, :default => [], :banner =>
|
7
|
+
argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
|
8
8
|
|
9
|
-
class_option :parent, :type => :string, :desc =>
|
9
|
+
class_option :parent, :type => :string, :desc => 'The parent class for the generated serializer'
|
10
10
|
|
11
11
|
def create_serializer_file
|
12
|
-
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
12
|
+
template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
@@ -26,9 +26,9 @@ module Rails
|
|
26
26
|
if options[:parent]
|
27
27
|
options[:parent]
|
28
28
|
elsif defined?(::ApplicationSerializer)
|
29
|
-
|
29
|
+
'ApplicationSerializer'
|
30
30
|
else
|
31
|
-
|
31
|
+
'ActiveModel::Serializer'
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
File without changes
|
File without changes
|
@@ -33,10 +33,10 @@ module ActionController
|
|
33
33
|
expected = {
|
34
34
|
data: {
|
35
35
|
id: assigns(:profile).id.to_s,
|
36
|
-
type:
|
36
|
+
type: 'profiles',
|
37
37
|
attributes: {
|
38
|
-
name:
|
39
|
-
description:
|
38
|
+
name: 'Name 1',
|
39
|
+
description: 'Description 1',
|
40
40
|
}
|
41
41
|
}
|
42
42
|
}
|
@@ -100,11 +100,11 @@ module ActionController
|
|
100
100
|
get :render_array_using_explicit_serializer_and_custom_serializers
|
101
101
|
|
102
102
|
expected = [
|
103
|
-
{
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
103
|
+
{ 'title' => 'New Post',
|
104
|
+
'body' => 'Body',
|
105
|
+
'id' => assigns(:post).id,
|
106
|
+
'comments' => [{ 'id' => 1 }, { 'id' => 2 }],
|
107
|
+
'author' => { 'id' => assigns(:author).id }
|
108
108
|
}
|
109
109
|
]
|
110
110
|
|
@@ -116,13 +116,13 @@ module ActionController
|
|
116
116
|
|
117
117
|
expected = {
|
118
118
|
id: 1337,
|
119
|
-
name:
|
119
|
+
name: 'Amazing Place',
|
120
120
|
locations: [
|
121
121
|
{
|
122
122
|
id: 42,
|
123
|
-
lat:
|
124
|
-
lng:
|
125
|
-
place:
|
123
|
+
lat: '-23.550520',
|
124
|
+
lng: '-46.633309',
|
125
|
+
place: 'Nowhere' # is a virtual attribute on LocationSerializer
|
126
126
|
}
|
127
127
|
]
|
128
128
|
}
|