active_model_serializers 0.10.3 → 0.10.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +154 -2
- data/README.md +153 -15
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer.rb +275 -81
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +53 -14
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -1
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/concerns/caching.rb +36 -23
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +6 -1
- data/lib/active_model/serializer/has_one_reflection.rb +3 -1
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +122 -73
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model_serializers.rb +29 -11
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +4 -2
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api.rb +44 -26
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +42 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -9
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +35 -18
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +2 -0
- data/lib/active_model_serializers/model.rb +111 -30
- data/lib/active_model_serializers/model/caching.rb +25 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +55 -0
- metadata +74 -291
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -102
- data/.simplecov +0 -110
- data/.travis.yml +0 -51
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -62
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -125
- data/docs/README.md +0 -42
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -247
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -169
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -14
- data/docs/general/rendering.md +0 -294
- data/docs/general/serializers.md +0 -461
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -137
- data/docs/howto/add_root_key.md +0 -55
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -65
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -144
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema.md +0 -151
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/concerns/associations.rb +0 -102
- data/lib/active_model/serializer/concerns/attributes.rb +0 -82
- data/lib/active_model/serializer/concerns/configuration.rb +0 -59
- data/lib/active_model/serializer/concerns/links.rb +0 -35
- data/lib/active_model/serializer/concerns/meta.rb +0 -29
- data/lib/active_model/serializer/concerns/type.rb +0 -25
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model_serializers/key_transform.rb +0 -74
- data/test/action_controller/adapter_selector_test.rb +0 -53
- data/test/action_controller/explicit_serializer_test.rb +0 -135
- data/test/action_controller/json/include_test.rb +0 -246
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -40
- data/test/action_controller/json_api/fields_test.rb +0 -57
- data/test/action_controller/json_api/linked_test.rb +0 -202
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -181
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -226
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- data/test/action_controller/serialization_test.rb +0 -472
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -22
- data/test/active_model_serializers/key_transform_test.rb +0 -297
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -22
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -143
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/attributes_test.rb +0 -43
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -104
- data/test/adapter/json/has_many_test.rb +0 -45
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -96
- data/test/adapter/json_api/errors_test.rb +0 -76
- data/test/adapter/json_api/fields_test.rb +0 -88
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -165
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -166
- data/test/adapter/json_api/json_api_test.rb +0 -33
- data/test/adapter/json_api/linked_test.rb +0 -413
- data/test/adapter/json_api/links_test.rb +0 -95
- data/test/adapter/json_api/pagination_links_test.rb +0 -193
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -397
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -504
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -46
- data/test/adapter/null_test.rb +0 -22
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -81
- data/test/benchmark/bm_adapter.rb +0 -38
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_lookup_chain.rb +0 -83
- data/test/benchmark/bm_transform.rb +0 -45
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -83
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -579
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -286
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -74
- data/test/grape_test.rb +0 -178
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -20
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -79
- data/test/serializers/association_macros_test.rb +0 -37
- data/test/serializers/associations_test.rb +0 -370
- data/test/serializers/attribute_test.rb +0 -151
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -202
- data/test/serializers/options_test.rb +0 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -136
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -87
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -82
- data/test/support/rails5_shims.rb +0 -53
- data/test/support/rails_app.rb +0 -36
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -71
- data/test/test_helper.rb +0 -58
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class CollectionSerializerTest < ActiveSupport::TestCase
|
6
|
-
class MessagesSerializer < ActiveModel::Serializer
|
7
|
-
type 'messages'
|
8
|
-
end
|
9
|
-
|
10
|
-
def setup
|
11
|
-
@comment = Comment.new
|
12
|
-
@post = Post.new
|
13
|
-
@resource = build_named_collection @comment, @post
|
14
|
-
@serializer = collection_serializer.new(@resource, some: :options)
|
15
|
-
end
|
16
|
-
|
17
|
-
def collection_serializer
|
18
|
-
CollectionSerializer
|
19
|
-
end
|
20
|
-
|
21
|
-
def build_named_collection(*resource)
|
22
|
-
resource.define_singleton_method(:name) { 'MeResource' }
|
23
|
-
resource
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_has_object_reader_serializer_interface
|
27
|
-
assert_equal @serializer.object, @resource
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_respond_to_each
|
31
|
-
assert_respond_to @serializer, :each
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_each_object_should_be_serialized_with_appropriate_serializer
|
35
|
-
serializers = @serializer.to_a
|
36
|
-
|
37
|
-
assert_kind_of CommentSerializer, serializers.first
|
38
|
-
assert_kind_of Comment, serializers.first.object
|
39
|
-
|
40
|
-
assert_kind_of PostSerializer, serializers.last
|
41
|
-
assert_kind_of Post, serializers.last.object
|
42
|
-
|
43
|
-
assert_equal :options, serializers.last.custom_options[:some]
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_serializer_option_not_passed_to_each_serializer
|
47
|
-
serializers = collection_serializer.new([@post], serializer: PostSerializer).to_a
|
48
|
-
|
49
|
-
refute serializers.first.custom_options.key?(:serializer)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_root_default
|
53
|
-
@serializer = collection_serializer.new([@comment, @post])
|
54
|
-
assert_nil @serializer.root
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_root
|
58
|
-
expected = 'custom_root'
|
59
|
-
@serializer = collection_serializer.new([@comment, @post], root: expected)
|
60
|
-
assert_equal expected, @serializer.root
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_root_with_no_serializers
|
64
|
-
expected = 'custom_root'
|
65
|
-
@serializer = collection_serializer.new([], root: expected)
|
66
|
-
assert_equal expected, @serializer.root
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_json_key_with_resource_with_serializer
|
70
|
-
singular_key = @serializer.send(:serializers).first.json_key
|
71
|
-
assert_equal singular_key.pluralize, @serializer.json_key
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_json_key_with_resource_with_name_and_no_serializers
|
75
|
-
serializer = collection_serializer.new(build_named_collection)
|
76
|
-
assert_equal 'me_resources', serializer.json_key
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_json_key_with_resource_with_nil_name_and_no_serializers
|
80
|
-
resource = []
|
81
|
-
resource.define_singleton_method(:name) { nil }
|
82
|
-
serializer = collection_serializer.new(resource)
|
83
|
-
assert_nil serializer.json_key
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_json_key_with_resource_without_name_and_no_serializers
|
87
|
-
serializer = collection_serializer.new([])
|
88
|
-
assert_nil serializer.json_key
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_json_key_with_empty_resources_with_serializer
|
92
|
-
resource = []
|
93
|
-
serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
|
94
|
-
assert_equal 'messages', serializer.json_key
|
95
|
-
end
|
96
|
-
|
97
|
-
def test_json_key_with_root
|
98
|
-
expected = 'custom_root'
|
99
|
-
serializer = collection_serializer.new(@resource, root: expected)
|
100
|
-
assert_equal expected, serializer.json_key
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_json_key_with_root_and_no_serializers
|
104
|
-
expected = 'custom_root'
|
105
|
-
serializer = collection_serializer.new(build_named_collection, root: expected)
|
106
|
-
assert_equal expected, serializer.json_key
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,78 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
4
|
-
ActiveRecord::Schema.define do
|
5
|
-
self.verbose = false
|
6
|
-
create_table :posts, force: true do |t|
|
7
|
-
t.string :title
|
8
|
-
t.text :body
|
9
|
-
t.references :author
|
10
|
-
t.timestamps null: false
|
11
|
-
end
|
12
|
-
create_table :authors, force: true do |t|
|
13
|
-
t.string :name
|
14
|
-
t.timestamps null: false
|
15
|
-
end
|
16
|
-
create_table :comments, force: true do |t|
|
17
|
-
t.text :contents
|
18
|
-
t.references :author
|
19
|
-
t.references :post
|
20
|
-
t.timestamp null: false
|
21
|
-
end
|
22
|
-
create_table :employees, force: true do |t|
|
23
|
-
t.string :name
|
24
|
-
t.string :email
|
25
|
-
t.timestamp null: false
|
26
|
-
end
|
27
|
-
create_table :object_tags, force: true do |t|
|
28
|
-
t.string :poly_tag_id
|
29
|
-
t.string :taggable_type
|
30
|
-
t.string :taggable_id
|
31
|
-
t.timestamp null: false
|
32
|
-
end
|
33
|
-
create_table :poly_tags, force: true do |t|
|
34
|
-
t.string :phrase
|
35
|
-
t.timestamp null: false
|
36
|
-
end
|
37
|
-
create_table :pictures, force: true do |t|
|
38
|
-
t.string :title
|
39
|
-
t.string :imageable_type
|
40
|
-
t.string :imageable_id
|
41
|
-
t.timestamp null: false
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
module ARModels
|
46
|
-
class Post < ActiveRecord::Base
|
47
|
-
has_many :comments
|
48
|
-
belongs_to :author
|
49
|
-
end
|
50
|
-
|
51
|
-
class Comment < ActiveRecord::Base
|
52
|
-
belongs_to :post
|
53
|
-
belongs_to :author
|
54
|
-
end
|
55
|
-
|
56
|
-
class Author < ActiveRecord::Base
|
57
|
-
has_many :posts
|
58
|
-
end
|
59
|
-
|
60
|
-
class PostSerializer < ActiveModel::Serializer
|
61
|
-
attributes :id, :title, :body
|
62
|
-
|
63
|
-
has_many :comments
|
64
|
-
belongs_to :author
|
65
|
-
end
|
66
|
-
|
67
|
-
class CommentSerializer < ActiveModel::Serializer
|
68
|
-
attributes :id, :contents
|
69
|
-
|
70
|
-
belongs_to :author
|
71
|
-
end
|
72
|
-
|
73
|
-
class AuthorSerializer < ActiveModel::Serializer
|
74
|
-
attributes :id, :name
|
75
|
-
|
76
|
-
has_many :posts
|
77
|
-
end
|
78
|
-
end
|
data/test/fixtures/poro.rb
DELETED
@@ -1,286 +0,0 @@
|
|
1
|
-
verbose = $VERBOSE
|
2
|
-
$VERBOSE = nil
|
3
|
-
class Model < ActiveModelSerializers::Model
|
4
|
-
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
|
5
|
-
|
6
|
-
### Helper methods, not required to be serializable
|
7
|
-
|
8
|
-
# Convenience when not adding @attributes readers and writers
|
9
|
-
def method_missing(meth, *args)
|
10
|
-
if meth.to_s =~ /^(.*)=$/
|
11
|
-
attributes[Regexp.last_match(1).to_sym] = args[0]
|
12
|
-
elsif attributes.key?(meth)
|
13
|
-
attributes[meth]
|
14
|
-
else
|
15
|
-
super
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# required for ActiveModel::AttributeAssignment#_assign_attribute
|
20
|
-
# in Rails 5
|
21
|
-
def respond_to_missing?(method_name, _include_private = false)
|
22
|
-
attributes.key?(method_name.to_s.tr('=', '').to_sym) || super
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# see
|
27
|
-
# https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb
|
28
|
-
# The below allows you to do:
|
29
|
-
#
|
30
|
-
# model = ModelWithErrors.new
|
31
|
-
# model.validate! # => ["cannot be nil"]
|
32
|
-
# model.errors.full_messages # => ["name cannot be nil"]
|
33
|
-
class ModelWithErrors < ::ActiveModelSerializers::Model
|
34
|
-
attr_accessor :name
|
35
|
-
end
|
36
|
-
|
37
|
-
class Profile < Model
|
38
|
-
end
|
39
|
-
|
40
|
-
class ProfileSerializer < ActiveModel::Serializer
|
41
|
-
attributes :name, :description
|
42
|
-
|
43
|
-
# TODO: is this used anywhere?
|
44
|
-
def arguments_passed_in?
|
45
|
-
instance_options[:my_options] == :accessible
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class ProfilePreviewSerializer < ActiveModel::Serializer
|
50
|
-
attributes :name
|
51
|
-
end
|
52
|
-
|
53
|
-
class Post < Model; end
|
54
|
-
class Like < Model; end
|
55
|
-
class Author < Model; end
|
56
|
-
class Bio < Model; end
|
57
|
-
class Blog < Model; end
|
58
|
-
class Role < Model; end
|
59
|
-
class User < Model; end
|
60
|
-
class Location < Model; end
|
61
|
-
class Place < Model; end
|
62
|
-
class Tag < Model; end
|
63
|
-
class VirtualValue < Model; end
|
64
|
-
class Comment < Model
|
65
|
-
# Uses a custom non-time-based cache key
|
66
|
-
def cache_key
|
67
|
-
"#{self.class.name.downcase}/#{id}"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class Employee < ActiveRecord::Base
|
72
|
-
has_many :pictures, as: :imageable
|
73
|
-
has_many :object_tags, as: :taggable
|
74
|
-
end
|
75
|
-
|
76
|
-
class ObjectTag < ActiveRecord::Base
|
77
|
-
belongs_to :poly_tag
|
78
|
-
belongs_to :taggable, polymorphic: true
|
79
|
-
end
|
80
|
-
|
81
|
-
class Picture < ActiveRecord::Base
|
82
|
-
belongs_to :imageable, polymorphic: true
|
83
|
-
has_many :object_tags, as: :taggable
|
84
|
-
end
|
85
|
-
|
86
|
-
class PolyTag < ActiveRecord::Base
|
87
|
-
has_many :object_tags
|
88
|
-
end
|
89
|
-
|
90
|
-
module Spam
|
91
|
-
class UnrelatedLink < Model; end
|
92
|
-
end
|
93
|
-
|
94
|
-
class PostSerializer < ActiveModel::Serializer
|
95
|
-
cache key: 'post', expires_in: 0.1, skip_digest: true
|
96
|
-
attributes :id, :title, :body
|
97
|
-
|
98
|
-
has_many :comments
|
99
|
-
belongs_to :blog
|
100
|
-
belongs_to :author
|
101
|
-
|
102
|
-
def blog
|
103
|
-
Blog.new(id: 999, name: 'Custom blog')
|
104
|
-
end
|
105
|
-
|
106
|
-
# TODO: is this used anywhere?
|
107
|
-
def custom_options
|
108
|
-
instance_options
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
class SpammyPostSerializer < ActiveModel::Serializer
|
113
|
-
attributes :id
|
114
|
-
has_many :related
|
115
|
-
end
|
116
|
-
|
117
|
-
class CommentSerializer < ActiveModel::Serializer
|
118
|
-
cache expires_in: 1.day, skip_digest: true
|
119
|
-
attributes :id, :body
|
120
|
-
|
121
|
-
belongs_to :post
|
122
|
-
belongs_to :author
|
123
|
-
|
124
|
-
def custom_options
|
125
|
-
instance_options
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
class AuthorSerializer < ActiveModel::Serializer
|
130
|
-
cache key: 'writer', skip_digest: true
|
131
|
-
attribute :id
|
132
|
-
attribute :name
|
133
|
-
|
134
|
-
has_many :posts
|
135
|
-
has_many :roles
|
136
|
-
has_one :bio
|
137
|
-
end
|
138
|
-
|
139
|
-
class RoleSerializer < ActiveModel::Serializer
|
140
|
-
cache only: [:name, :slug], skip_digest: true
|
141
|
-
attributes :id, :name, :description
|
142
|
-
attribute :friendly_id, key: :slug
|
143
|
-
|
144
|
-
def friendly_id
|
145
|
-
"#{object.name}-#{object.id}"
|
146
|
-
end
|
147
|
-
|
148
|
-
belongs_to :author
|
149
|
-
end
|
150
|
-
|
151
|
-
class LikeSerializer < ActiveModel::Serializer
|
152
|
-
attributes :id, :time
|
153
|
-
|
154
|
-
belongs_to :likeable
|
155
|
-
end
|
156
|
-
|
157
|
-
class LocationSerializer < ActiveModel::Serializer
|
158
|
-
cache only: [:address], skip_digest: true
|
159
|
-
attributes :id, :lat, :lng
|
160
|
-
|
161
|
-
belongs_to :place, key: :address
|
162
|
-
|
163
|
-
def place
|
164
|
-
'Nowhere'
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
class PlaceSerializer < ActiveModel::Serializer
|
169
|
-
attributes :id, :name
|
170
|
-
|
171
|
-
has_many :locations
|
172
|
-
end
|
173
|
-
|
174
|
-
class BioSerializer < ActiveModel::Serializer
|
175
|
-
cache except: [:content], skip_digest: true
|
176
|
-
attributes :id, :content, :rating
|
177
|
-
|
178
|
-
belongs_to :author
|
179
|
-
end
|
180
|
-
|
181
|
-
class BlogSerializer < ActiveModel::Serializer
|
182
|
-
cache key: 'blog'
|
183
|
-
attributes :id, :name
|
184
|
-
|
185
|
-
belongs_to :writer
|
186
|
-
has_many :articles
|
187
|
-
end
|
188
|
-
|
189
|
-
class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
|
190
|
-
def json_key
|
191
|
-
'paginated'
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
class AlternateBlogSerializer < ActiveModel::Serializer
|
196
|
-
attribute :id
|
197
|
-
attribute :name, key: :title
|
198
|
-
end
|
199
|
-
|
200
|
-
class CustomBlogSerializer < ActiveModel::Serializer
|
201
|
-
attribute :id
|
202
|
-
attribute :special_attribute
|
203
|
-
|
204
|
-
has_many :articles
|
205
|
-
end
|
206
|
-
|
207
|
-
class CommentPreviewSerializer < ActiveModel::Serializer
|
208
|
-
attributes :id
|
209
|
-
|
210
|
-
belongs_to :post
|
211
|
-
end
|
212
|
-
|
213
|
-
class AuthorPreviewSerializer < ActiveModel::Serializer
|
214
|
-
attributes :id
|
215
|
-
|
216
|
-
has_many :posts
|
217
|
-
end
|
218
|
-
|
219
|
-
class PostPreviewSerializer < ActiveModel::Serializer
|
220
|
-
attributes :title, :body, :id
|
221
|
-
|
222
|
-
has_many :comments, serializer: CommentPreviewSerializer
|
223
|
-
belongs_to :author, serializer: AuthorPreviewSerializer
|
224
|
-
end
|
225
|
-
|
226
|
-
class PostWithTagsSerializer < ActiveModel::Serializer
|
227
|
-
attributes :id
|
228
|
-
|
229
|
-
has_many :tags
|
230
|
-
end
|
231
|
-
|
232
|
-
class PostWithCustomKeysSerializer < ActiveModel::Serializer
|
233
|
-
attributes :id
|
234
|
-
|
235
|
-
has_many :comments, key: :reviews
|
236
|
-
belongs_to :author, key: :writer
|
237
|
-
has_one :blog, key: :site
|
238
|
-
end
|
239
|
-
|
240
|
-
class VirtualValueSerializer < ActiveModel::Serializer
|
241
|
-
attributes :id
|
242
|
-
|
243
|
-
has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
|
244
|
-
{ type: 'reviews', id: '2' }]
|
245
|
-
has_one :maker, virtual_value: { type: 'makers', id: '1' }
|
246
|
-
|
247
|
-
def reviews
|
248
|
-
end
|
249
|
-
|
250
|
-
def maker
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
class PolymorphicHasManySerializer < ActiveModel::Serializer
|
255
|
-
attributes :id, :name
|
256
|
-
end
|
257
|
-
|
258
|
-
class PolymorphicBelongsToSerializer < ActiveModel::Serializer
|
259
|
-
attributes :id, :title
|
260
|
-
|
261
|
-
has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
|
262
|
-
end
|
263
|
-
|
264
|
-
class PolymorphicSimpleSerializer < ActiveModel::Serializer
|
265
|
-
attributes :id
|
266
|
-
end
|
267
|
-
|
268
|
-
class PolymorphicObjectTagSerializer < ActiveModel::Serializer
|
269
|
-
attributes :id
|
270
|
-
|
271
|
-
has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
|
272
|
-
end
|
273
|
-
|
274
|
-
class PolymorphicTagSerializer < ActiveModel::Serializer
|
275
|
-
attributes :id, :phrase
|
276
|
-
|
277
|
-
has_many :object_tags, serializer: PolymorphicObjectTagSerializer
|
278
|
-
end
|
279
|
-
|
280
|
-
module Spam
|
281
|
-
class UnrelatedLinkSerializer < ActiveModel::Serializer
|
282
|
-
cache only: [:id]
|
283
|
-
attributes :id
|
284
|
-
end
|
285
|
-
end
|
286
|
-
$VERBOSE = verbose
|