agi_active_model_serializers 0.10.7

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.
Files changed (220) hide show
  1. checksums.yaml +7 -0
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +35 -0
  5. data/.rubocop.yml +102 -0
  6. data/.simplecov +110 -0
  7. data/.travis.yml +51 -0
  8. data/CHANGELOG.md +612 -0
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/CONTRIBUTING.md +105 -0
  11. data/Gemfile +56 -0
  12. data/MIT-LICENSE +22 -0
  13. data/README.md +307 -0
  14. data/Rakefile +103 -0
  15. data/active_model_serializers.gemspec +63 -0
  16. data/appveyor.yml +24 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/README.md +41 -0
  21. data/docs/STYLE.md +58 -0
  22. data/docs/general/adapters.md +247 -0
  23. data/docs/general/caching.md +58 -0
  24. data/docs/general/configuration_options.md +169 -0
  25. data/docs/general/deserialization.md +100 -0
  26. data/docs/general/fields.md +31 -0
  27. data/docs/general/getting_started.md +133 -0
  28. data/docs/general/instrumentation.md +40 -0
  29. data/docs/general/key_transforms.md +40 -0
  30. data/docs/general/logging.md +14 -0
  31. data/docs/general/rendering.md +279 -0
  32. data/docs/general/serializers.md +461 -0
  33. data/docs/how-open-source-maintained.jpg +0 -0
  34. data/docs/howto/add_pagination_links.md +138 -0
  35. data/docs/howto/add_relationship_links.md +137 -0
  36. data/docs/howto/add_root_key.md +55 -0
  37. data/docs/howto/grape_integration.md +42 -0
  38. data/docs/howto/outside_controller_use.md +65 -0
  39. data/docs/howto/passing_arbitrary_options.md +27 -0
  40. data/docs/howto/serialize_poro.md +32 -0
  41. data/docs/howto/test.md +154 -0
  42. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  43. data/docs/integrations/ember-and-json-api.md +144 -0
  44. data/docs/integrations/grape.md +19 -0
  45. data/docs/jsonapi/errors.md +56 -0
  46. data/docs/jsonapi/schema.md +151 -0
  47. data/docs/jsonapi/schema/schema.json +366 -0
  48. data/docs/rfcs/0000-namespace.md +106 -0
  49. data/docs/rfcs/template.md +15 -0
  50. data/lib/action_controller/serialization.rb +66 -0
  51. data/lib/active_model/serializable_resource.rb +11 -0
  52. data/lib/active_model/serializer.rb +231 -0
  53. data/lib/active_model/serializer/adapter.rb +24 -0
  54. data/lib/active_model/serializer/adapter/attributes.rb +15 -0
  55. data/lib/active_model/serializer/adapter/base.rb +18 -0
  56. data/lib/active_model/serializer/adapter/json.rb +15 -0
  57. data/lib/active_model/serializer/adapter/json_api.rb +15 -0
  58. data/lib/active_model/serializer/adapter/null.rb +15 -0
  59. data/lib/active_model/serializer/array_serializer.rb +12 -0
  60. data/lib/active_model/serializer/association.rb +34 -0
  61. data/lib/active_model/serializer/attribute.rb +25 -0
  62. data/lib/active_model/serializer/belongs_to_reflection.rb +7 -0
  63. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  64. data/lib/active_model/serializer/collection_serializer.rb +87 -0
  65. data/lib/active_model/serializer/concerns/associations.rb +102 -0
  66. data/lib/active_model/serializer/concerns/attributes.rb +82 -0
  67. data/lib/active_model/serializer/concerns/caching.rb +292 -0
  68. data/lib/active_model/serializer/concerns/configuration.rb +59 -0
  69. data/lib/active_model/serializer/concerns/links.rb +35 -0
  70. data/lib/active_model/serializer/concerns/meta.rb +29 -0
  71. data/lib/active_model/serializer/concerns/type.rb +25 -0
  72. data/lib/active_model/serializer/error_serializer.rb +14 -0
  73. data/lib/active_model/serializer/errors_serializer.rb +32 -0
  74. data/lib/active_model/serializer/field.rb +90 -0
  75. data/lib/active_model/serializer/fieldset.rb +31 -0
  76. data/lib/active_model/serializer/has_many_reflection.rb +7 -0
  77. data/lib/active_model/serializer/has_one_reflection.rb +7 -0
  78. data/lib/active_model/serializer/lint.rb +150 -0
  79. data/lib/active_model/serializer/null.rb +17 -0
  80. data/lib/active_model/serializer/reflection.rb +163 -0
  81. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  82. data/lib/active_model/serializer/version.rb +5 -0
  83. data/lib/active_model_serializers.rb +53 -0
  84. data/lib/active_model_serializers/adapter.rb +98 -0
  85. data/lib/active_model_serializers/adapter/attributes.rb +13 -0
  86. data/lib/active_model_serializers/adapter/base.rb +83 -0
  87. data/lib/active_model_serializers/adapter/json.rb +21 -0
  88. data/lib/active_model_serializers/adapter/json_api.rb +517 -0
  89. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  90. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  91. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  92. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  93. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  94. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +69 -0
  95. data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -0
  96. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +51 -0
  97. data/lib/active_model_serializers/adapter/null.rb +9 -0
  98. data/lib/active_model_serializers/callbacks.rb +55 -0
  99. data/lib/active_model_serializers/deprecate.rb +54 -0
  100. data/lib/active_model_serializers/deserialization.rb +15 -0
  101. data/lib/active_model_serializers/json_pointer.rb +14 -0
  102. data/lib/active_model_serializers/logging.rb +122 -0
  103. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  104. data/lib/active_model_serializers/model.rb +71 -0
  105. data/lib/active_model_serializers/railtie.rb +48 -0
  106. data/lib/active_model_serializers/register_jsonapi_renderer.rb +78 -0
  107. data/lib/active_model_serializers/serializable_resource.rb +82 -0
  108. data/lib/active_model_serializers/serialization_context.rb +39 -0
  109. data/lib/active_model_serializers/test.rb +7 -0
  110. data/lib/active_model_serializers/test/schema.rb +138 -0
  111. data/lib/active_model_serializers/test/serializer.rb +125 -0
  112. data/lib/generators/rails/USAGE +6 -0
  113. data/lib/generators/rails/resource_override.rb +10 -0
  114. data/lib/generators/rails/serializer_generator.rb +36 -0
  115. data/lib/generators/rails/templates/serializer.rb.erb +15 -0
  116. data/lib/grape/active_model_serializers.rb +16 -0
  117. data/lib/grape/formatters/active_model_serializers.rb +32 -0
  118. data/lib/grape/helpers/active_model_serializers.rb +17 -0
  119. data/test/action_controller/adapter_selector_test.rb +53 -0
  120. data/test/action_controller/explicit_serializer_test.rb +135 -0
  121. data/test/action_controller/json/include_test.rb +246 -0
  122. data/test/action_controller/json_api/deserialization_test.rb +112 -0
  123. data/test/action_controller/json_api/errors_test.rb +40 -0
  124. data/test/action_controller/json_api/fields_test.rb +66 -0
  125. data/test/action_controller/json_api/linked_test.rb +202 -0
  126. data/test/action_controller/json_api/pagination_test.rb +116 -0
  127. data/test/action_controller/json_api/transform_test.rb +189 -0
  128. data/test/action_controller/lookup_proc_test.rb +49 -0
  129. data/test/action_controller/namespace_lookup_test.rb +232 -0
  130. data/test/action_controller/serialization_scope_name_test.rb +229 -0
  131. data/test/action_controller/serialization_test.rb +472 -0
  132. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  133. data/test/active_model_serializers/json_pointer_test.rb +22 -0
  134. data/test/active_model_serializers/logging_test.rb +77 -0
  135. data/test/active_model_serializers/model_test.rb +69 -0
  136. data/test/active_model_serializers/railtie_test_isolated.rb +63 -0
  137. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
  138. data/test/active_model_serializers/serialization_context_test_isolated.rb +71 -0
  139. data/test/active_model_serializers/test/schema_test.rb +131 -0
  140. data/test/active_model_serializers/test/serializer_test.rb +62 -0
  141. data/test/active_record_test.rb +9 -0
  142. data/test/adapter/attributes_test.rb +43 -0
  143. data/test/adapter/deprecation_test.rb +100 -0
  144. data/test/adapter/json/belongs_to_test.rb +45 -0
  145. data/test/adapter/json/collection_test.rb +104 -0
  146. data/test/adapter/json/has_many_test.rb +45 -0
  147. data/test/adapter/json/transform_test.rb +93 -0
  148. data/test/adapter/json_api/belongs_to_test.rb +155 -0
  149. data/test/adapter/json_api/collection_test.rb +96 -0
  150. data/test/adapter/json_api/errors_test.rb +76 -0
  151. data/test/adapter/json_api/fields_test.rb +96 -0
  152. data/test/adapter/json_api/has_many_embed_ids_test.rb +43 -0
  153. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
  154. data/test/adapter/json_api/has_many_test.rb +165 -0
  155. data/test/adapter/json_api/has_one_test.rb +80 -0
  156. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +168 -0
  157. data/test/adapter/json_api/json_api_test.rb +33 -0
  158. data/test/adapter/json_api/linked_test.rb +413 -0
  159. data/test/adapter/json_api/links_test.rb +95 -0
  160. data/test/adapter/json_api/pagination_links_test.rb +193 -0
  161. data/test/adapter/json_api/parse_test.rb +137 -0
  162. data/test/adapter/json_api/relationship_test.rb +397 -0
  163. data/test/adapter/json_api/resource_identifier_test.rb +110 -0
  164. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  165. data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
  166. data/test/adapter/json_api/transform_test.rb +512 -0
  167. data/test/adapter/json_api/type_test.rb +61 -0
  168. data/test/adapter/json_test.rb +46 -0
  169. data/test/adapter/null_test.rb +22 -0
  170. data/test/adapter/polymorphic_test.rb +171 -0
  171. data/test/adapter_test.rb +67 -0
  172. data/test/array_serializer_test.rb +22 -0
  173. data/test/benchmark/app.rb +65 -0
  174. data/test/benchmark/benchmarking_support.rb +67 -0
  175. data/test/benchmark/bm_active_record.rb +81 -0
  176. data/test/benchmark/bm_adapter.rb +38 -0
  177. data/test/benchmark/bm_caching.rb +119 -0
  178. data/test/benchmark/bm_lookup_chain.rb +83 -0
  179. data/test/benchmark/bm_transform.rb +45 -0
  180. data/test/benchmark/config.ru +3 -0
  181. data/test/benchmark/controllers.rb +83 -0
  182. data/test/benchmark/fixtures.rb +219 -0
  183. data/test/cache_test.rb +595 -0
  184. data/test/collection_serializer_test.rb +123 -0
  185. data/test/fixtures/active_record.rb +113 -0
  186. data/test/fixtures/poro.rb +232 -0
  187. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  188. data/test/generators/serializer_generator_test.rb +74 -0
  189. data/test/grape_test.rb +178 -0
  190. data/test/lint_test.rb +49 -0
  191. data/test/logger_test.rb +20 -0
  192. data/test/poro_test.rb +9 -0
  193. data/test/serializable_resource_test.rb +79 -0
  194. data/test/serializers/association_macros_test.rb +37 -0
  195. data/test/serializers/associations_test.rb +383 -0
  196. data/test/serializers/attribute_test.rb +153 -0
  197. data/test/serializers/attributes_test.rb +52 -0
  198. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  199. data/test/serializers/configuration_test.rb +32 -0
  200. data/test/serializers/fieldset_test.rb +14 -0
  201. data/test/serializers/meta_test.rb +202 -0
  202. data/test/serializers/options_test.rb +32 -0
  203. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  204. data/test/serializers/root_test.rb +21 -0
  205. data/test/serializers/serialization_test.rb +55 -0
  206. data/test/serializers/serializer_for_test.rb +136 -0
  207. data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
  208. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  209. data/test/support/isolated_unit.rb +82 -0
  210. data/test/support/rails5_shims.rb +53 -0
  211. data/test/support/rails_app.rb +36 -0
  212. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  213. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  214. data/test/support/schemas/custom/show.json +7 -0
  215. data/test/support/schemas/hyper_schema.json +93 -0
  216. data/test/support/schemas/render_using_json_api.json +43 -0
  217. data/test/support/schemas/simple_json_pointers.json +10 -0
  218. data/test/support/serialization_testing.rb +71 -0
  219. data/test/test_helper.rb +58 -0
  220. metadata +602 -0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting one of the owners listed at https://rubygems.org/gems/active_model_serializers. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,105 @@
1
+ ## Have an issue?
2
+
3
+ Before opening an issue, try the following:
4
+
5
+ ##### Consult the documentation
6
+
7
+ See if your issue can be resolved by information in the documentation.
8
+
9
+ - [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master/docs)
10
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
11
+ - [Guides](docs)
12
+ - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
13
+ - [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
14
+
15
+ ##### Check for an existing issue
16
+
17
+ Take a look at the issues to see if a similar one has already been created. If
18
+ one exists, please add any additional information that might expedite
19
+ resolution.
20
+
21
+ #### Open an issue
22
+
23
+ If the documentation wasn't able to help resolve the issue and no issue already
24
+ exists, please open a new issue with the following in mind:
25
+
26
+ - Please make sure only to include one issue per report. If you encounter
27
+ multiple, unrelated issues, please report them as such.
28
+ - Be detailed. Provide backtraces and example code when possible. Provide
29
+ information about your environment. e.g., Ruby version, rails version, etc.
30
+ - Own your issue. Actively participate in the discussion and help drive the
31
+ issue to closure.
32
+ - If you resolve your own issue, please share the details on the issue and close
33
+ it out. Others might have the same issue and sharing solutions is helpful.
34
+
35
+ ## Contributing
36
+
37
+ Contributing can be done in many ways and is not exclusive to code. If you have
38
+ thoughts on a particular issue or feature, we encourage you to open new issues
39
+ for discussion or add your comments to existing ones.
40
+
41
+ #### Pull requests
42
+
43
+ We also gladly welcome pull requests. When preparing to work on pull request,
44
+ please adhere to these standards:
45
+
46
+ - Base work on the master branch unless fixing an issue with
47
+ [0.9-stable](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
48
+ or
49
+ [0.8-stable](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
50
+ - Squash your commits and regularly rebase off master.
51
+ - Provide a description of the changes contained in the pull request.
52
+ - Note any specific areas that should be reviewed.
53
+ - Include tests.
54
+ - The test suite must pass on [supported Ruby versions](.travis.yml)
55
+ - Include updates to the [documentation](https://github.com/rails-api/active_model_serializers/tree/master/docs)
56
+ where applicable.
57
+ - Update the
58
+ [CHANGELOG](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md)
59
+ to the appropriate sections with a brief description of the changes.
60
+ - Do not change the VERSION file.
61
+
62
+ #### Running tests
63
+
64
+ Run all tests
65
+
66
+ `$ rake test`
67
+
68
+ Run a single test suite
69
+
70
+ `$ rake test TEST=path/to/test.rb`
71
+
72
+ Run a single test
73
+
74
+ `$ rake test TEST=path/to/test.rb TESTOPTS="--name=test_something"`
75
+
76
+ Run tests against different Rails versions by setting the RAILS_VERSION variable
77
+ and bundling gems. (save this script somewhere executable and run from top of AMS repository)
78
+
79
+ ```bash
80
+ #!/usr/bin/env bash
81
+
82
+ rcommand='puts YAML.load_file("./.travis.yml")["env"]["matrix"].join(" ").gsub("RAILS_VERSION=", "")'
83
+ versions=$(ruby -ryaml -e "$rcommand")
84
+
85
+ for version in ${versions[@]}; do
86
+ export RAILS_VERSION="$version"
87
+ rm -f Gemfile.lock
88
+ bundle check || bundle --local || bundle
89
+ bundle exec rake test
90
+ if [ "$?" -eq 0 ]; then
91
+ # green in ANSI
92
+ echo -e "\033[32m **** Tests passed against Rails ${RAILS_VERSION} **** \033[0m"
93
+ else
94
+ # red in ANSI
95
+ echo -e "\033[31m **** Tests failed against Rails ${RAILS_VERSION} **** \033[0m"
96
+ read -p '[Enter] any key to continue, [q] to quit...' prompt
97
+ if [ "$prompt" = 'q' ]; then
98
+ unset RAILS_VERSION
99
+ exit 1
100
+ fi
101
+ fi
102
+ unset RAILS_VERSION
103
+ done
104
+ ```
105
+
data/Gemfile ADDED
@@ -0,0 +1,56 @@
1
+ source 'https://rubygems.org'
2
+ #
3
+ # Add a Gemfile.local to locally bundle gems outside of version control
4
+ local_gemfile = File.join(File.expand_path('..', __FILE__), 'Gemfile.local')
5
+ eval_gemfile local_gemfile if File.readable?(local_gemfile)
6
+
7
+ # Specify your gem's dependencies in active_model_serializers.gemspec
8
+ gemspec
9
+
10
+ version = ENV['RAILS_VERSION'] || '4.2'
11
+
12
+ if version == 'master'
13
+ gem 'rack', github: 'rack/rack'
14
+ gem 'arel', github: 'rails/arel'
15
+ git 'https://github.com/rails/rails.git' do
16
+ gem 'railties'
17
+ gem 'activesupport'
18
+ gem 'activemodel'
19
+ gem 'actionpack'
20
+ gem 'activerecord', group: :test
21
+ # Rails 5
22
+ gem 'actionview'
23
+ end
24
+ else
25
+ gem_version = "~> #{version}.0"
26
+ gem 'railties', gem_version
27
+ gem 'activesupport', gem_version
28
+ gem 'activemodel', gem_version
29
+ gem 'actionpack', gem_version
30
+ gem 'activerecord', gem_version, group: :test
31
+ end
32
+
33
+ # https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54
34
+ @windows_platforms = [:mswin, :mingw, :x64_mingw]
35
+
36
+ # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
37
+ gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
38
+
39
+ group :bench do
40
+ # https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
41
+ gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
42
+ end
43
+
44
+ group :test do
45
+ gem 'sqlite3', platform: (@windows_platforms + [:ruby])
46
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
47
+ gem 'codeclimate-test-reporter', require: false
48
+ gem 'm', '~> 1.5'
49
+ gem 'pry', '~> 0.10'
50
+ gem 'pry-byebug', '~> 3.4', platform: :ruby
51
+ end
52
+
53
+ group :development, :test do
54
+ gem 'rubocop', '~> 0.40.0', require: false
55
+ gem 'yard', require: false
56
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Steve Klabnik
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,307 @@
1
+ # ActiveModelSerializers
2
+
3
+ <table>
4
+ <tr>
5
+ <td>Build Status</td>
6
+ <td>
7
+ <a href="https://travis-ci.org/rails-api/active_model_serializers"><img src="https://travis-ci.org/rails-api/active_model_serializers.svg?branch=master" alt="Build Status" ></a>
8
+ <a href="https://ci.appveyor.com/project/joaomdmoura/active-model-serializers/branch/master"><img src="https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true" alt="Build status"></a>
9
+ </td>
10
+ </tr>
11
+ <tr>
12
+ <td>Code Quality</td>
13
+ <td>
14
+ <a href="https://codeclimate.com/github/rails-api/active_model_serializers"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/gpa.svg" alt="Code Quality"></a>
15
+ <a href="https://codebeat.co/projects/github-com-rails-api-active_model_serializers"><img src="https://codebeat.co/badges/a9ab35fa-8b5a-4680-9d4e-a81f9a55ebcd" alt="codebeat" ></a>
16
+ <a href="https://codeclimate.com/github/rails-api/active_model_serializers/coverage"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/coverage.svg" alt="Test Coverage"></a>
17
+ </td>
18
+ </tr>
19
+ <tr>
20
+ <td>Issue Stats</td>
21
+ <td>
22
+ <a href="https://github.com/rails-api/active_model_serializers/pulse/monthly">Pulse</a>
23
+ </td>
24
+ </tr>
25
+ </table>
26
+
27
+ ## About
28
+
29
+ ActiveModelSerializers brings convention over configuration to your JSON generation.
30
+
31
+ ActiveModelSerializers works through two components: **serializers** and **adapters**.
32
+
33
+ Serializers describe _which_ attributes and relationships should be serialized.
34
+
35
+ Adapters describe _how_ attributes and relationships should be serialized.
36
+
37
+ SerializableResource co-ordinates the resource, Adapter and Serializer to produce the
38
+ resource serialization. The serialization has the `#as_json`, `#to_json` and `#serializable_hash`
39
+ methods used by the Rails JSON Renderer. (SerializableResource actually delegates
40
+ these methods to the adapter.)
41
+
42
+ By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
43
+ But we strongly advise you to use **JsonApi Adapter**, which
44
+ follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
45
+ Check how to change the adapter in the sections below.
46
+
47
+ `0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
48
+
49
+ `0.10.x` is based on the `0.8.0` code, but with a more flexible
50
+ architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
51
+
52
+ It is generally safe and recommended to use the master branch.
53
+
54
+ ## Installation
55
+
56
+ Add this line to your application's Gemfile:
57
+
58
+ ```
59
+ gem 'active_model_serializers', '~> 0.10.0'
60
+ ```
61
+
62
+ And then execute:
63
+
64
+ ```
65
+ $ bundle
66
+ ```
67
+
68
+ ## Getting Started
69
+
70
+ See [Getting Started](docs/general/getting_started.md) for the nuts and bolts.
71
+
72
+ More information is available in the [Guides](docs) and
73
+ [High-level behavior](README.md#high-level-behavior).
74
+
75
+ ## Getting Help
76
+
77
+ If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new)
78
+ and see our [contributing guide](CONTRIBUTING.md).
79
+
80
+ If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
81
+
82
+ If you'd like to chat, we have a [community slack](http://amserializers.herokuapp.com).
83
+
84
+ Thanks!
85
+
86
+ ## Documentation
87
+
88
+ If you're reading this at https://github.com/rails-api/active_model_serializers you are
89
+ reading documentation for our `master`, which may include features that have not
90
+ been released yet. Please see below for the documentation relevant to you.
91
+
92
+ - [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
93
+ - [0.10.3 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.3)
94
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/active_model_serializers/0.10.3)
95
+ - [Guides](docs)
96
+ - [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
97
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
98
+ - [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
99
+ - [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
100
+
101
+
102
+ ## High-level behavior
103
+
104
+ Choose an adapter from [adapters](lib/active_model_serializers/adapter):
105
+
106
+ ``` ruby
107
+ ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
108
+ ```
109
+
110
+ Given a [serializable model](lib/active_model/serializer/lint.rb):
111
+
112
+ ```ruby
113
+ # either
114
+ class SomeResource < ActiveRecord::Base
115
+ # columns: title, body
116
+ end
117
+ # or
118
+ class SomeResource < ActiveModelSerializers::Model
119
+ attributes :title, :body
120
+ end
121
+ ```
122
+
123
+ And initialized as:
124
+
125
+ ```ruby
126
+ resource = SomeResource.new(title: 'ActiveModelSerializers', body: 'Convention over configuration')
127
+ ```
128
+
129
+ Given a serializer for the serializable model:
130
+
131
+ ```ruby
132
+ class SomeSerializer < ActiveModel::Serializer
133
+ attribute :title, key: :name
134
+ attributes :body
135
+ end
136
+ ```
137
+
138
+ The model can be serialized as:
139
+
140
+ ```ruby
141
+ options = {}
142
+ serialization = ActiveModelSerializers::SerializableResource.new(resource, options)
143
+ serialization.to_json
144
+ serialization.as_json
145
+ ```
146
+
147
+ SerializableResource delegates to the adapter, which it builds as:
148
+
149
+ ```ruby
150
+ adapter_options = {}
151
+ adapter = ActiveModelSerializers::Adapter.create(serializer, adapter_options)
152
+ adapter.to_json
153
+ adapter.as_json
154
+ adapter.serializable_hash
155
+ ```
156
+
157
+ The adapter formats the serializer's attributes and associations (a.k.a. includes):
158
+
159
+ ```ruby
160
+ serializer_options = {}
161
+ serializer = SomeSerializer.new(resource, serializer_options)
162
+ serializer.attributes
163
+ serializer.associations
164
+ ```
165
+
166
+ ## Architecture
167
+
168
+ This section focuses on architecture the 0.10.x version of ActiveModelSerializers. If you are interested in the architecture of the 0.8 or 0.9 versions,
169
+ please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
170
+ [0.9 README](https://github.com/rails-api/active_model_serializers/blob/0-9-stable/README.md).
171
+
172
+ The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
173
+
174
+ ### ActiveModel::Serializer
175
+
176
+ An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
177
+ and exposes an `attributes` method, among a few others.
178
+ It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
179
+ It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
180
+ It may be useful to think of it as a
181
+ [presenter](http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
182
+
183
+ #### ActiveModel::CollectionSerializer
184
+
185
+ The **`ActiveModel::CollectionSerializer`** represents a collection of resources as serializers
186
+ and, if there is no serializer, primitives.
187
+
188
+ ### ActiveModelSerializers::Adapter::Base
189
+
190
+ The **`ActiveModelSerializeres::Adapter::Base`** describes the structure of the JSON document generated from a
191
+ serializer. For example, the `Attributes` example represents each serializer as its
192
+ unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
193
+ API](http://jsonapi.org/) document.
194
+
195
+ ### ActiveModelSerializers::SerializableResource
196
+
197
+ The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
198
+ to an object that responds to `to_json`, and `as_json`. It is used in the controller to
199
+ encapsulate the serialization resource when rendered. However, it can also be used on its own
200
+ to serialize a resource outside of a controller, as well.
201
+
202
+ ### Primitive handling
203
+
204
+ Definitions: A primitive is usually a String or Array. There is no serializer
205
+ defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
206
+ `to_json`). (The below also applies for any object with no serializer.)
207
+
208
+ - ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
209
+
210
+ Internally, if no serializer can be found in the controller, the resource is not decorated by
211
+ ActiveModelSerializers.
212
+
213
+ - However, when a primitive value is an attribute or in a collection, it is not modified.
214
+
215
+ When serializing a collection and the collection serializer (CollectionSerializer) cannot
216
+ identify a serializer for a resource in its collection, it throws [`:no_serializer`](https://github.com/rails-api/active_model_serializers/issues/1191#issuecomment-142327128).
217
+ For example, when caught by `Reflection#build_association`, and the association value is set directly:
218
+
219
+ ```ruby
220
+ reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
221
+ ```
222
+
223
+ (which is called by the adapter as `serializer.associations(*)`.)
224
+
225
+ ### How options are parsed
226
+
227
+ High-level overview:
228
+
229
+ - For a **collection**
230
+ - `:serializer` specifies the collection serializer and
231
+ - `:each_serializer` specifies the serializer for each resource in the collection.
232
+ - For a **single resource**, the `:serializer` option is the resource serializer.
233
+ - Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
234
+ [`ADAPTER_OPTION_KEYS`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/serializable_resource.rb#L5).
235
+ The remaining options are serializer options.
236
+
237
+ Details:
238
+
239
+ 1. **ActionController::Serialization**
240
+ 1. `serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)`
241
+ 1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
242
+ The `adapter_opts` keys are defined in [`ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
243
+ 1. **ActiveModelSerializers::SerializableResource**
244
+ 1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
245
+ - Where `serializer?` is `use_adapter? && !!(serializer)`
246
+ - Where `use_adapter?`: 'True when no explicit adapter given, or explicit value is truthy (non-nil);
247
+ False when explicit adapter is falsy (nil or false)'
248
+ - Where `serializer`:
249
+ 1. from explicit `:serializer` option, else
250
+ 2. implicitly from resource `ActiveModel::Serializer.serializer_for(resource)`
251
+ 1. A side-effect of checking `serializer` is:
252
+ - The `:serializer` option is removed from the serializer_opts hash
253
+ - If the `:each_serializer` option is present, it is removed from the serializer_opts hash and set as the `:serializer` option
254
+ 1. The serializer and adapter are created as
255
+ 1. `serializer_instance = serializer.new(resource, serializer_opts)`
256
+ 2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
257
+ 1. **ActiveModel::Serializer::CollectionSerializer#new**
258
+ 1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
259
+ is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/a54d237e2828fe6bab1ea5dfe6360d4ecc8214cd/lib/active_model/serializer/array_serializer.rb#L14-L16).
260
+ 1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
261
+ resource as defined by the serializer.
262
+
263
+ (In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
264
+ methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
265
+ to know about, but not part of ActiveModelSerializers.)
266
+
267
+ ### What does a 'serializable resource' look like?
268
+
269
+ - An `ActiveRecord::Base` object.
270
+ - Any Ruby object that passes the
271
+ [Lint](http://www.rubydoc.info/github/rails-api/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
272
+ [code](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb).
273
+
274
+ ActiveModelSerializers provides a
275
+ [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb),
276
+ which is a simple serializable PORO (Plain-Old Ruby Object).
277
+
278
+ `ActiveModelSerializers::Model` may be used either as a reference implementation, or in production code.
279
+
280
+ ```ruby
281
+ class MyModel < ActiveModelSerializers::Model
282
+ attributes :id, :name, :level
283
+ end
284
+ ```
285
+
286
+ The default serializer for `MyModel` would be `MyModelSerializer` whether MyModel is an
287
+ ActiveRecord::Base object or not.
288
+
289
+ Outside of the controller the rules are **exactly** the same as for records. For example:
290
+
291
+ ```ruby
292
+ render json: MyModel.new(level: 'awesome'), adapter: :json
293
+ ```
294
+
295
+ would be serialized the same as
296
+
297
+ ```ruby
298
+ ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'), adapter: :json).as_json
299
+ ```
300
+
301
+ ## Semantic Versioning
302
+
303
+ This project adheres to [semver](http://semver.org/)
304
+
305
+ ## Contributing
306
+
307
+ See [CONTRIBUTING.md](CONTRIBUTING.md)