diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..13e1fa7fe --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,29 @@ +#### Expected behavior vs actual behavior + + + +#### Steps to reproduce +*(e.g., detailed walkthrough, runnable script, example application)* + + + +#### Environment + +ActiveModelSerializers Version *(commit ref if not on tag)*: + +Output of `ruby -e "puts RUBY_DESCRIPTION"`: + +OS Type & Version: + +Integrated application and version *(e.g., Rails, Grape, etc)*: + + +#### Backtrace +*(e.g., provide any applicable backtraces from your application)* + + + +#### Additonal helpful information +*(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)* + + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 000000000..f054b00d2 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,15 @@ +#### Purpose + + +#### Changes + + +#### Caveats + + +#### Related GitHub issues + + +#### Additional helpful information + + diff --git a/.gitignore b/.gitignore index 1ecf6e4d1..42303d4bb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,12 +4,15 @@ .config .yardoc Gemfile.lock +Gemfile.local InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg +Vagrantfile +.vagrant rdoc spec/reports test/tmp @@ -17,3 +20,16 @@ test/version_tmp tmp *.swp .ruby-version +.ruby-gemset +vendor/bundle +tags + +# silly macs +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db diff --git a/.travis.yml b/.travis.yml index 78612434c..b48fe6104 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,25 +2,9 @@ language: ruby sudo: false -rvm: - - 1.9.3 - - 2.0.0 - - 2.1.1 - - jruby-19mode - - rbx-2 - - ruby-head +cache: + directories: + - vendor/bundle -install: - - bundle install --retry=3 - -env: - - "RAILS_VERSION=3.2" - - "RAILS_VERSION=4.0" - - "RAILS_VERSION=4.1" - - "RAILS_VERSION=master" - -matrix: - allow_failures: - - rvm: ruby-head - - env: "RAILS_VERSION=master" - - env: "RAILS_VERSION=3.2" +script: + - true diff --git a/CHANGELOG-0-08.md b/CHANGELOG-0-08.md new file mode 100644 index 000000000..eec4e2862 --- /dev/null +++ b/CHANGELOG-0-08.md @@ -0,0 +1,92 @@ +## 0.08.x + +### v0.8.3 (2014/12/10 14:45 +00:00) +- [#753](https://github.com/rails-api/active_model_serializers/pull/753) Test against Ruby 2.2 on Travis CI (@tricknotes) +- [#745](https://github.com/rails-api/active_model_serializers/pull/745) Missing a word (@jockee) + +### v0.8.2 (2014/09/01 21:00 +00:00) +- [#612](https://github.com/rails-api/active_model_serializers/pull/612) Feature/adapter (@bolshakov) + * adds adapters pattern +- [#615](https://github.com/rails-api/active_model_serializers/pull/615) Rails does not support const_defined? in development mode (@tpitale) +- [#613](https://github.com/rails-api/active_model_serializers/pull/613) README: typo fix on attributes (@spk) +- [#614](https://github.com/rails-api/active_model_serializers/pull/614) Fix rails 4.0.x build. (@arthurnn) +- [#610](https://github.com/rails-api/active_model_serializers/pull/610) ArraySerializer (@bolshakov) +- [#607](https://github.com/rails-api/active_model_serializers/pull/607) ruby syntax highlights (@zigomir) +- [#602](https://github.com/rails-api/active_model_serializers/pull/602) Add DSL for associations (@JordanFaust) + +### 0.8.1 (May 6, 2013) + +* Fix bug whereby a serializer using 'options' would blow up. + +### 0.8.0 (May 5, 2013) + +* Attributes can now have optional types. + +* A new DefaultSerializer ensures that POROs behave the same way as ActiveModels. + +* If you wish to override ActiveRecord::Base#to_Json, you can now require + 'active_record/serializer_override'. We don't recommend you do this, but + many users do, so we've left it optional. + +* Fixed a bug where ActionController wouldn't always have MimeResponds. + +* An optinal caching feature allows you to cache JSON & hashes that AMS uses. + Adding 'cached true' to your Serializers will turn on this cache. + +* URL helpers used inside of Engines now work properly. + +* Serializers now can filter attributes with `only` and `except`: + + ``` + UserSerializer.new(user, only: [:first_name, :last_name]) + UserSerializer.new(user, except: :first_name) + ``` + +* Basic Mongoid support. We now include our mixins in the right place. + +* On Ruby 1.8, we now generate an `id` method that properly serializes `id` + columns. See issue #127 for more. + +* Add an alias for `scope` method to be the name of the context. By default + this is `current_user`. The name is automatically set when using + `serialization_scope` in the controller. + +* Pass through serialization options (such as `:include`) when a model + has no serializer defined. + +## [0.7.0 (March 6, 2013)](https://github.com/rails-api/active_model_serializers/commit/fabdc621ff97fbeca317f6301973dd4564b9e695) + +* ```embed_key``` option to allow embedding by attributes other than IDs +* Fix rendering nil with custom serializer +* Fix global ```self.root = false``` +* Add support for specifying the serializer for an association as a String +* Able to specify keys on the attributes method +* Serializer Reloading via ActiveSupport::DescendantsTracker +* Reduce double map to once; Fixes datamapper eager loading. + +## 0.6.0 (October 22, 2012) + +* Serialize sets properly +* Add root option to ArraySerializer +* Support polymorphic associations +* Support :each_serializer in ArraySerializer +* Add `scope` method to easily access the scope in the serializer +* Fix regression with Rails 3.2.6; add Rails 4 support +* Allow serialization_scope to be disabled with serialization_scope nil +* Array serializer should support pure ruby objects besides serializers + +## 0.05.x + +### [0.5.2 (June 5, 2012)](https://github.com/rails-api/active_model_serializers/commit/615afd125c260432d456dc8be845867cf87ea118#diff-0c5c12f311d3b54734fff06069efd2ac) + +### [0.5.1 (May 23, 2012)](https://github.com/rails-api/active_model_serializers/commit/00194ec0e41831802fcbf893a34c0bb0853ebe14#diff-0c5c12f311d3b54734fff06069efd2ac) + +### [0.5.0 (May 16, 2012)](https://github.com/rails-api/active_model_serializers/commit/33d4842dcd35c7167b0b33fc0abcf00fb2c92286) + +* First tagged version +* Changes generators to always generate an ApplicationSerializer + +## [0.1.0 (December 21, 2011)](https://github.com/rails-api/active_model_serializers/commit/1e0c9ef93b96c640381575dcd30be07ac946818b) + +## First Commit as [Rails Serializers 0.0.1](https://github.com/rails-api/active_model_serializers/commit/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e) + (December 1, 2011). diff --git a/CHANGELOG-0-09.md b/CHANGELOG-0-09.md new file mode 100644 index 000000000..a36e8e9b9 --- /dev/null +++ b/CHANGELOG-0-09.md @@ -0,0 +1,74 @@ +## 0.09.x + +### v0.9.3 (2015/01/21 20:29 +00:00) + +Features: +- [#774](https://github.com/rails-api/active_model_serializers/pull/774) Fix nested include attributes (@nhocki) +- [#771](https://github.com/rails-api/active_model_serializers/pull/771) Make linked resource type names consistent with root names (@sweatypitts) +- [#696](https://github.com/rails-api/active_model_serializers/pull/696) Explicitly set serializer for associations (@ggordon) +- [#700](https://github.com/rails-api/active_model_serializers/pull/700) sparse fieldsets (@arenoir) +- [#768](https://github.com/rails-api/active_model_serializers/pull/768) Adds support for `meta` and `meta_key` attribute (@kurko) + +### v0.9.1 (2014/12/04 11:54 +00:00) +- [#707](https://github.com/rails-api/active_model_serializers/pull/707) A Friendly Note on Which AMS Version to Use (@jherdman) +- [#730](https://github.com/rails-api/active_model_serializers/pull/730) Fixes nested has_many links in JSONAPI (@kurko) +- [#718](https://github.com/rails-api/active_model_serializers/pull/718) Allow overriding the adapter with render option (@ggordon) +- [#720](https://github.com/rails-api/active_model_serializers/pull/720) Rename attribute with :key (0.8.x compatibility) (@ggordon) +- [#728](https://github.com/rails-api/active_model_serializers/pull/728) Use type as key for linked resources (@kurko) +- [#729](https://github.com/rails-api/active_model_serializers/pull/729) Use the new beta build env on Travis (@joshk) +- [#703](https://github.com/rails-api/active_model_serializers/pull/703) Support serializer and each_serializer options in renderer (@ggordon, @mieko) +- [#727](https://github.com/rails-api/active_model_serializers/pull/727) Includes links inside of linked resources (@kurko) +- [#726](https://github.com/rails-api/active_model_serializers/pull/726) Bugfix: include nested has_many associations (@kurko) +- [#722](https://github.com/rails-api/active_model_serializers/pull/722) Fix infinite recursion (@ggordon) +- [#1](https://github.com/rails-api/active_model_serializers/pull/1) Allow for the implicit use of ArraySerializer when :each_serializer is specified (@mieko) +- [#692](https://github.com/rails-api/active_model_serializers/pull/692) Include 'linked' member for json-api collections (@ggordon) +- [#714](https://github.com/rails-api/active_model_serializers/pull/714) Define as_json instead of to_json (@guilleiguaran) +- [#710](https://github.com/rails-api/active_model_serializers/pull/710) JSON-API: Don't include linked section if associations are empty (@guilleiguaran) +- [#711](https://github.com/rails-api/active_model_serializers/pull/711) Fixes rbx gems bundling on TravisCI (@kurko) +- [#709](https://github.com/rails-api/active_model_serializers/pull/709) Add type key when association name is different than object type (@guilleiguaran) +- [#708](https://github.com/rails-api/active_model_serializers/pull/708) Handle correctly null associations (@guilleiguaran) +- [#691](https://github.com/rails-api/active_model_serializers/pull/691) Fix embed option for associations (@jacob-s-son) +- [#689](https://github.com/rails-api/active_model_serializers/pull/689) Fix support for custom root in JSON-API adapter (@guilleiguaran) +- [#685](https://github.com/rails-api/active_model_serializers/pull/685) Serialize ids as strings in JSON-API adapter (@guilleiguaran) +- [#684](https://github.com/rails-api/active_model_serializers/pull/684) Refactor adapters to implement support for array serialization (@guilleiguaran) +- [#682](https://github.com/rails-api/active_model_serializers/pull/682) Include root by default in JSON-API serializers (@guilleiguaran) +- [#625](https://github.com/rails-api/active_model_serializers/pull/625) Add DSL for urls (@JordanFaust) +- [#677](https://github.com/rails-api/active_model_serializers/pull/677) Add support for embed: :ids option for in associations (@guilleiguaran) +- [#681](https://github.com/rails-api/active_model_serializers/pull/681) Check superclasses for Serializers (@quainjn) +- [#680](https://github.com/rails-api/active_model_serializers/pull/680) Add support for root keys (@NullVoxPopuli) +- [#675](https://github.com/rails-api/active_model_serializers/pull/675) Support Rails 4.2.0 (@tricknotes) +- [#667](https://github.com/rails-api/active_model_serializers/pull/667) Require only activemodel instead of full rails (@guilleiguaran) +- [#653](https://github.com/rails-api/active_model_serializers/pull/653) Add "_test" suffix to JsonApi::HasManyTest filename. (@alexgenco) +- [#631](https://github.com/rails-api/active_model_serializers/pull/631) Update build badge URL (@craiglittle) + +### 0.9.0.alpha1 - January 7, 2014 + +### 0.9.0.pre + +* The following methods were removed + - Model#active\_model\_serializer + - Serializer#include! + - Serializer#include? + - Serializer#attr\_disabled= + - Serializer#cache + - Serializer#perform\_caching + - Serializer#schema (needs more discussion) + - Serializer#attribute + - Serializer#include\_#{name}? (filter method added) + - Serializer#attributes (took a hash) + +* The following things were added + - Serializer#filter method + - CONFIG object + +* Remove support for ruby 1.8 versions. + +* Require rails >= 3.2. + +* Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces. + +* Added a "prefix" option in case you want to use a different version of serializer. + +* Serializers default namespace can be set in `default_serializer_options` and inherited by associations. + +* [Beginning of rewrite: c65d387705ec534db171712671ba7fcda4f49f68](https://github.com/rails-api/active_model_serializers/commit/c65d387705ec534db171712671ba7fcda4f49f68) diff --git a/CHANGELOG-0-10.md b/CHANGELOG-0-10.md new file mode 100644 index 000000000..81e3505cc --- /dev/null +++ b/CHANGELOG-0-10.md @@ -0,0 +1,466 @@ +## 0.10.x + +### [0-10-stable (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...0-10-stable) + +Breaking changes: + +Features: + +Fixes: + +Misc: + +### [v0.10.6 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...v0.10.6) + +Fixes: + +- [#1857](https://github.com/rails-api/active_model_serializers/pull/1857) JSON:API does not load belongs_to relation to get identifier id. (@bf4) +- [#2119](https://github.com/rails-api/active_model_serializers/pull/2119) JSON:API returns null resource object identifier when 'id' is null. (@bf4) +- [#2093](https://github.com/rails-api/active_model_serializers/pull/2093) undef problematic Serializer methods: display, select. (@bf4) + +Misc: + +- [#2104](https://github.com/rails-api/active_model_serializers/pull/2104) Documentation for serializers and rendering. (@cassidycodes) +- [#2081](https://github.com/rails-api/active_model_serializers/pull/2081) Documentation for `include` option in adapters. (@charlie-wasp) +- [#2120](https://github.com/rails-api/active_model_serializers/pull/2120) Documentation for association options: foreign_key, type, class_name, namespace. (@bf4) + +### [v0.10.5 (2017-03-07)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...v0.10.5) + +Breaking changes: + +Features: + +- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4) +- [#2057](https://github.com/rails-api/active_model_serializers/pull/2057) + Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.2']` + (@jaredbeck) + +Fixes: + +- [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4) + +Misc: + +- [#2055](https://github.com/rails-api/active_model_serializers/pull/2055) + Replace deprecated dependency jsonapi with jsonapi-renderer. (@jaredbeck) +- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) Make test attributes explicit. Tests have Model#associations. (@bf4) +- [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh) +- [#2035](https://github.com/rails-api/active_model_serializers/pull/2035) Document how to disable the logger. (@MSathieu) +- [#2039](https://github.com/rails-api/active_model_serializers/pull/2039) Documentation fixes. (@biow0lf) + +### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4) + +Misc: + +- [#2005](https://github.com/rails-api/active_model_serializers/pull/2005) Update jsonapi runtime dependency to 0.1.1.beta6, support Ruby 2.4. (@kofronpi) +- [#1993](https://github.com/rails-api/active_model_serializers/pull/1993) Swap out KeyTransform for CaseTransform gem for the possibility of native extension use. (@NullVoxPopuli) + +### [v0.10.3 (2016-11-21)](https://github.com/rails-api/active_model_serializers/compare/v0.10.2...v0.10.3) + +Fixes: + +- [#1973](https://github.com/rails-api/active_model_serializers/pull/1973) Fix namespace lookup for collections and has_many relationships (@groyoh) +- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87) +- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj) +- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino) +- [#1930](https://github.com/rails-api/active_model_serializers/pull/1930) Ensure valid jsonapi when relationship has no links or data (@richmolj) + +Features: + +- [#1757](https://github.com/rails-api/active_model_serializers/pull/1757) Make serializer lookup chain configurable. (@NullVoxPopuli) +- [#1968](https://github.com/rails-api/active_model_serializers/pull/1968) (@NullVoxPopuli) + - Add controller namespace to default controller lookup + - Provide a `namespace` render option + - document how set the namespace in the controller for implicit lookup. +- [#1791](https://github.com/rails-api/active_model_serializers/pull/1791) (@bf4, @youroff, @NullVoxPopuli) + - Added `jsonapi_namespace_separator` config option. +- [#1889](https://github.com/rails-api/active_model_serializers/pull/1889) Support key transformation for Attributes adapter (@iancanderson, @danbee) +- [#1917](https://github.com/rails-api/active_model_serializers/pull/1917) Add `jsonapi_pagination_links_enabled` configuration option (@richmolj) +- [#1797](https://github.com/rails-api/active_model_serializers/pull/1797) Only include 'relationships' when sideloading (@richmolj) + +Fixes: + +- [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh) +- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene) + +Misc: +- [#1767](https://github.com/rails-api/active_model_serializers/pull/1767) Replace raising/rescuing `CollectionSerializer::NoSerializerError`, + throw/catch `:no_serializer`. (@bf4) +- [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli) +- [#1812](https://github.com/rails-api/active_model_serializers/pull/1812) add a code of conduct (@corainchicago) + +- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz) + +- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@cassidycodes) +- [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli) +- [#1959](https://github.com/rails-api/active_model_serializers/pull/1959) Add documentation for root. (@shunsuke227ono) +- [#1967](https://github.com/rails-api/active_model_serializers/pull/1967) Improve type method documentation. (@yukideluxe) + +### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2) + +Fixes: +- [#1814](https://github.com/rails-api/active_model_serializers/pull/1814) Ensuring read_multi works with fragment cache +- [#1848](https://github.com/rails-api/active_model_serializers/pull/1848) Redefine associations on inherited serializers. (@EhsanYousefi) + +Misc: +- [#1808](https://github.com/rails-api/active_model_serializers/pull/1808) Adds documentation for `fields` option. (@luizkowalski) + +### [v0.10.1 (2016-06-16)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0...v0.10.1) + +Features: +- [#1668](https://github.com/rails-api/active_model_serializers/pull/1668) Exclude nil and empty links. (@sigmike) +- [#1426](https://github.com/rails-api/active_model_serializers/pull/1426) Add ActiveModelSerializers.config.default_includes (@empact) + +Fixes: +- [#1754](https://github.com/rails-api/active_model_serializers/pull/1754) Fixes #1759, Grape integration, improves serialization_context + missing error message on pagination. Document overriding CollectionSerializer#paginated?. (@bf4) + Moved serialization_context creation to Grape formatter, so resource serialization works without explicit calls to the `render` helper method. + Added Grape collection tests. (@onomated) +- [#1287](https://github.com/rails-api/active_model_serializers/pull/1287) Pass `fields` options from adapter to serializer. (@vasilakisfil) +- [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option + is set to `false`. (@groyoh) +- [#1747](https://github.com/rails-api/active_model_serializers/pull/1747) Improve jsonapi mime type registration for Rails 5 (@remear) + +Misc: +- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2) +- [#1685](https://github.com/rails-api/active_model_serializers/pull/1685) Replace `IncludeTree` with `IncludeDirective` from the jsonapi gem. + +### [v0.10.0 (2016-05-17)](https://github.com/rails-api/active_model_serializers/compare/4a2d9853ba7...v0.10.0) + +Breaking changes: +- [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear) + +Features: +- [#1677](https://github.com/rails-api/active_model_serializers/pull/1677) Add `assert_schema`, `assert_request_schema`, `assert_request_response_schema`. (@bf4) +- [#1697](https://github.com/rails-api/active_model_serializers/pull/1697) Include actual exception message with custom exceptions; + `Test::Schema` exceptions are now `Minitest::Assertion`s. (@bf4) +- [#1699](https://github.com/rails-api/active_model_serializers/pull/1699) String/Lambda support for conditional attributes/associations (@mtsmfm) +- [#1687](https://github.com/rails-api/active_model_serializers/pull/1687) Only calculate `_cache_digest` (in `cache_key`) when `skip_digest` is false. (@bf4) +- [#1647](https://github.com/rails-api/active_model_serializers/pull/1647) Restrict usage of `serializable_hash` options + to the ActiveModel::Serialization and ActiveModel::Serializers::JSON interface. (@bf4) + +Fixes: +- [#1700](https://github.com/rails-api/active_model_serializers/pull/1700) Support pagination link for Kaminari when no data is returned. (@iamnader) +- [#1726](https://github.com/rails-api/active_model_serializers/pull/1726) Adds polymorphic option to association definition which includes association type/nesting in serializer (@cgmckeever) + +Misc: +- [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre) +- [#1730](https://github.com/rails-api/active_model_serializers/pull/1730) Adds documentation for overriding default serializer based on conditions (@groyoh/@cgmckeever) + +### [v0.10.0.rc5 (2016-04-04)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc4...v0.10.0.rc5) + +Breaking changes: + +- [#1645](https://github.com/rails-api/active_model_serializers/pull/1645) Changed :dashed key transform to :dash. (@remear) +- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Default key case for the JsonApi adapter changed to dashed. (@remear) + +Features: +- [#1645](https://github.com/rails-api/active_model_serializers/pull/1645) Transform keys referenced in values. (@remear) +- [#1650](https://github.com/rails-api/active_model_serializers/pull/1650) Fix serialization scope options `scope`, `scope_name` + take precedence over `serialization_scope` in the controller. + Fix tests that required tearing down dynamic methods. (@bf4) +- [#1644](https://github.com/rails-api/active_model_serializers/pull/1644) Include adapter name in cache key so + that the same serializer can be cached per adapter. (@bf4 via #1346 by @kevintyll) +- [#1642](https://github.com/rails-api/active_model_serializers/pull/1642) Prefer object.cache_key over the generated + cache key. (@bf4 via #1346 by @kevintyll) +- [#1637](https://github.com/rails-api/active_model_serializers/pull/1637) Make references to 'ActionController::Base.cache_store' explicit + in order to avoid issues when application controllers inherit from 'ActionController::API'. (@ncuesta) +- [#1633](https://github.com/rails-api/active_model_serializers/pull/1633) Yield 'serializer' to serializer association blocks. (@bf4) +- [#1616](https://github.com/rails-api/active_model_serializers/pull/1616) SerializableResource handles no serializer like controller. (@bf4) +- [#1618](https://github.com/rails-api/active_model_serializers/issues/1618) Get collection root key for + empty collection from explicit serializer option, when possible. (@bf4) +- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Provide key translation. (@remear) +- [#1494](https://github.com/rails-api/active_model_serializers/pull/1494) Make serializers serializalbe + (using the Attributes adapter by default). (@bf4) +- [#1550](https://github.com/rails-api/active_model_serializers/pull/1550) Add + Rails url_helpers to `SerializationContext` for use in links. (@remear, @bf4) +- [#1004](https://github.com/rails-api/active_model_serializers/pull/1004) JSON API errors object implementation. + - Only implements `detail` and `source` as derived from `ActiveModel::Error` + - Provides checklist of remaining questions and remaining parts of the spec. +- [#1515](https://github.com/rails-api/active_model_serializers/pull/1515) Adds support for symbols to the + `ActiveModel::Serializer.type` method. (@groyoh) +- [#1504](https://github.com/rails-api/active_model_serializers/pull/1504) Adds the changes missing from #1454 + and add more tests for resource identifier and relationship objects. Fix association block with link + returning `data: nil`.(@groyoh) +- [#1372](https://github.com/rails-api/active_model_serializers/pull/1372) Support + cache_store.read_multi. (@LcpMarvel) +- [#1018](https://github.com/rails-api/active_model_serializers/pull/1018) Add more tests and docs for top-level links. (@leandrocp) +- [#1454](https://github.com/rails-api/active_model_serializers/pull/1454) Add support for + relationship-level links and meta attributes. (@beauby) +- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby) + +Fixes: +- [#1657](https://github.com/rails-api/active_model_serializers/pull/1657) Add missing missing require "active_support/json". (@andreaseger) +- [#1661](https://github.com/rails-api/active_model_serializers/pull/1661) Fixes `read_attribute_for_serialization` not + seeing methods defined in serialization superclass (#1653, #1658, #1660), introduced in #1650. (@bf4) +- [#1651](https://github.com/rails-api/active_model_serializers/pull/1651) Fix deserialization of nil relationships. (@NullVoxPopuli) +- [#1480](https://github.com/rails-api/active_model_serializers/pull/1480) Fix setting of cache_store from Rails configuration. (@bf4) + Fix unintentional mutating of value in memory cache store. (@groyoh) +- [#1622](https://github.com/rails-api/active_model_serializers/pull/1622) Fragment cache changed from per-record to per-serializer. + Now, two serializers that use the same model may be separately cached. (@lserman) +- [#1478](https://github.com/rails-api/active_model_serializers/pull/1478) Cache store will now be correctly set when serializers are + loaded *before* Rails initializes. (@bf4) +- [#1570](https://github.com/rails-api/active_model_serializers/pull/1570) Fixed pagination issue with last page size. (@bmorrall) +- [#1516](https://github.com/rails-api/active_model_serializers/pull/1516) No longer return a nil href when only + adding meta to a relationship link. (@groyoh) +- [#1458](https://github.com/rails-api/active_model_serializers/pull/1458) Preserve the serializer + type when fragment caching. (@bdmac) +- [#1477](https://github.com/rails-api/active_model_serializers/pull/1477) Fix `fragment_cached?` + method to check if caching. (@bdmac) +- [#1501](https://github.com/rails-api/active_model_serializers/pull/1501) Adds tests for SerializableResource::use_adapter?,doc typos (@domitian) +- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00) + +Misc: +- [#1608](https://github.com/rails-api/active_model_serializers/pull/1608) Move SerializableResource to ActiveModelSerializers (@groyoh) +- [#1602](https://github.com/rails-api/active_model_serializers/pull/1602) Add output examples to Adapters docs (@remear) +- [#1557](https://github.com/rails-api/active_model_serializers/pull/1557) Update docs regarding overriding the root key (@Jwan622) +- [#1471](https://github.com/rails-api/active_model_serializers/pull/1471) [Cleanup] Serializer caching is its own concern. (@bf4) +- [#1482](https://github.com/rails-api/active_model_serializers/pull/1482) Document JSON API implementation defs and progress in class. (@bf4) +- [#1551](https://github.com/rails-api/active_model_serializers/pull/1551) Added codebeat badge (@korzonek) +- [#1527](https://github.com/rails-api/active_model_serializers/pull/1527) Refactor fragment cache class. (@groyoh) +- [#1560](https://github.com/rails-api/active_model_serializers/pull/1560) Update rubocop and address its warnings. (@bf4 @groyoh) +- [#1545](https://github.com/rails-api/active_model_serializers/pull/1545) Document how to pass arbitrary options to the + serializer (@CodedBeardedSignedTaylor) +- [#1496](https://github.com/rails-api/active_model_serializers/pull/1496) Run all branches against JRuby on CI (@nadavshatz) +- [#1559](https://github.com/rails-api/active_model_serializers/pull/1559) Add a deprecation DSL. (@bf4 @groyoh) +- [#1543](https://github.com/rails-api/active_model_serializers/pull/1543) Add the changes missing from #1535. (@groyoh) +- [#1535](https://github.com/rails-api/active_model_serializers/pull/1535) Move the adapter and adapter folder to + active_model_serializers folder and changes the module namespace. (@domitian @bf4) +- [#1497](https://github.com/rails-api/active_model_serializers/pull/1497) Add JRuby-9000 to appveyor.yml(@corainchicago) +- [#1420](https://github.com/rails-api/active_model_serializers/pull/1420) Adds tests and documentation for polymorphism(@marcgarreau) + + +### [v0.10.0.rc4 (2016-01-27)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc3...v0.10.0.rc4) +Breaking changes: + +- [#1360](https://github.com/rails-api/active_model_serializers/pull/1360) + [#1369](https://github.com/rails-api/active_model_serializers/pull/1369) Drop support for Ruby 1.9.3 (@karaAJC, @maurogeorge) +- [#1131](https://github.com/rails-api/active_model_serializers/pull/1131) Remove Serializer#root_name (@beauby) +- [#1138](https://github.com/rails-api/active_model_serializers/pull/1138) Introduce Adapter::Base (@bf4) + * Adapters now inherit Adapter::Base. 'Adapter' is now a module, no longer a class. + * using a class as a namespace that you also inherit from is complicated and circular at times i.e. + buggy (see https://github.com/rails-api/active_model_serializers/pull/1177) + * The class methods on Adapter aren't necessarily related to the instance methods, they're more + Adapter functions. + * named `Base` because it's a Rails-ism. + * It helps to isolate and highlight what the Adapter interface actually is. +- [#1418](https://github.com/rails-api/active_model_serializers/pull/1418) + serialized collections now use the root option as is; now, only the + root derived from the serializer or object is always pluralized. + +Features: + +- [#1406](https://github.com/rails-api/active_model_serializers/pull/1406) Allow for custom dynamic values in JSON API links (@beauby) +- [#1270](https://github.com/rails-api/active_model_serializers/pull/1270) Adds `assert_response_schema` test helper (@maurogeorge) +- [#1099](https://github.com/rails-api/active_model_serializers/pull/1099) Adds `assert_serializer` test helper (@maurogeorge) +- [#1403](https://github.com/rails-api/active_model_serializers/pull/1403) Add support for if/unless on attributes/associations (@beauby) +- [#1248](https://github.com/rails-api/active_model_serializers/pull/1248) Experimental: Add support for JSON API deserialization (@beauby) +- [#1378](https://github.com/rails-api/active_model_serializers/pull/1378) Change association blocks + to be evaluated in *serializer* scope, rather than *association* scope. (@bf4) + * Syntax changes from e.g. + `has_many :titles do customers.pluck(:title) end` (in #1356) to + `has_many :titles do object.customers.pluck(:title) end` +- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for + attributes and associations (@bf4 @beauby @noahsilas) + * Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute + :title do 'Mr. Topum Hat' end` + * Allows defining associations so that they don't conflict with existing methods. e.g. `has_many + :titles do customers.pluck(:title) end` + * Allows dynamic associations, as compared to compare to using + [`virtual_value`](https://github.com/rails-api/active_model_serializers/pull/1356#discussion_r47146466). + e.g. `has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]` + * Removes dynamically defined methods on the serializer +- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0 (@johnhamelink) +- [#1322](https://github.com/rails-api/active_model_serializers/pull/1322) Instrumenting rendering of resources (@bf4, @maurogeorge) +- [#1291](https://github.com/rails-api/active_model_serializers/pull/1291) Add logging (@maurogeorge) +- [#1272](https://github.com/rails-api/active_model_serializers/pull/1272) Add PORO serializable base class: ActiveModelSerializers::Model (@bf4) +- [#1255](https://github.com/rails-api/active_model_serializers/pull/1255) Make more class attributes inheritable (@bf4) +- [#1249](https://github.com/rails-api/active_model_serializers/pull/1249) Inheritance of serializer inheriting the cache configuration(@Rodrigora) +- [#1247](https://github.com/rails-api/active_model_serializers/pull/1247) Add support for toplevel JSON API links (@beauby) +- [#1246](https://github.com/rails-api/active_model_serializers/pull/1246) Add support for resource-level JSON API links (@beauby) +- [#1225](https://github.com/rails-api/active_model_serializers/pull/1225) Better serializer lookup, use nested serializer when it exists (@beauby) +- [#1213](https://github.com/rails-api/active_model_serializers/pull/1213) `type` directive for serializer to control type field with json-api adapter (@youroff) +- [#1172](https://github.com/rails-api/active_model_serializers/pull/1172) Better serializer registration, get more than just the first module (@bf4) +- [#1158](https://github.com/rails-api/active_model_serializers/pull/1158) Add support for wildcards in `include` option (@beauby) +- [#1127](https://github.com/rails-api/active_model_serializers/pull/1127) Add support for nested + associations for JSON and Attributes adapters via the `include` option (@NullVoxPopuli, @beauby). +- [#1050](https://github.com/rails-api/active_model_serializers/pull/1050) Add support for toplevel jsonapi member (@beauby, @bf4) +- [#1251](https://github.com/rails-api/active_model_serializers/pull/1251) Rename ArraySerializer to + CollectionSerializer for clarity, add ActiveModelSerializers.config.collection_serializer (@bf4) +- [#1295](https://github.com/rails-api/active_model_serializers/pull/1295) Add config `serializer_lookup_enabled` that, + when disabled, requires serializers to explicitly specified. (@trek) + +Fixes: + +- [#1352](https://github.com/rails-api/active_model_serializers/pull/1352) Fix generators; Isolate Rails-specifc code in Railties (@dgynn, @bf4) +- [#1384](https://github.com/rails-api/active_model_serializers/pull/1384)Fix database state leaking across tests (@bf4) +- [#1297](https://github.com/rails-api/active_model_serializers/pull/1297) Fix `fields` option to restrict relationships as well (@beauby) +- [#1239](https://github.com/rails-api/active_model_serializers/pull/1239) Fix duplicates in JSON API compound documents (@beauby) +- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8) +- [#1358](https://github.com/rails-api/active_model_serializers/pull/1358) Handle serializer file paths with spaces (@rwstauner, @bf4) +- [#1195](https://github.com/rails-api/active_model_serializers/pull/1195) Fix id override (@beauby) +- [#1185](https://github.com/rails-api/active_model_serializers/pull/1185) Fix options passing in Json and Attributes adapters (@beauby) + +Misc: + +- [#1383](https://github.com/rails-api/active_model_serializers/pull/1383) Simplify reflections handling (@beauby) +- [#1370](https://github.com/rails-api/active_model_serializers/pull/1370) Simplify attributes handling via a mixin (@beauby) +- [#1301](https://github.com/rails-api/active_model_serializers/pull/1301) Mapping JSON API spec / schema to AMS (@bf4) +- [#1271](https://github.com/rails-api/active_model_serializers/pull/1271) Handle no serializer source file to digest (@bf4) +- [#1260](https://github.com/rails-api/active_model_serializers/pull/1260) Serialization and Cache Documentation (@bf4) +- [#1259](https://github.com/rails-api/active_model_serializers/pull/1259) Add more info to CONTRIBUTING (@bf4) +- [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby) +- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby) +- [#1220](https://github.com/rails-api/active_model_serializers/pull/1220) Remove empty rubocop.rake (@maurogeorge) +- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4) +- [#1177](https://github.com/rails-api/active_model_serializers/pull/1177) Remove Adapter autoloads in favor of require (@bf4) +- [#1117](https://github.com/rails-api/active_model_serializers/pull/1117) FlattenJson adapter no longer inherits Json adapter, renamed to Attributes (@bf4) +- [#1171](https://github.com/rails-api/active_model_serializers/pull/1171) add require statements to top of file (@shicholas) +- [#1167](https://github.com/rails-api/active_model_serializers/pull/1167) Delegate Serializer.attributes to Serializer.attribute (@bf4) +- [#1174](https://github.com/rails-api/active_model_serializers/pull/1174) Consistently refer to the 'JSON API' and the 'JsonApi' adapter (@bf4) +- [#1173](https://github.com/rails-api/active_model_serializers/pull/1173) Comment private accessor warnings (@bf4) +- [#1166](https://github.com/rails-api/active_model_serializers/pull/1166) Prefer methods over instance variables (@bf4) +- [#1168](https://github.com/rails-api/active_model_serializers/pull/1168) Fix appveyor failure cache not being expired (@bf4) +- [#1161](https://github.com/rails-api/active_model_serializers/pull/1161) Remove duplicate test helper (@bf4) +- [#1360](https://github.com/rails-api/active_model_serializers/pull/1360) Update CI to test 2.2.2 -> 2.2.3 (@karaAJC) +- [#1371](https://github.com/rails-api/active_model_serializers/pull/1371) Refactor, update, create documentation (@bf4) + +### [v0.10.0.rc3 (2015-09-16)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc2...v0.10.0.rc3) +- [#1129](https://github.com/rails-api/active_model_serializers/pull/1129) Remove SerializableResource.serialize in favor of `.new` (@bf4) +- [#1155](https://github.com/rails-api/active_model_serializers/pull/1155) Outside controller use tutorial (@CodedBeardedSignedTaylor) +- [#1154](https://github.com/rails-api/active_model_serializers/pull/1154) Rubocop fixes for issues introduced by #1089 (@NullVoxPopuli) +- [#1089](https://github.com/rails-api/active_model_serializers/pull/1089) Add ActiveModelSerializers.logger with default null device (@bf4) +- [#1109](https://github.com/rails-api/active_model_serializers/pull/1109) Make better use of Minitest's lifecycle (@bf4) +- [#1144](https://github.com/rails-api/active_model_serializers/pull/1144) Fix Markdown to adapters documentation (@bacarini) +- [#1121](https://github.com/rails-api/active_model_serializers/pull/1121) Refactor `add_links` in JSONAPI adapter. (@beauby) +- [#1150](https://github.com/rails-api/active_model_serializers/pull/1150) Remove legacy method accidentally reintroduced in #1017 (@beauby) +- [#1149](https://github.com/rails-api/active_model_serializers/pull/1149) Update README with nested included association example. (@mattmueller) +- [#1110](https://github.com/rails-api/active_model_serializers/pull/1110) Add lint tests for AR models (@beauby) +- [#1131](https://github.com/rails-api/active_model_serializers/pull/1131) Extended format for JSONAPI `include` option (@beauby) + * adds extended format for `include` option to JsonApi adapter +- [#1142](https://github.com/rails-api/active_model_serializers/pull/1142) Updating wording on cache expiry in README (@leighhalliday) +- [#1140](https://github.com/rails-api/active_model_serializers/pull/1140) Fix typo in fieldset exception (@lautis) +- [#1132](https://github.com/rails-api/active_model_serializers/pull/1132) Get rid of unnecessary instance variables, and implied dependencies. (@beauby) +- [#1139](https://github.com/rails-api/active_model_serializers/pull/1139) Documentation for serializing resources without render (@PericlesTheo) +- [#1017](https://github.com/rails-api/active_model_serializers/pull/1017) Make Adapters registerable so they are not namespace-constrained (@bf4) +- [#1120](https://github.com/rails-api/active_model_serializers/pull/1120) Add windows platform to loading sqlite3 (@Eric-Guo) +- [#1123](https://github.com/rails-api/active_model_serializers/pull/1123) Remove url options (@bacarini) +- [#1093](https://github.com/rails-api/active_model_serializers/pull/1093) Factor `with_adapter` + force cache clear before each test. (@beauby) +- [#1095](https://github.com/rails-api/active_model_serializers/pull/1095) Add documentation about configuration options. (@beauby) +- [#1069](https://github.com/rails-api/active_model_serializers/pull/1069) Add test coverage; account for no artifacts on CI (@bf4) +- [#1103](https://github.com/rails-api/active_model_serializers/pull/1103) Move `id` and `json_api_type` methods from `Serializer` to `JsonApi`. (@beauby) +- [#1106](https://github.com/rails-api/active_model_serializers/pull/1106) Add Style enforcer (via Rubocop) (@bf4) +- [#1079](https://github.com/rails-api/active_model_serializers/pull/1079) Add ArraySerializer#object like Serializer (@bf4) +- [#1096](https://github.com/rails-api/active_model_serializers/pull/1096) Fix definition of serializer attributes with multiple calls to `attri… (@beauby) +- [#1105](https://github.com/rails-api/active_model_serializers/pull/1105) Add ActiveRecord-backed fixtures. (@beauby) +- [#1108](https://github.com/rails-api/active_model_serializers/pull/1108) Better lint (@bf4) +- [#1102](https://github.com/rails-api/active_model_serializers/pull/1102) Remove remains of `embed` option. (@beauby) +- [#1090](https://github.com/rails-api/active_model_serializers/pull/1090) Clarify AMS dependencies (@bf4) +- [#1081](https://github.com/rails-api/active_model_serializers/pull/1081) Add configuration option to set resource type to singular/plural (@beauby) +- [#1067](https://github.com/rails-api/active_model_serializers/pull/1067) Fix warnings (@bf4) +- [#1066](https://github.com/rails-api/active_model_serializers/pull/1066) Adding appveyor to the project (@joaomdmoura, @Eric-Guo, @bf4) +- [#1071](https://github.com/rails-api/active_model_serializers/pull/1071) Make testing suite running and pass in Windows (@Eric-Guo, @bf4) +- [#1041](https://github.com/rails-api/active_model_serializers/pull/1041) Adding pagination links (@bacarini) + * adds support for `pagination links` at top level of JsonApi adapter +- [#1063](https://github.com/rails-api/active_model_serializers/pull/1063) Lead by example: lint PORO model (@bf4) +- [#1](https://github.com/rails-api/active_model_serializers/pull/1) Test caller line parsing and digesting (@bf4) +- [#1048](https://github.com/rails-api/active_model_serializers/pull/1048) Let FlattenJson adapter decide it doesn't include meta (@bf4) +- [#1060](https://github.com/rails-api/active_model_serializers/pull/1060) Update fragment cache to support namespaced objects (@aaronlerch) +- [#1052](https://github.com/rails-api/active_model_serializers/pull/1052) Use underscored json_root when serializing a collection (@whatthewhat) +- [#1051](https://github.com/rails-api/active_model_serializers/pull/1051) Fix some invalid JSON in docs (@tjschuck) +- [#1049](https://github.com/rails-api/active_model_serializers/pull/1049) Fix incorrect s/options = {}/options ||= {} (@bf4) +- [#1037](https://github.com/rails-api/active_model_serializers/pull/1037) allow for type attribute (@lanej) +- [#1034](https://github.com/rails-api/active_model_serializers/pull/1034) allow id attribute to be overriden (@lanej) +- [#1035](https://github.com/rails-api/active_model_serializers/pull/1035) Fixed Comments highlight (@artLopez) +- [#1031](https://github.com/rails-api/active_model_serializers/pull/1031) Disallow to define multiple associations at once (@bolshakov) +- [#1032](https://github.com/rails-api/active_model_serializers/pull/1032) Wrap railtie requirement with rescue (@elliotlarson) +- [#1026](https://github.com/rails-api/active_model_serializers/pull/1026) Bump Version Number to 0.10.0.rc2 (@jfelchner) +- [#985](https://github.com/rails-api/active_model_serializers/pull/985) Associations implementation refactoring (@bolshakov) +- [#954](https://github.com/rails-api/active_model_serializers/pull/954) Encapsulate serialization in ActiveModel::SerializableResource (@bf4) +- [#972](https://github.com/rails-api/active_model_serializers/pull/972) Capture app warnings on test run (@bf4) +- [#1019](https://github.com/rails-api/active_model_serializers/pull/1019) Improve README.md (@baojjeu) +- [#998](https://github.com/rails-api/active_model_serializers/pull/998) Changing root to model class name (@joaomdmoura) +- [#1006](https://github.com/rails-api/active_model_serializers/pull/1006) Fix adapter inflection bug for api -> API (@bf4) +- [#1016](https://github.com/rails-api/active_model_serializers/pull/1016) require rails/railtie before subclassing Rails::Railtie (@bf4) +- [#1013](https://github.com/rails-api/active_model_serializers/pull/1013) Root option with empty array support (@vyrak, @mareczek) +- [#994](https://github.com/rails-api/active_model_serializers/pull/994) Starting Docs structure (@joaomdmoura) +- [#1007](https://github.com/rails-api/active_model_serializers/pull/1007) Bug fix for ArraySerializer json_key (@jiajiawang) +- [#1003](https://github.com/rails-api/active_model_serializers/pull/1003) Fix transient test failures (@Rodrigora) +- [#996](https://github.com/rails-api/active_model_serializers/pull/996) Add linter for serializable resource (@bf4) +- [#990](https://github.com/rails-api/active_model_serializers/pull/990) Adding json-api meta test (@joaomdmoura) +- [#984](https://github.com/rails-api/active_model_serializers/pull/984) Add option "key" to serializer associations (@Rodrigora) +- [#982](https://github.com/rails-api/active_model_serializers/pull/982) Fix typo (@bf4) +- [#981](https://github.com/rails-api/active_model_serializers/pull/981) Remove unused PORO#to_param (@bf4) +- [#978](https://github.com/rails-api/active_model_serializers/pull/978) fix generators template bug (@regonn) +- [#975](https://github.com/rails-api/active_model_serializers/pull/975) Fixes virtual value not being used (@GriffinHeart) +- [#970](https://github.com/rails-api/active_model_serializers/pull/970) Fix transient tests failures (@Rodrigora) +- [#962](https://github.com/rails-api/active_model_serializers/pull/962) Rendering objects that doesn't have serializers (@bf4, @joaomdmoura, @JustinAiken) +- [#939](https://github.com/rails-api/active_model_serializers/pull/939) Use a more precise generated cache key (@aaronlerch) +- [#971](https://github.com/rails-api/active_model_serializers/pull/971) Restore has_one to generator (@bf4) +- [#965](https://github.com/rails-api/active_model_serializers/pull/965) options fedault valueserializable_hash and as_json (@bf4) +- [#959](https://github.com/rails-api/active_model_serializers/pull/959) TYPO on README.md (@kangkyu) + +### [v0.10.0.rc2 (2015-06-16)](https://github.com/rails-api/active_model_serializers/compare/v0.10.0.rc1...v0.10.0.rc2) +- [#958](https://github.com/rails-api/active_model_serializers/pull/958) Splitting json adapter into two (@joaomdmoura) + * adds FlattenJSON as default adapter +- [#953](https://github.com/rails-api/active_model_serializers/pull/953) use model name to determine the type (@lsylvester) + * uses model name to determine the type +- [#949](https://github.com/rails-api/active_model_serializers/pull/949) Don't pass serializer option to associated serializers (@bf4, @edwardloveall) +- [#902](https://github.com/rails-api/active_model_serializers/pull/902) Added serializer file digest to the cache_key (@cristianbica) +- [#948](https://github.com/rails-api/active_model_serializers/pull/948) AMS supports JSONAPI 1.0 instead of RC4 (@SeyZ) +- [#936](https://github.com/rails-api/active_model_serializers/pull/936) Include meta when using json adapter with custom root (@chrisbranson) +- [#942](https://github.com/rails-api/active_model_serializers/pull/942) Small code styling issue (@thiagofm) +- [#930](https://github.com/rails-api/active_model_serializers/pull/930) Reverting PR #909 (@joaomdmoura) +- [#924](https://github.com/rails-api/active_model_serializers/pull/924) Avoid unnecessary calls to attribute methods when fragment caching (@navinpeiris) +- [#925](https://github.com/rails-api/active_model_serializers/pull/925) Updates JSON API Adapter to generate RC4 schema (@benedikt) + * adds JSON API support 1.0 +- [#918](https://github.com/rails-api/active_model_serializers/pull/918) Adding rescue_with_handler to clear state (@ryansch) +- [#909](https://github.com/rails-api/active_model_serializers/pull/909) Defining Json-API Adapter as Default (@joaomdmoura) + * remove root key option and split JSON adapter +- [#914](https://github.com/rails-api/active_model_serializers/pull/914) Prevent possible duplicated attributes in serializer (@groyoh) +- [#880](https://github.com/rails-api/active_model_serializers/pull/880) Inabling subclasses serializers to inherit attributes (@groyoh) +- [#913](https://github.com/rails-api/active_model_serializers/pull/913) Avoiding the serializer option when instantiating a new one for ArraySerializer Fixed #911 (@groyoh) +- [#897](https://github.com/rails-api/active_model_serializers/pull/897) Allow to define custom serializer for given class (@imanel) +- [#892](https://github.com/rails-api/active_model_serializers/pull/892) Fixed a bug that appeared when json adapter serialize a nil association (@groyoh) +- [#895](https://github.com/rails-api/active_model_serializers/pull/895) Adding a test to cover 'meta' and 'meta_key' attr_readers (@adomokos) +- [#894](https://github.com/rails-api/active_model_serializers/pull/894) Fixing typos in README.md (@adomokos) +- [#888](https://github.com/rails-api/active_model_serializers/pull/888) Changed duplicated test name in action controller test (@groyoh) +- [#890](https://github.com/rails-api/active_model_serializers/pull/890) Remove unused method `def_serializer` (@JustinAiken) +- [#887](https://github.com/rails-api/active_model_serializers/pull/887) Fixing tests on JRuby (@joaomdmoura) +- [#885](https://github.com/rails-api/active_model_serializers/pull/885) Updates rails versions for test and dev (@tonyta) + +### [v0.10.0.rc1 (2015-04-22)](https://github.com/rails-api/active_model_serializers/compare/86fc7d7227f3ce538fcb28c1e8c7069ce311f0e1...v0.10.0.rc1) +- [#810](https://github.com/rails-api/active_model_serializers/pull/810) Adding Fragment Cache to AMS (@joaomdmoura) + * adds fragment cache support +- [#868](https://github.com/rails-api/active_model_serializers/pull/868) Fixed a bug that appears when a nil association is included (@groyoh) +- [#861](https://github.com/rails-api/active_model_serializers/pull/861) README: Add emphasis to single-word difference (@machty) +- [#858](https://github.com/rails-api/active_model_serializers/pull/858) Included resource fixes (@mateomurphy) +- [#853](https://github.com/rails-api/active_model_serializers/pull/853) RC3 Updates for JSON API (@mateomurphy) +- [#852](https://github.com/rails-api/active_model_serializers/pull/852) Fix options merge order in `each_association` (@mateomurphy) +- [#850](https://github.com/rails-api/active_model_serializers/pull/850) Use association value for determining serializer used (@mateomurphy) +- [#843](https://github.com/rails-api/active_model_serializers/pull/843) Remove the mailing list from the README (@JoshSmith) +- [#842](https://github.com/rails-api/active_model_serializers/pull/842) Add notes on how you can help to contributing documentation (@JoshSmith) +- [#833](https://github.com/rails-api/active_model_serializers/pull/833) Cache serializers for class (@lsylvester) +- [#837](https://github.com/rails-api/active_model_serializers/pull/837) Store options in array serializers (@kurko) +- [#836](https://github.com/rails-api/active_model_serializers/pull/836) Makes passed in options accessible inside serializers (@kurko) +- [#773](https://github.com/rails-api/active_model_serializers/pull/773) Make json api adapter 'include' option accept an array (@sweatypitts) +- [#830](https://github.com/rails-api/active_model_serializers/pull/830) Add contributing readme (@JoshSmith) +- [#811](https://github.com/rails-api/active_model_serializers/pull/811) Reimplement serialization scope and scope_name (@mateomurphy) +- [#725](https://github.com/rails-api/active_model_serializers/pull/725) Support has_one to be compatible with 0.8.x (@ggordon) + * adds `has_one` attribute for backwards compatibility +- [#822](https://github.com/rails-api/active_model_serializers/pull/822) Replace has_one with attribute in template (@bf4) +- [#821](https://github.com/rails-api/active_model_serializers/pull/821) Fix explicit serializer for associations (@wjordan) +- [#798](https://github.com/rails-api/active_model_serializers/pull/798) Fix lost test `test_include_multiple_posts_and_linked` (@donbobka) +- [#807](https://github.com/rails-api/active_model_serializers/pull/807) Add Overriding attribute methods section to README. (@alexstophel) +- [#693](https://github.com/rails-api/active_model_serializers/pull/693) Cache Support at AMS 0.10.0 (@joaomdmoura) + * adds cache support to attributes and associations. +- [#792](https://github.com/rails-api/active_model_serializers/pull/792) Association overrides (@kurko) + * adds method to override association +- [#794](https://github.com/rails-api/active_model_serializers/pull/794) add to_param for correct URL generation (@carlesjove) + +### v0.10.0-pre + +- [Introduce Adapter](https://github.com/rails-api/active_model_serializers/commit/f00fe5595ddf741dc26127ed8fe81adad833ead5) +- Prefer `ActiveModel::Serializer` to `ActiveModelSerializers`: + - [Namespace](https://github.com/rails-api/active_model_serializers/commit/729a823868e8c7ac86c653fcc7100ee511e08cb6#diff-fe7aa2941c19a41ccea6e52940d84016). + - [README](https://github.com/rails-api/active_model_serializers/commit/4a2d9853ba7486acc1747752982aa5650e7fd6e9). diff --git a/CHANGELOG-prehistory.md b/CHANGELOG-prehistory.md new file mode 100644 index 000000000..a27588306 --- /dev/null +++ b/CHANGELOG-prehistory.md @@ -0,0 +1,15 @@ +## Prehistory + +- [Changing Serialization/Serializers namespace to `Serializable` (November 30, 2011)](https://github.com/rails/rails/commit/8896b4fdc8a543157cdf4dfc378607ebf6c10ab0) + - [Merge branch 'serializers'. This implements the ActiveModel::Serializer object. Includes code, tests, generators and guides. From José and Yehuda with love.](https://github.com/rails/rails/commit/fcacc6986ab60f1fb2e423a73bf47c7abd7b191d) + - But [was reverted](https://github.com/rails/rails/commit/5b2eb64ceb08cd005dc06b721935de5853971473). + '[Revert the serializers API as other alternatives are now also under discussion](https://github.com/rails/rails/commit/0a4035b12a6c59253cb60f9e3456513c6a6a9d33)'. +- [Proposed Implementation to Rails 3.2 by @wycats and @josevalim (November 25, 2011)](https://github.com/rails/rails/pull/3753) + - [Creation of `ActionController::Serialization`, initial serializer + support (September, 26 2011)](https://github.com/rails/rails/commit/8ff7693a8dc61f43fc4eaf72ed24d3b8699191fe). + - [Docs and CHANGELOG](https://github.com/rails/rails/commit/696d01f7f4a8ed787924a41cce6df836cd73c46f) + - [Deprecation of ActiveModel::Serialization to ActiveModel::Serializable](https://github.com/rails/rails/blob/696d01f7f4a8ed787924a41cce6df836cd73c46f/activemodel/lib/active_model/serialization.rb) +- [Creation of `ActiveModel::Serialization` from `ActiveModel::Serializer` in Rails (2009)](https://github.com/rails/rails/commit/c6bc8e662614be711f45a8d4b231d5f993b024a7#diff-d029b9768d8df0407a35804a468e3ae5) +- [Integration of `ActiveModel::Serializer` into `ActiveRecord::Serialization`](https://github.com/rails/rails/commit/783db25e0c640c1588732967a87d65c10fddc08e) +- [Creation of `ActiveModel::Serializer` in Rails (2009)](https://github.com/rails/rails/commit/d2b78b3594b9cc9870e6a6ebfeb2e56d00e6ddb8#diff-80d5beeced9bdc24ca2b04a201543bdd) +- [Creation of `ActiveModel::Serializers::JSON` in Rails (2009)](https://github.com/rails/rails/commit/fbdf706fffbfb17731a1f459203d242414ef5086) diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..11070ef79 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +## Dev + +### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/master..dev) + +Breaking changes: + +Features: + +Fixes: + +Misc: + +## [0.10.x](CHANGELOG-0-10.md) + +## [0.09.x](CHANGELOG-0-09.md) + +## [0.08.x](CHANGELOG-0-08.md) + +## [Prehistory](CHANGELOG-prehistory.md) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..d46841e99 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting one of the owners listed at https://rubygems.org/gems/active_model_serializers. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..3ea519c47 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,100 @@ +## Have an issue? + +Before opening an issue, try the following: + +##### Consult the documentation + +See if your issue can be resolved by information in the [documentation](README.md). + +##### Check for an existing issue + +Take a look at the issues to see if a similar one has already been created. If +one exists, please add any additional information that might expedite +resolution. + +#### Open an issue + +If the documentation wasn't able to help resolve the issue and no issue already +exists, please open a new issue with the following in mind: + +- Please make sure only to include one issue per report. If you encounter + multiple, unrelated issues, please report them as such. +- Be detailed. Provide backtraces and example code when possible. Provide + information about your environment. e.g., Ruby version, rails version, etc. +- Own your issue. Actively participate in the discussion and help drive the + issue to closure. +- If you resolve your own issue, please share the details on the issue and close + it out. Others might have the same issue and sharing solutions is helpful. + +## Contributing + +Contributing can be done in many ways and is not exclusive to code. If you have +thoughts on a particular issue or feature, we encourage you to open new issues +for discussion or add your comments to existing ones. + +#### Pull requests + +We also gladly welcome pull requests. When preparing to work on pull request, +please adhere to these standards: + +- Base work on the relevant branch: + [0.10-stable](https://github.com/rails-api/active_model_serializers/tree/0-10-stable) + or + [0.9-stable](https://github.com/rails-api/active_model_serializers/tree/0-9-stable) + or + [0.8-stable](https://github.com/rails-api/active_model_serializers/tree/0-8-stable) +- Squash your commits and regularly rebase off master. +- Provide a description of the changes contained in the pull request. +- Note any specific areas that should be reviewed. +- Include tests. +- The test suite must pass on [supported Ruby versions](.travis.yml) +- Include updates to the [documentation](docs) + where applicable. +- Update the + [CHANGELOG](CHANGELOG.md) + to the appropriate sections with a brief description of the changes. +- Do not change the VERSION file. + +#### Running tests + +Run all tests + +`$ rake test` + +Run a single test suite + +`$ rake test TEST=path/to/test.rb` + +Run a single test + +`$ rake test TEST=path/to/test.rb TESTOPTS="--name=test_something"` + +Run tests against different Rails versions by setting the RAILS_VERSION variable +and bundling gems. (save this script somewhere executable and run from top of AMS repository) + +```bash +#!/usr/bin/env bash + +rcommand='puts YAML.load_file("./.travis.yml")["env"]["matrix"].join(" ").gsub("RAILS_VERSION=", "")' +versions=$(ruby -ryaml -e "$rcommand") + +for version in ${versions[@]}; do + export RAILS_VERSION="$version" + rm -f Gemfile.lock + bundle check || bundle --local || bundle + bundle exec rake test + if [ "$?" -eq 0 ]; then + # green in ANSI + echo -e "\033[32m **** Tests passed against Rails ${RAILS_VERSION} **** \033[0m" + else + # red in ANSI + echo -e "\033[31m **** Tests failed against Rails ${RAILS_VERSION} **** \033[0m" + read -p '[Enter] any key to continue, [q] to quit...' prompt + if [ "$prompt" = 'q' ]; then + unset RAILS_VERSION + exit 1 + fi +fi + unset RAILS_VERSION +done +``` diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 25b466ebf..000000000 --- a/Gemfile +++ /dev/null @@ -1,17 +0,0 @@ -source 'https://rubygems.org' - -# Specify your gem's dependencies in active_model_serializers.gemspec -gemspec - -gem "minitest" - -version = ENV["RAILS_VERSION"] || "4.1" - -if version == "master" - gem "rails", github: "rails/rails" - - # ugh https://github.com/rails/rails/issues/16063#issuecomment-48090125 - gem "arel", github: "rails/arel" -else - gem "rails", "~> #{version}.0" -end diff --git a/LICENSE.txt b/MIT-LICENSE similarity index 100% rename from LICENSE.txt rename to MIT-LICENSE diff --git a/README.md b/README.md index 50387ee2c..26086defc 100644 --- a/README.md +++ b/README.md @@ -1,211 +1,113 @@ -# ActiveModel::Serializers +# ActiveModelSerializers -[![Build Status](https://travis-ci.org/rails-api/active_model_serializers.svg)](https://travis-ci.org/rails-api/active_model_serializers) +## About -ActiveModel::Serializers brings convention over configuration to your JSON generation. +ActiveModelSerializers is undergoing some renovations. See [Development Status](#status-of-ams). -AMS does this through two components: **serializers** and **adapters**. Serializers describe which attributes and relationships should be serialized. Adapters describe how attributes and relationships should be serialized. - -# MAINTENANCE, PLEASE READ - -This is the master branch of AMS. It will become the `0.10.0` release when it's -ready, but it's not. You probably don't want to use it yet. As such, we recommend -that any new projects you start use the latest `0.8.x` version of this gem. This -version is the most widely used, and will most closely resemble the forthcoming release. +## Getting Help -There are two released versions of AMS that you may want to use: `0.9.x` and -`0.8.x`. `9` was recently `master`, so if you were using master, you probably want -to use it. `8` was the version that was on RubyGems, so if you were using that, -that's probably what you want. +If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new) +and see our [contributing guide](CONTRIBUTING.md). -`0.10.x` will be based on the `0.8.0` code, but with a more flexible -architecture. We'd love your help. +If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers). -For more, please see [the rails-api-core mailing list](https://groups.google.com/d/msg/rails-api-core/8zu1xjIOTAM/siZ0HySKgaAJ). +If you'd like to chat, we have a [community slack](http://amserializers.herokuapp.com). Thanks! -## Example - -Given two models, a `Post(title: string, body: text)` and a -`Comment(name:string, body:text, post_id:integer)`, you will have two -serializers: - -```ruby -class PostSerializer < ActiveModel::Serializer - attributes :title, :body - - has_many :comments - - url :post -end -``` - -and - -```ruby -class CommentSerializer < ActiveModel::Serializer - attributes :name, :body - - belongs_to :post - - url [:post, :comment] -end -``` - -Generally speaking, you as a user of AMS will write (or generate) these -serializer classes. By default, they will use the JsonApiAdapter, implemented -by AMS. If you want to use a different adapter, such as a HalAdapter, you can -change this in an initializer: - -```ruby -ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::HalAdapter -``` - -or - -```ruby -ActiveModel::Serializer.config.adapter = :hal -``` - -You won't need to implement an adapter unless you wish to use a new format or -media type with AMS. - -If you would like the key in the outputted JSON to be different from its name in ActiveRecord, you can use the :key option to customize it: - -```ruby -class PostSerializer < ActiveModel::Serializer - attributes :id, :body - - # look up :subject on the model, but use +title+ in the JSON - attribute :subject, :key => :title - has_many :comments -end -``` +## Documentation -In your controllers, when you use `render :json`, Rails will now first search -for a serializer for the object and use it if available. +If you're reading this at https://github.com/rails-api/active_model_serializers you are +reading documentation for our `master`, which is not yet released. -```ruby -class PostsController < ApplicationController - def show - @post = Post.find(params[:id]) + + + + + + + + + + + + + + + + +
+ 0.10 (0-10-stable) Documentation + + + + + + + + Guides + +
+ 0.9 (0-9-stable) Documentation + + + + + +
+ 0.8 (0-8-stable) Documentation + + + + + +
- render json: @post - end -end -``` -In this case, Rails will look for a serializer named `PostSerializer`, and if -it exists, use it to serialize the `Post`. +## Status of AMS -### Built in Adapters +### *Status*: -The `:json_api` adapter will include the associated resources in the `"linked"` -member when the resource names are included in the `include` option. +- ❗️ All existing PRs against master will need to be closed and re-opened against 0-10-stable, if so desired +- ❗️ Master, for the moment, won't have any released version of AMS on it. +- :eyes: See below for [alternatives](#alternatives) -```ruby - render @posts, include: 'authors,comments' -``` -### Specify a serializer +### *Changes to 0.10.x maintenance*: -If you wish to use a serializer other than the default, you can explicitly pass it to the renderer. +- The 0.10.x version has become a huge maintenance version. We had hoped to get it in shape for a 1.0 release, but it is clear that isn't going to happen. Almost none of the maintainers from 0.8, 0.9, or earlier 0.10 are still working on AMS. We'll continue to maintain 0.10.x on the 0-10-stable branch, but maintainers won't otherwise be actively developing on it. + - We may choose to make a 0.11.x ( 0-11-stable) release based on 0-10-stable that just removes the deprecations. -#### 1. For a resource: +### *What's happening to AMS*: -```ruby - render json: @post, serializer: PostPreviewSerializer -``` +- There's been a lot of churn around AMS since it began back in [Rails 3.2](CHANGELOG-prehistory.md) and a lot of new libraries are around and the JSON:API spec has reached 1.0. +- If there is to be a 1.0 release of AMS, it will need to address the general needs of serialization in much the way ActiveJob can be used with different workers. +- The next major release *is* in development. We're starting simple and avoiding, at least at the outset, all the complications in AMS version, especially all the implicit behavior from guessing the serializer, to the association's serializer, to the serialization type, etc. +- The basic idea is that models to serializers are a one to many relationship. Everything will need to be explicit. If you want to serialize a User with a UserSerializer, you'll need to call it directly. The serializer will essentially be for defining a basic JSON:API resource object: id, type, attributes, and relationships. The serializer will have an as_json method and can be told which fields (attributes/relationships) to serialize to JSON and will likely *not* know serialize any more than the relations id and type. Serializing anything more about the relations would require code that called a serializer. (This is still somewhat in discussion). +- If this works out, the idea is to get something into Rails that existing libraries can use. -#### 2. For an array resource: +See [PR 2121](https://github.com/rails-api/active_model_serializers/pull/2121) where these changes were introduced for more information and any discussion. -```ruby -# Use the default `ArraySerializer`, which will use `each_serializer` to -# serialize each element -render json: @posts, each_serializer: PostPreviewSerializer -# Or, you can explicitly provide the collection serializer as well -render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer -``` -## Installation +## Alternatives -Add this line to your application's Gemfile: +- [jsonapi-rb](http://jsonapi-rb.org/) is a [highly performant](https://gist.github.com/NullVoxPopuli/748e89ddc1732b42fdf42435d773734a) and modular JSON:API-only implementation. There's a vibrant community around it that has produced projects such as [JSON:API Suite](https://jsonapi-suite.github.io/jsonapi_suite_deprecated/). +- [fast_jsonapi](https://github.com/fast-jsonapi/fast_jsonapi) is a lightning fast JSON:API serializer for Ruby Objects. +- [jsonapi-resources](https://github.com/cerebris/jsonapi-resources) is a popular resource-focused framework for implementing JSON:API servers. +- [blueprinter](https://github.com/procore/blueprinter) is a fast, declarative, and API spec agnostic serializer that uses composable views to reduce duplication. From your friends at Procore. +- [Alba](https://github.com/okuramasafumi/alba) is fast and spec agnostic serialization solution. It has some unique features such as global or per-resource error handling. +- [Transmutation](https://github.com/spellbook-technology/transmutation) is fast and lightweight JSON attribute serialization solution. It provides an intuitive serializer lookup, inspired from AMS. -``` -gem 'active_model_serializers' -``` -And then execute: +For benchmarks against alternatives, see https://github.com/rails-api/active_model_serializers/tree/benchmarks -``` -$ bundle -``` -## Creating a Serializer -The easiest way to create a new serializer is to generate a new resource, which -will generate a serializer at the same time: +## Semantic Versioning -``` -$ rails g resource post title:string body:string -``` - -This will generate a serializer in `app/serializers/post_serializer.rb` for -your new model. You can also generate a serializer for an existing model with -the serializer generator: - -``` -$ rails g serializer post -``` - -The generated seralizer will contain basic `attributes` and -`has_many`/`belongs_to` declarations, based on the model. For example: - -```ruby -class PostSerializer < ActiveModel::Serializer - attributes :title, :body - - has_many :comments - - url :post -end -``` - -and - -```ruby -class CommentSerializer < ActiveModel::Serializer - attributes :name, :body - - belongs_to :post_id - - url [:post, :comment] -end -``` - -The attribute names are a **whitelist** of attributes to be serialized. - -The `has_many` and `belongs_to` declarations describe relationships between -resources. By default, when you serialize a `Post`, you will get its `Comment`s -as well. - -The `url` declaration describes which named routes to use while generating URLs -for your JSON. Not every adapter will require URLs. - -## Getting Help - -If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new). - -If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers). - -Thanks! +This project adheres to [semver](http://semver.org/) ## Contributing -1. Fork it ( https://github.com/rails-api/active_model_serializers/fork ) -2. Create your feature branch (`git checkout -b my-new-feature`) -3. Commit your changes (`git commit -am 'Add some feature'`) -4. Push to the branch (`git push origin my-new-feature`) -5. Create a new Pull Request +See [CONTRIBUTING.md](CONTRIBUTING.md) diff --git a/Rakefile b/Rakefile index 8a1f1e9e8..fbfba082c 100644 --- a/Rakefile +++ b/Rakefile @@ -1,12 +1,5 @@ -require "bundler/gem_tasks" - -require 'rake/testtask' - -Rake::TestTask.new do |t| - t.libs << "test" - t.test_files = FileList['test/**/*_test.rb'] - t.ruby_opts = ['-r./test/test_helper.rb'] - t.verbose = true +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' end - -task :default => :test diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index 3ae91fc15..02578620b 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -1,26 +1,20 @@ # coding: utf-8 -lib = File.expand_path('../lib', __FILE__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'active_model/serializer/version' Gem::Specification.new do |spec| - spec.name = "active_model_serializers" - spec.version = ActiveModel::Serializer::VERSION - spec.authors = ["Steve Klabnik"] - spec.email = ["steve@steveklabnik.com"] - spec.summary = %q{Conventions-based JSON generation for Rails.} - spec.description = %q{ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.} - spec.homepage = "https://github.com/rails-api/active_model_serializers" - spec.license = "MIT" + spec.name = 'active_model_serializers' + spec.version = "1.0.0-dev" + spec.platform = Gem::Platform::RUBY + spec.authors = ['Steve Klabnik'] + spec.email = ['steve@steveklabnik.com'] + spec.summary = 'Conventions-based JSON generation for Rails.' + spec.description = 'ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.' + spec.homepage = 'https://github.com/rails-api/active_model_serializers' + spec.license = 'MIT' spec.files = `git ls-files -z`.split("\x0") - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] + spec.executables = [] - spec.add_dependency "activemodel", ">= 4.0" - - spec.add_development_dependency "rails", ">= 4.0" - spec.add_development_dependency "bundler", "~> 1.6" - spec.add_development_dependency "rake" + spec.required_ruby_version = '>= 2.1' end diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 000000000..aabf26a6b --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,11 @@ +version: 1.0.{build}-{branch} + +skip_tags: true + +cache: + - vendor/bundle + +test_script: + - true + +build: off diff --git a/docs/rfcs/template.md b/docs/rfcs/template.md new file mode 100644 index 000000000..5484eca9c --- /dev/null +++ b/docs/rfcs/template.md @@ -0,0 +1,15 @@ +- Start Date: (YYYY-MM-DD) +- RFC PR: https://github.com/rails-api/active_model_serializers/pull/dddd +- ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/dddd + +# Summary + +# Motivation + +# Detailed design + +# Drawbacks + +# Alternatives + +# Unresolved questions diff --git a/lib/action_controller/serialization.rb b/lib/action_controller/serialization.rb deleted file mode 100644 index edb5bc0e0..000000000 --- a/lib/action_controller/serialization.rb +++ /dev/null @@ -1,42 +0,0 @@ -require 'active_support/core_ext/class/attribute' - -module ActionController - module Serialization - extend ActiveSupport::Concern - - include ActionController::Renderers - - ADAPTER_OPTION_KEYS = [:include, :root, :adapter] - - def get_serializer(resource) - @_serializer ||= @_serializer_opts.delete(:serializer) - @_serializer ||= ActiveModel::Serializer.serializer_for(resource) - - if @_serializer_opts.key?(:each_serializer) - @_serializer_opts[:serializer] = @_serializer_opts.delete(:each_serializer) - end - - @_serializer - end - - def use_adapter? - !(@_adapter_opts.key?(:adapter) && !@_adapter_opts[:adapter]) - end - - [:_render_option_json, :_render_with_renderer_json].each do |renderer_method| - define_method renderer_method do |resource, options| - @_adapter_opts, @_serializer_opts = - options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } - - if use_adapter? && (serializer = get_serializer(resource)) - # omg hax - object = serializer.new(resource, @_serializer_opts) - adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts) - super(adapter, options) - else - super(resource, options) - end - end - end - end -end diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb deleted file mode 100644 index f2a34c350..000000000 --- a/lib/active_model/serializer.rb +++ /dev/null @@ -1,164 +0,0 @@ -module ActiveModel - class Serializer - extend ActiveSupport::Autoload - autoload :Configuration - autoload :ArraySerializer - autoload :Adapter - include Configuration - - class << self - attr_accessor :_attributes - attr_accessor :_associations - attr_accessor :_urls - end - - def self.inherited(base) - base._attributes = [] - base._associations = {} - base._urls = [] - end - - def self.attributes(*attrs) - @_attributes.concat attrs - - attrs.each do |attr| - define_method attr do - object.read_attribute_for_serialization(attr) - end unless method_defined?(attr) - end - end - - def self.attribute(attr, options = {}) - key = options.fetch(:key, attr) - @_attributes.concat [key] - define_method key do - object.read_attribute_for_serialization(attr) - end unless method_defined?(key) - end - - # Defines an association in the object that should be rendered. - # - # The serializer object should implement the association name - # as a method which should return an array when invoked. If a method - # with the association name does not exist, the association name is - # dispatched to the serialized object. - def self.has_many(*attrs) - associate(:has_many, attrs) - end - - # Defines an association in the object that should be rendered. - # - # The serializer object should implement the association name - # as a method which should return an object when invoked. If a method - # with the association name does not exist, the association name is - # dispatched to the serialized object. - def self.belongs_to(*attrs) - associate(:belongs_to, attrs) - end - - def self.associate(type, attrs) #:nodoc: - options = attrs.extract_options! - self._associations = _associations.dup - - attrs.each do |attr| - unless method_defined?(attr) - define_method attr do - object.send attr - end - end - - self._associations[attr] = {type: type, options: options} - end - end - - def self.url(attr) - @_urls.push attr - end - - def self.urls(*attrs) - @_urls.concat attrs - end - - def self.serializer_for(resource) - if resource.respond_to?(:to_ary) - config.array_serializer - else - get_serializer_for(resource.class) - end - end - - def self.adapter - adapter_class = case config.adapter - when Symbol - ActiveModel::Serializer::Adapter.adapter_class(config.adapter) - when Class - config.adapter - end - unless adapter_class - valid_adapters = Adapter.constants.map { |klass| ":#{klass.to_s.downcase}" } - raise ArgumentError, "Unknown adapter: #{config.adapter}. Valid adapters are: #{valid_adapters}" - end - - adapter_class - end - - def self._root - @@root ||= false - end - - def self._root=(root) - @@root = root - end - - def self.root_name - name.demodulize.underscore.sub(/_serializer$/, '') if name - end - - attr_accessor :object, :root - - def initialize(object, options = {}) - @object = object - @root = options[:root] || (self.class._root ? self.class.root_name : false) - end - - def json_key - if root == true || root.nil? - self.class.root_name - else - root - end - end - - def attributes(options = {}) - self.class._attributes.dup.each_with_object({}) do |name, hash| - hash[name] = send(name) - end - end - - def each_association(&block) - self.class._associations.dup.each do |name, options| - association = object.send(name) - serializer_class = ActiveModel::Serializer.serializer_for(association) - serializer = serializer_class.new(association) if serializer_class - - if block_given? - block.call(name, serializer, options[:options]) - end - end - end - - private - - def self.get_serializer_for(klass) - serializer_class_name = "#{klass.name}Serializer" - serializer_class = serializer_class_name.safe_constantize - - if serializer_class - serializer_class - elsif klass.superclass - get_serializer_for(klass.superclass) - end - end - - end -end diff --git a/lib/active_model/serializer/adapter.rb b/lib/active_model/serializer/adapter.rb deleted file mode 100644 index bf546097e..000000000 --- a/lib/active_model/serializer/adapter.rb +++ /dev/null @@ -1,35 +0,0 @@ -module ActiveModel - class Serializer - class Adapter - extend ActiveSupport::Autoload - autoload :Json - autoload :Null - autoload :JsonApi - - attr_reader :serializer - - def initialize(serializer, options = {}) - @serializer = serializer - @options = options - end - - def serializable_hash(options = {}) - raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.' - end - - def as_json(options = {}) - serializable_hash(options) - end - - def self.create(resource, options = {}) - override = options.delete(:adapter) - klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter - klass.new(resource, options) - end - - def self.adapter_class(adapter) - "ActiveModel::Serializer::Adapter::#{adapter.to_s.classify}".safe_constantize - end - end - end -end diff --git a/lib/active_model/serializer/adapter/json.rb b/lib/active_model/serializer/adapter/json.rb deleted file mode 100644 index 8ad1e41d6..000000000 --- a/lib/active_model/serializer/adapter/json.rb +++ /dev/null @@ -1,34 +0,0 @@ -module ActiveModel - class Serializer - class Adapter - class Json < Adapter - def serializable_hash(options = {}) - if serializer.respond_to?(:each) - @result = serializer.map{|s| self.class.new(s).serializable_hash } - else - @result = serializer.attributes(options) - - serializer.each_association do |name, association, opts| - if association.respond_to?(:each) - array_serializer = association - @result[name] = array_serializer.map { |item| item.attributes(opts) } - else - if association - @result[name] = association.attributes(options) - else - @result[name] = nil - end - end - end - end - - if root = options.fetch(:root, serializer.json_key) - @result = { root => @result } - end - - @result - end - end - end - end -end diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb deleted file mode 100644 index f02152c79..000000000 --- a/lib/active_model/serializer/adapter/json_api.rb +++ /dev/null @@ -1,151 +0,0 @@ -module ActiveModel - class Serializer - class Adapter - class JsonApi < Adapter - def initialize(serializer, options = {}) - super - serializer.root = true - @hash = {} - @top = @options.fetch(:top) { @hash } - end - - def serializable_hash(options = {}) - @root = (@options[:root] || serializer.json_key.to_s.pluralize).to_sym - - if serializer.respond_to?(:each) - @hash[@root] = serializer.map do |s| - self.class.new(s, @options.merge(top: @top)).serializable_hash[@root] - end - else - @hash[@root] = attributes_for_serializer(serializer, @options) - - add_resource_links(@hash[@root], serializer) - end - - @hash - end - - private - - def add_links(resource, name, serializers) - type = serialized_object_type(serializers) - resource[:links] ||= {} - - if name.to_s == type || !type - resource[:links][name] ||= [] - resource[:links][name] += serializers.map{|serializer| serializer.id.to_s } - else - resource[:links][name] ||= {} - resource[:links][name][:type] = type - resource[:links][name][:ids] ||= [] - resource[:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s } - end - end - - def add_link(resource, name, serializer) - resource[:links] ||= {} - resource[:links][name] = nil - - if serializer - type = serialized_object_type(serializer) - if name.to_s == type || !type - resource[:links][name] = serializer.id.to_s - else - resource[:links][name] ||= {} - resource[:links][name][:type] = type - resource[:links][name][:id] = serializer.id.to_s - end - end - end - - def add_linked(resource_name, serializers, parent = nil) - serializers = Array(serializers) unless serializers.respond_to?(:each) - - resource_path = [parent, resource_name].compact.join('.') - - if include_assoc?(resource_path) - plural_name = serialized_object_type(serializers).pluralize.to_sym - @top[:linked] ||= {} - @top[:linked][plural_name] ||= [] - - serializers.each do |serializer| - attrs = attributes_for_serializer(serializer, @options) - - add_resource_links(attrs, serializer, add_linked: false) - - @top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs) - end - end - - serializers.each do |serializer| - serializer.each_association do |name, association, opts| - add_linked(name, association, resource_path) if association - end if include_nested_assoc? resource_path - end - end - - def attributes_for_serializer(serializer, options) - if serializer.respond_to?(:each) - result = [] - serializer.each do |object| - attributes = object.attributes(options) - attributes[:id] = attributes[:id].to_s if attributes[:id] - result << attributes - end - else - result = serializer.attributes(options) - result[:id] = result[:id].to_s if result[:id] - end - - result - end - - def include_assoc?(assoc) - return false unless @options[:include] - check_assoc("#{assoc}$") - end - - def include_nested_assoc?(assoc) - return false unless @options[:include] - check_assoc("#{assoc}.") - end - - def check_assoc(assoc) - @options[:include].split(',').any? do |s| - s.match(/^#{assoc.gsub('.', '\.')}/) - end - end - - def serialized_object_type(serializer) - return false unless Array(serializer).first - type_name = Array(serializer).first.object.class.to_s.underscore - if serializer.respond_to?(:first) - type_name.pluralize - else - type_name - end - end - - def add_resource_links(attrs, serializer, options = {}) - options[:add_linked] = options.fetch(:add_linked, true) - - serializer.each_association do |name, association, opts| - attrs[:links] ||= {} - - if association.respond_to?(:each) - add_links(attrs, name, association) - else - add_link(attrs, name, association) - end - - if @options[:embed] != :ids && options[:add_linked] - Array(association).each do |association| - add_linked(name, association) - end - end - end - end - end - end - end -end diff --git a/lib/active_model/serializer/adapter/null.rb b/lib/active_model/serializer/adapter/null.rb deleted file mode 100644 index 547c08ba8..000000000 --- a/lib/active_model/serializer/adapter/null.rb +++ /dev/null @@ -1,11 +0,0 @@ -module ActiveModel - class Serializer - class Adapter - class Null < Adapter - def serializable_hash(options = {}) - {} - end - end - end - end -end diff --git a/lib/active_model/serializer/array_serializer.rb b/lib/active_model/serializer/array_serializer.rb deleted file mode 100644 index 83693c978..000000000 --- a/lib/active_model/serializer/array_serializer.rb +++ /dev/null @@ -1,26 +0,0 @@ -module ActiveModel - class Serializer - class ArraySerializer - include Enumerable - delegate :each, to: :@objects - - def initialize(objects, options = {}) - @objects = objects.map do |object| - serializer_class = options.fetch( - :serializer, - ActiveModel::Serializer.serializer_for(object) - ) - serializer_class.new(object) - end - end - - def json_key - @objects.first.json_key if @objects.first - end - - def root=(root) - @objects.first.root = root if @objects.first - end - end - end -end diff --git a/lib/active_model/serializer/configuration.rb b/lib/active_model/serializer/configuration.rb deleted file mode 100644 index ef57262c4..000000000 --- a/lib/active_model/serializer/configuration.rb +++ /dev/null @@ -1,13 +0,0 @@ -module ActiveModel - class Serializer - module Configuration - include ActiveSupport::Configurable - extend ActiveSupport::Concern - - included do |base| - base.config.array_serializer = ActiveModel::Serializer::ArraySerializer - base.config.adapter = :json - end - end - end -end diff --git a/lib/active_model/serializer/version.rb b/lib/active_model/serializer/version.rb deleted file mode 100644 index 1fda65f55..000000000 --- a/lib/active_model/serializer/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ActiveModel - class Serializer - VERSION = "0.10.0.pre" - end -end diff --git a/lib/active_model_serializers.rb b/lib/active_model_serializers.rb deleted file mode 100644 index c96b90a92..000000000 --- a/lib/active_model_serializers.rb +++ /dev/null @@ -1,14 +0,0 @@ -require "active_model" -require "active_model/serializer/version" -require "active_model/serializer" - -begin - require 'action_controller' - require 'action_controller/serialization' - - ActiveSupport.on_load(:action_controller) do - include ::ActionController::Serialization - end -rescue LoadError - # rails not installed, continuing -end diff --git a/lib/generators/serializer/USAGE b/lib/generators/serializer/USAGE deleted file mode 100644 index 422f785ac..000000000 --- a/lib/generators/serializer/USAGE +++ /dev/null @@ -1,6 +0,0 @@ -Description: - Generates a serializer for the given resource with tests. - -Example: - `rails generate serializer Account name created_at` - diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb deleted file mode 100644 index 54db86e21..000000000 --- a/lib/generators/serializer/serializer_generator.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Rails - module Generators - class SerializerGenerator < NamedBase - source_root File.expand_path("../templates", __FILE__) - check_class_collision :suffix => "Serializer" - - argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" - - class_option :parent, :type => :string, :desc => "The parent class for the generated serializer" - - def create_serializer_file - template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb") - end - - private - - def attributes_names - [:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym } - end - - def association_names - attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym } - end - - def parent_class_name - if options[:parent] - options[:parent] - elsif defined?(::ApplicationSerializer) - "ApplicationSerializer" - else - "ActiveModel::Serializer" - end - end - end - end -end - diff --git a/lib/generators/serializer/templates/serializer.rb b/lib/generators/serializer/templates/serializer.rb deleted file mode 100644 index b1342098b..000000000 --- a/lib/generators/serializer/templates/serializer.rb +++ /dev/null @@ -1,8 +0,0 @@ -<% module_namespacing do -%> -class <%= class_name %>Serializer < <%= parent_class_name %> - attributes <%= attributes_names.map(&:inspect).join(", ") %> -end -<% association_names.each do |attribute| -%> - has_one :<%= attribute %> -<% end -%> -<% end -%> diff --git a/test/action_controller/adapter_selector_test.rb b/test/action_controller/adapter_selector_test.rb deleted file mode 100644 index 96d7dd524..000000000 --- a/test/action_controller/adapter_selector_test.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'test_helper' - -module ActionController - module Serialization - class AdapterSelectorTest < ActionController::TestCase - class MyController < ActionController::Base - def render_using_default_adapter - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile - end - - def render_using_adapter_override - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile, adapter: :json_api - end - - def render_skipping_adapter - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile, adapter: false - end - end - - tests MyController - - def test_render_using_default_adapter - get :render_using_default_adapter - assert_equal '{"name":"Name 1","description":"Description 1"}', response.body - end - - def test_render_using_adapter_override - get :render_using_adapter_override - assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', response.body - end - - def test_render_skipping_adapter - get :render_skipping_adapter - assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body - end - end - end -end diff --git a/test/action_controller/explicit_serializer_test.rb b/test/action_controller/explicit_serializer_test.rb deleted file mode 100644 index cb8b2bd7b..000000000 --- a/test/action_controller/explicit_serializer_test.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'test_helper' - -module ActionController - module Serialization - class ExplicitSerializerTest < ActionController::TestCase - class MyController < ActionController::Base - def render_using_explicit_serializer - @profile = Profile.new(name: 'Name 1', - description: 'Description 1', - comments: 'Comments 1') - render json: @profile, serializer: ProfilePreviewSerializer - end - - def render_array_using_explicit_serializer - array = [ - Profile.new(name: 'Name 1', - description: 'Description 1', - comments: 'Comments 1'), - Profile.new(name: 'Name 2', - description: 'Description 2', - comments: 'Comments 2') - ] - render json: array, - serializer: PaginatedSerializer, - each_serializer: ProfilePreviewSerializer - end - - def render_array_using_implicit_serializer - array = [ - Profile.new(name: 'Name 1', - description: 'Description 1', - comments: 'Comments 1'), - Profile.new(name: 'Name 2', - description: 'Description 2', - comments: 'Comments 2') - ] - render json: array, - each_serializer: ProfilePreviewSerializer - end - end - - tests MyController - - def test_render_using_explicit_serializer - get :render_using_explicit_serializer - - assert_equal 'application/json', @response.content_type - assert_equal '{"name":"Name 1"}', @response.body - end - - def test_render_array_using_explicit_serializer - get :render_array_using_explicit_serializer - assert_equal 'application/json', @response.content_type - - expected = { - 'paginated' => [ - { 'name' => 'Name 1' }, - { 'name' => 'Name 2' } - ] - } - - assert_equal expected.to_json, @response.body - end - - def test_render_array_using_explicit_serializer - get :render_array_using_implicit_serializer - assert_equal 'application/json', @response.content_type - - expected = [ - { 'name' => 'Name 1' }, - { 'name' => 'Name 2' } - ] - assert_equal expected.to_json, @response.body - end - - end - end -end diff --git a/test/action_controller/json_api_linked_test.rb b/test/action_controller/json_api_linked_test.rb deleted file mode 100644 index dca55e67b..000000000 --- a/test/action_controller/json_api_linked_test.rb +++ /dev/null @@ -1,129 +0,0 @@ -require 'test_helper' - -module ActionController - module Serialization - class JsonApiLinkedTest < ActionController::TestCase - class MyController < ActionController::Base - def setup_post - @role1 = Role.new(id: 1, name: 'admin') - @role2 = Role.new(id: 2, name: 'colab') - @author = Author.new(id: 1, name: 'Steve K.') - @author.posts = [] - @author.bio = nil - @author.roles = [@role1, @role2] - @role1.author = @author - @role2.author = @author - @author2 = Author.new(id: 2, name: 'Anonymous') - @author2.posts = [] - @author2.bio = nil - @author2.roles = [] - @post = Post.new(id: 1, title: 'New Post', body: 'Body') - @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') - @post.comments = [@first_comment, @second_comment] - @post.author = @author - @first_comment.post = @post - @first_comment.author = @author2 - @second_comment.post = @post - @second_comment.author = nil - end - - def render_resource_without_include - setup_post - render json: @post, adapter: :json_api - end - - def render_resource_with_include - setup_post - render json: @post, include: 'author', adapter: :json_api - end - - def render_resource_with_nested_include - setup_post - render json: @post, include: 'comments.author', adapter: :json_api - end - - def render_resource_with_nested_has_many_include - setup_post - render json: @post, include: 'author,author.roles', adapter: :json_api - end - - def render_collection_without_include - setup_post - render json: [@post], adapter: :json_api - end - - def render_collection_with_include - setup_post - render json: [@post], include: 'author,comments', adapter: :json_api - end - end - - tests MyController - - def test_render_resource_without_include - get :render_resource_without_include - response = JSON.parse(@response.body) - refute response.key? 'linked' - end - - def test_render_resource_with_include - get :render_resource_with_include - response = JSON.parse(@response.body) - assert response.key? 'linked' - assert_equal 1, response['linked']['authors'].size - assert_equal 'Steve K.', response['linked']['authors'].first['name'] - end - - def test_render_resource_with_nested_has_many_include - get :render_resource_with_nested_has_many_include - response = JSON.parse(@response.body) - expected_linked = { - "authors" => [{ - "id" => "1", - "name" => "Steve K.", - "links" => { - "posts" => [], - "roles" => ["1", "2"], - "bio" => nil - } - }], - "roles"=>[{ - "id" => "1", - "name" => "admin", - "links" => { - "author" => "1" - } - }, { - "id" => "2", - "name" => "colab", - "links" => { - "author" => "1" - } - }] - } - assert_equal expected_linked, response['linked'] - end - - def test_render_resource_with_nested_include - get :render_resource_with_nested_include - response = JSON.parse(@response.body) - assert response.key? 'linked' - assert_equal 1, response['linked']['authors'].size - assert_equal 'Anonymous', response['linked']['authors'].first['name'] - end - - def test_render_collection_without_include - get :render_collection_without_include - response = JSON.parse(@response.body) - refute response.key? 'linked' - end - - def test_render_collection_with_include - get :render_collection_with_include - response = JSON.parse(@response.body) - assert response.key? 'linked' - end - end - end -end diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb deleted file mode 100644 index a4c20a54c..000000000 --- a/test/action_controller/serialization_test.rb +++ /dev/null @@ -1,92 +0,0 @@ -require 'test_helper' - -module ActionController - module Serialization - class ImplicitSerializerTest < ActionController::TestCase - class MyController < ActionController::Base - def render_using_implicit_serializer - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile - end - - def render_using_custom_root - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile, root: "custom_root" - end - - def render_using_default_adapter_root - old_adapter = ActiveModel::Serializer.config.adapter - # JSON-API adapter sets root by default - ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile - ensure - ActiveModel::Serializer.config.adapter = old_adapter - end - - def render_using_custom_root_in_adapter_with_a_default - # JSON-API adapter sets root by default - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - render json: @profile, root: "profile", adapter: :json_api - end - - def render_array_using_implicit_serializer - array = [ - Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), - Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }) - ] - render json: array - end - end - - tests MyController - - # We just have Null for now, this will change - def test_render_using_implicit_serializer - get :render_using_implicit_serializer - - assert_equal 'application/json', @response.content_type - assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body - end - - def test_render_using_custom_root - get :render_using_custom_root - - assert_equal 'application/json', @response.content_type - assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body - end - - def test_render_using_default_root - get :render_using_default_adapter_root - - assert_equal 'application/json', @response.content_type - assert_equal '{"profiles":{"name":"Name 1","description":"Description 1"}}', @response.body - end - - def test_render_using_custom_root_in_adapter_with_a_default - get :render_using_custom_root_in_adapter_with_a_default - - assert_equal 'application/json', @response.content_type - assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body - end - - def test_render_array_using_implicit_serializer - get :render_array_using_implicit_serializer - assert_equal 'application/json', @response.content_type - - expected = [ - { - name: 'Name 1', - description: 'Description 1', - }, - { - name: 'Name 2', - description: 'Description 2', - } - ] - - assert_equal expected.to_json, @response.body - end - end - end -end diff --git a/test/adapter/json/belongs_to_test.rb b/test/adapter/json/belongs_to_test.rb deleted file mode 100644 index ea1186475..000000000 --- a/test/adapter/json/belongs_to_test.rb +++ /dev/null @@ -1,37 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class Json - class BelongsToTest < Minitest::Test - def setup - @post = Post.new(id: 42, title: 'New Post', body: 'Body') - @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!') - @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @post.comments = [@comment] - @anonymous_post.comments = [] - @post.author = @author - @comment.post = @post - @comment.author = nil - @anonymous_post.author = nil - - @serializer = CommentSerializer.new(@comment) - @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) - end - - def test_includes_post - assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:post]) - end - - def test_include_nil_author - serializer = PostSerializer.new(@anonymous_post) - adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) - - assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash) - end - end - end - end - end -end diff --git a/test/adapter/json/collection_test.rb b/test/adapter/json/collection_test.rb deleted file mode 100644 index 51af54489..000000000 --- a/test/adapter/json/collection_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class Json - class Collection < Minitest::Test - def setup - @author = Author.new(id: 1, name: 'Steve K.') - @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!') - @second_post = Post.new(id: 2, title: 'New Post', body: 'Body') - @first_post.comments = [] - @second_post.comments = [] - @first_post.author = @author - @second_post.author = @author - - @serializer = ArraySerializer.new([@first_post, @second_post]) - @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) - end - - def test_include_multiple_posts - assert_equal([ - {title: "Hello!!", body: "Hello, world!!", id: 1, comments: [], author: {id: 1, name: "Steve K."}}, - {title: "New Post", body: "Body", id: 2, comments: [], author: {id: 1, name: "Steve K."}} - ], @adapter.serializable_hash) - end - end - end - end - end -end diff --git a/test/adapter/json/has_many_test.rb b/test/adapter/json/has_many_test.rb deleted file mode 100644 index 77f672c70..000000000 --- a/test/adapter/json/has_many_test.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class Json - class HasManyTestTest < Minitest::Test - def setup - @author = Author.new(id: 1, name: 'Steve K.') - @post = Post.new(title: 'New Post', body: 'Body') - @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') - @post.comments = [@first_comment, @second_comment] - @post.author = @author - @first_comment.post = @post - @second_comment.post = @post - - @serializer = PostSerializer.new(@post) - @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) - end - - def test_has_many - assert_equal([ - {id: 1, body: 'ZOMG A COMMENT'}, - {id: 2, body: 'ZOMG ANOTHER COMMENT'} - ], @adapter.serializable_hash[:comments]) - end - end - end - end - end -end - diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb deleted file mode 100644 index e641429e8..000000000 --- a/test/adapter/json_api/belongs_to_test.rb +++ /dev/null @@ -1,110 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class JsonApi - class BelongsToTest < Minitest::Test - def setup - @author = Author.new(id: 1, name: 'Steve K.') - @author.bio = nil - @author.roles = [] - @post = Post.new(id: 42, title: 'New Post', body: 'Body') - @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!') - @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @post.comments = [@comment] - @anonymous_post.comments = [] - @comment.post = @post - @comment.author = nil - @post.author = @author - @anonymous_post.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") - @blog.writer = @author - @blog.articles = [@post, @anonymous_post] - @author.posts = [] - - @serializer = CommentSerializer.new(@comment) - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) - end - - def test_includes_post_id - assert_equal("42", @adapter.serializable_hash[:comments][:links][:post]) - end - - def test_includes_linked_post - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post') - expected = [{ - id: "42", - title: 'New Post', - body: 'Body', - links: { - comments: ["1"], - author: "1" - } - }] - assert_equal expected, @adapter.serializable_hash[:linked][:posts] - end - - def test_include_nil_author - serializer = PostSerializer.new(@anonymous_post) - adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - - assert_equal({comments: [], author: nil}, adapter.serializable_hash[:posts][:links]) - end - - def test_include_type_for_association_when_is_different_than_name - serializer = BlogSerializer.new(@blog) - adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - links = adapter.serializable_hash[:blogs][:links] - expected = { - writer: { - type: "author", - id: "1" - }, - articles: { - type: "posts", - ids: ["42", "43"] - } - } - assert_equal expected, links - end - - def test_include_linked_resources_with_type_name - serializer = BlogSerializer.new(@blog) - adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: "writer,articles") - linked = adapter.serializable_hash[:linked] - expected = { - authors: [{ - id: "1", - name: "Steve K.", - links: { - posts: [], - roles: [], - bio: nil - } - }], - posts: [{ - title: "New Post", - body: "Body", - id: "42", - links: { - comments: ["1"], - author: "1" - } - }, { - title: "Hello!!", - body: "Hello, world!!", - id: "43", - links: { - comments: [], - author: nil - } - }] - } - assert_equal expected, linked - end - end - end - end - end -end diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb deleted file mode 100644 index a48e66b0d..000000000 --- a/test/adapter/json_api/collection_test.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class JsonApi - class CollectionTest < Minitest::Test - def setup - @author = Author.new(id: 1, name: 'Steve K.') - @author.bio = nil - @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!') - @second_post = Post.new(id: 2, title: 'New Post', body: 'Body') - @first_post.comments = [] - @second_post.comments = [] - @first_post.author = @author - @second_post.author = @author - @author.posts = [@first_post, @second_post] - - @serializer = ArraySerializer.new([@first_post, @second_post]) - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) - end - - def test_include_multiple_posts - assert_equal([ - { title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: [], author: "1" } }, - { title: "New Post", body: "Body", id: "2", links: { comments: [], author: "1" } } - ], @adapter.serializable_hash[:posts]) - end - end - end - end - end -end diff --git a/test/adapter/json_api/has_many_embed_ids_test.rb b/test/adapter/json_api/has_many_embed_ids_test.rb deleted file mode 100644 index a6072aa4f..000000000 --- a/test/adapter/json_api/has_many_embed_ids_test.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class JsonApi - class HasManyEmbedIdsTest < Minitest::Test - def setup - @author = Author.new(name: 'Steve K.') - @author.bio = nil - @author.roles = nil - @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!') - @second_post = Post.new(id: 2, title: 'New Post', body: 'Body') - @author.posts = [@first_post, @second_post] - @first_post.author = @author - @second_post.author = @author - @first_post.comments = [] - @second_post.comments = [] - - @serializer = AuthorSerializer.new(@author) - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) - end - - def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:authors][:links][:posts]) - end - - def test_no_includes_linked_comments - assert_nil @adapter.serializable_hash[:linked] - end - end - end - end - end -end diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb deleted file mode 100644 index e6a511fba..000000000 --- a/test/adapter/json_api/has_many_test.rb +++ /dev/null @@ -1,72 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class JsonApi - class HasManyTest < Minitest::Test - def setup - @author = Author.new(id: 1, name: 'Steve K.') - @author.posts = [] - @author.bio = nil - @post = Post.new(id: 1, title: 'New Post', body: 'Body') - @post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second') - @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @first_comment.author = nil - @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') - @second_comment.author = nil - @post.comments = [@first_comment, @second_comment] - @post_without_comments.comments = [] - @first_comment.post = @post - @second_comment.post = @post - @post.author = @author - @post_without_comments.author = nil - @blog = Blog.new(id: 1, name: "My Blog!!") - @blog.writer = @author - @blog.articles = [@post] - - @serializer = PostSerializer.new(@post) - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) - end - - def test_includes_comment_ids - assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments]) - end - - def test_includes_linked_comments - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments') - expected = [{ - id: "1", - body: 'ZOMG A COMMENT', - links: { - post: "1", - author: nil - } - }, { - id: "2", - body: 'ZOMG ANOTHER COMMENT', - links: { - post: "1", - author: nil - } - }] - assert_equal expected, @adapter.serializable_hash[:linked][:comments] - end - - def test_no_include_linked_if_comments_is_empty - serializer = PostSerializer.new(@post_without_comments) - adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - - assert_nil adapter.serializable_hash[:linked] - end - - def test_include_type_for_association_when_is_different_than_name - serializer = BlogSerializer.new(@blog) - adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) - assert_equal({type: "posts", ids: ["1"]}, adapter.serializable_hash[:blogs][:links][:articles]) - end - end - end - end - end -end diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb deleted file mode 100644 index 8e896460c..000000000 --- a/test/adapter/json_api/linked_test.rb +++ /dev/null @@ -1,145 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class JsonApi - class LinkedTest < Minitest::Test - def setup - @author1 = Author.new(id: 1, name: 'Steve K.') - @author2 = Author.new(id: 2, name: 'Tenderlove') - @bio1 = Bio.new(id: 1, content: 'AMS Contributor') - @bio2 = Bio.new(id: 2, content: 'Rails Contributor') - @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!') - @second_post = Post.new(id: 2, title: 'New Post', body: 'Body') - @third_post = Post.new(id: 3, title: 'Yet Another Post', body: 'Body') - @first_post.comments = [] - @second_post.comments = [] - @first_post.author = @author1 - @second_post.author = @author2 - @third_post.author = @author1 - @author1.posts = [@first_post, @third_post] - @author1.bio = @bio1 - @author1.roles = [] - @author2.posts = [@second_post] - @author2.bio = @bio2 - @author2.roles = [] - @bio1.author = @author1 - @bio2.author = @author2 - end - - def test_include_multiple_posts_and_linked - @serializer = ArraySerializer.new([@first_post, @second_post]) - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.bio,comments') - - @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') - @first_post.comments = [@first_comment, @second_comment] - @first_comment.post = @first_post - @first_comment.author = nil - @second_comment.post = @first_post - @second_comment.author = nil - assert_equal([ - { title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: ['1', '2'], author: "1" } }, - { title: "New Post", body: "Body", id: "2", links: { comments: [], :author => "2" } } - ], @adapter.serializable_hash[:posts]) - - - expected = { - comments: [{ - id: "1", - body: "ZOMG A COMMENT", - links: { - post: "1", - author: nil - } - }, { - id: "2", - body: "ZOMG ANOTHER COMMENT", - links: { - post: "1", - author: nil - } - }], - authors: [{ - id: "1", - name: "Steve K.", - links: { - posts: ["1"], - roles: [], - bio: "1" - } - }, { - id: "2", - name: "Tenderlove", - links: { - posts: ["2"], - roles: [], - bio: "2" - } - }], - bios: [{ - id: "1", - content: "AMS Contributor", - links: { - author: "1" - } - }, { - id: "2", - content: "Rails Contributor", - links: { - author: "2" - } - }] - } - assert_equal expected, @adapter.serializable_hash[:linked] - end - - def test_include_multiple_posts_and_linked - @serializer = BioSerializer.new(@bio1) - @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.posts') - - @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') - @first_post.comments = [@first_comment, @second_comment] - @third_post.comments = [] - @first_comment.post = @first_post - @first_comment.author = nil - @second_comment.post = @first_post - @second_comment.author = nil - - expected = { - authors: [{ - id: "1", - name: "Steve K.", - links: { - posts: ["1", "3"], - roles: [], - bio: "1" - } - }], - posts: [{ - title: "Hello!!", - body: "Hello, world!!", - id: "1", - links: { - comments: ["1", "2"], - author: "1" - } - }, { - title: "Yet Another Post", - body: "Body", - id: "3", - links: { - comments: [], - author: "1" - } - }] - } - assert_equal expected, @adapter.serializable_hash[:linked] - end - end - end - end - end -end diff --git a/test/adapter/json_test.rb b/test/adapter/json_test.rb deleted file mode 100644 index fe573f7b4..000000000 --- a/test/adapter/json_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class JsonTest < Minitest::Test - def setup - @author = Author.new(id: 1, name: 'Steve K.') - @post = Post.new(title: 'New Post', body: 'Body') - @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') - @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') - @post.comments = [@first_comment, @second_comment] - @first_comment.post = @post - @second_comment.post = @post - @post.author = @author - - @serializer = PostSerializer.new(@post) - @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) - end - - def test_has_many - assert_equal([ - {id: 1, body: 'ZOMG A COMMENT'}, - {id: 2, body: 'ZOMG ANOTHER COMMENT'} - ], @adapter.serializable_hash[:comments]) - end - end - end - end -end - diff --git a/test/adapter/null_test.rb b/test/adapter/null_test.rb deleted file mode 100644 index 9f23b778e..000000000 --- a/test/adapter/null_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class Adapter - class NullTest < Minitest::Test - def setup - profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - serializer = ProfileSerializer.new(profile) - - @adapter = Null.new(serializer) - end - - def test_serializable_hash - assert_equal({}, @adapter.serializable_hash) - end - - def test_it_returns_empty_json - assert_equal('{}', @adapter.to_json) - end - end - end - end -end - diff --git a/test/adapter_test.rb b/test/adapter_test.rb deleted file mode 100644 index d76559d55..000000000 --- a/test/adapter_test.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class AdapterTest < Minitest::Test - def setup - profile = Profile.new - @serializer = ProfileSerializer.new(profile) - @adapter = ActiveModel::Serializer::Adapter.new(@serializer) - end - - def test_serializable_hash_is_abstract_method - assert_raises(NotImplementedError) do - @adapter.serializable_hash(only: [:name]) - end - end - - def test_serializer - assert_equal @serializer, @adapter.serializer - end - - def test_adapter_class_for_known_adapter - klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api) - assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass - end - - def test_adapter_class_for_unknown_adapter - klass = ActiveModel::Serializer::Adapter.adapter_class(:json_simple) - assert_nil klass - end - - def test_create_adapter - adapter = ActiveModel::Serializer::Adapter.create(@serializer) - assert_equal ActiveModel::Serializer::Adapter::Json, adapter.class - end - - def test_create_adapter_with_override - adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api}) - assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class - end - end - end -end diff --git a/test/array_serializer_test.rb b/test/array_serializer_test.rb deleted file mode 100644 index 4c9b63e16..000000000 --- a/test/array_serializer_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class ArraySerializerTest < Minitest::Test - def setup - @comment = Comment.new - @post = Post.new - @serializer = ArraySerializer.new([@comment, @post]) - end - - def test_respond_to_each - assert_respond_to @serializer, :each - end - - def test_each_object_should_be_serialized_with_appropriate_serializer - serializers = @serializer.to_a - - assert_kind_of CommentSerializer, serializers.first - assert_kind_of Comment, serializers.first.object - - assert_kind_of PostSerializer, serializers.last - assert_kind_of Post, serializers.last.object - end - end - end -end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb deleted file mode 100644 index f7c1becb7..000000000 --- a/test/fixtures/poro.rb +++ /dev/null @@ -1,102 +0,0 @@ -class Model - def initialize(hash={}) - @attributes = hash - end - - def read_attribute_for_serialization(name) - if name == :id || name == 'id' - id - else - @attributes[name] - end - end - - def id - @attributes[:id] || @attributes['id'] || object_id - end - - def method_missing(meth, *args) - if meth.to_s =~ /^(.*)=$/ - @attributes[$1.to_sym] = args[0] - elsif @attributes.key?(meth) - @attributes[meth] - else - super - end - end -end - -class Profile < Model -end - -class ProfileSerializer < ActiveModel::Serializer - attributes :name, :description - - urls :posts, :comments -end - -class ProfilePreviewSerializer < ActiveModel::Serializer - attributes :name - - urls :posts, :comments -end - -Post = Class.new(Model) -Comment = Class.new(Model) -Author = Class.new(Model) -Bio = Class.new(Model) -Blog = Class.new(Model) -Role = Class.new(Model) - -PostSerializer = Class.new(ActiveModel::Serializer) do - attributes :title, :body, :id - - has_many :comments - belongs_to :author - url :comments -end - -CommentSerializer = Class.new(ActiveModel::Serializer) do - attributes :id, :body - - belongs_to :post - belongs_to :author -end - -AuthorSerializer = Class.new(ActiveModel::Serializer) do - attributes :id, :name - - has_many :posts, embed: :ids - has_many :roles, embed: :ids - belongs_to :bio -end - -RoleSerializer = Class.new(ActiveModel::Serializer) do - attributes :id, :name - - belongs_to :author -end - -BioSerializer = Class.new(ActiveModel::Serializer) do - attributes :id, :content - - belongs_to :author -end - -BlogSerializer = Class.new(ActiveModel::Serializer) do - attributes :id, :name - - belongs_to :writer - has_many :articles -end - -PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do - def json_key - 'paginated' - end -end - -AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do - attribute :id - attribute :name, key: :title -end diff --git a/test/serializers/adapter_for_test.rb b/test/serializers/adapter_for_test.rb deleted file mode 100644 index 606410803..000000000 --- a/test/serializers/adapter_for_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -module ActiveModel - class Serializer - class AdapterForTest < Minitest::Test - def setup - @previous_adapter = ActiveModel::Serializer.config.adapter - end - - def teardown - ActiveModel::Serializer.config.adapter = @previous_adapter - end - - def test_returns_default_adapter - adapter = ActiveModel::Serializer.adapter - assert_equal ActiveModel::Serializer::Adapter::Json, adapter - end - - def test_overwrite_adapter_with_symbol - ActiveModel::Serializer.config.adapter = :null - - adapter = ActiveModel::Serializer.adapter - assert_equal ActiveModel::Serializer::Adapter::Null, adapter - ensure - - end - - def test_overwrite_adapter_with_class - ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::Null - - adapter = ActiveModel::Serializer.adapter - assert_equal ActiveModel::Serializer::Adapter::Null, adapter - end - - def test_raises_exception_if_invalid_symbol_given - ActiveModel::Serializer.config.adapter = :unknown - - assert_raises ArgumentError do - ActiveModel::Serializer.adapter - end - end - - def test_raises_exception_if_it_does_not_know_hot_to_infer_adapter - ActiveModel::Serializer.config.adapter = 42 - - assert_raises ArgumentError do - ActiveModel::Serializer.adapter - end - end - end - end -end diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb deleted file mode 100644 index b2278b450..000000000 --- a/test/serializers/associations_test.rb +++ /dev/null @@ -1,82 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class AssociationsTest < Minitest::Test - class Model - def initialize(hash={}) - @attributes = hash - end - - def read_attribute_for_serialization(name) - @attributes[name] - end - - def method_missing(meth, *args) - if meth.to_s =~ /^(.*)=$/ - @attributes[$1.to_sym] = args[0] - elsif @attributes.key?(meth) - @attributes[meth] - else - super - end - end - end - - - def setup - @author = Author.new(name: 'Steve K.') - @author.bio = nil - @author.roles = [] - @post = Post.new({ title: 'New Post', body: 'Body' }) - @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) - @post.comments = [@comment] - @comment.post = @post - @comment.author = nil - @post.author = @author - @author.posts = [@post] - - @author_serializer = AuthorSerializer.new(@author) - @comment_serializer = CommentSerializer.new(@comment) - end - - def test_has_many - assert_equal( - { posts: { type: :has_many, options: { embed: :ids } }, - roles: { type: :has_many, options: { embed: :ids } }, - bio: { type: :belongs_to, options: {} } }, - @author_serializer.class._associations - ) - @author_serializer.each_association do |name, serializer, options| - if name == :posts - assert_equal({embed: :ids}, options) - assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer) - elsif name == :bio - assert_equal({}, options) - assert_nil serializer - elsif name == :roles - assert_equal({embed: :ids}, options) - assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer) - else - flunk "Unknown association: #{name}" - end - end - end - - def test_has_one - assert_equal({post: {type: :belongs_to, options: {}}, :author=>{:type=>:belongs_to, :options=>{}}}, @comment_serializer.class._associations) - @comment_serializer.each_association do |name, serializer, options| - if name == :post - assert_equal({}, options) - assert_kind_of(PostSerializer, serializer) - elsif name == :author - assert_equal({}, options) - assert_nil serializer - else - flunk "Unknown association: #{name}" - end - end - end - end - end -end diff --git a/test/serializers/attribute_test.rb b/test/serializers/attribute_test.rb deleted file mode 100644 index 35f27ee4f..000000000 --- a/test/serializers/attribute_test.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class AttributeTest < Minitest::Test - def setup - @blog = Blog.new({ id: 1, name: 'AMS Hints' }) - @blog_serializer = AlternateBlogSerializer.new(@blog) - end - - def test_attributes_definition - assert_equal([:id, :title], - @blog_serializer.class._attributes) - end - - def test_json_serializable_hash - adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer) - assert_equal({:id=>1, :title=>"AMS Hints"}, adapter.serializable_hash) - end - end - end -end - diff --git a/test/serializers/attributes_test.rb b/test/serializers/attributes_test.rb deleted file mode 100644 index f9467b182..000000000 --- a/test/serializers/attributes_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class AttributesTest < Minitest::Test - def setup - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - @profile_serializer = ProfileSerializer.new(@profile) - end - - def test_attributes_definition - assert_equal([:name, :description], - @profile_serializer.class._attributes) - end - end - end -end - diff --git a/test/serializers/configuration_test.rb b/test/serializers/configuration_test.rb deleted file mode 100644 index 9c6c5feab..000000000 --- a/test/serializers/configuration_test.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class ConfigurationTest < Minitest::Test - def test_array_serializer - assert_equal ActiveModel::Serializer::ArraySerializer, ActiveModel::Serializer.config.array_serializer - end - - def test_default_adapter - assert_equal :json, ActiveModel::Serializer.config.adapter - end - end - end -end diff --git a/test/serializers/generators_test.rb b/test/serializers/generators_test.rb deleted file mode 100644 index 765017601..000000000 --- a/test/serializers/generators_test.rb +++ /dev/null @@ -1,59 +0,0 @@ -class Foo < Rails::Application - if Rails.version.to_s.start_with? '4' - config.eager_load = false - config.secret_key_base = 'abc123' - end -end - -Rails.application.load_generators - -require 'generators/serializer/serializer_generator' - -class SerializerGeneratorTest < Rails::Generators::TestCase - destination File.expand_path("../tmp", __FILE__) - setup :prepare_destination - - tests Rails::Generators::SerializerGenerator - arguments %w(account name:string description:text business:references) - - def test_generates_a_serializer - run_generator - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/ - end - - def test_generates_a_namespaced_serializer - run_generator ["admin/account"] - assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/ - end - - def test_uses_application_serializer_if_one_exists - Object.const_set(:ApplicationSerializer, Class.new) - run_generator - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/ - ensure - Object.send :remove_const, :ApplicationSerializer - end - - def test_uses_given_parent - Object.const_set(:ApplicationSerializer, Class.new) - run_generator ["Account", "--parent=MySerializer"] - assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/ - ensure - Object.send :remove_const, :ApplicationSerializer - end - - def test_generates_attributes_and_associations - run_generator - assert_file "app/serializers/account_serializer.rb" do |serializer| - assert_match(/^ attributes :id, :name, :description$/, serializer) - assert_match(/^ has_one :business$/, serializer) - end - end - - def test_with_no_attributes_does_not_add_extra_space - run_generator ["account"] - assert_file "app/serializers/account_serializer.rb" do |content| - assert_no_match /\n\nend/, content - end - end -end diff --git a/test/serializers/serializer_for_test.rb b/test/serializers/serializer_for_test.rb deleted file mode 100644 index 5369c57b2..000000000 --- a/test/serializers/serializer_for_test.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class SerializerForTest < Minitest::Test - class ArraySerializerTest < Minitest::Test - def setup - @array = [1, 2, 3] - @previous_array_serializer = ActiveModel::Serializer.config.array_serializer - end - - def teardown - ActiveModel::Serializer.config.array_serializer = @previous_array_serializer - end - - def test_serializer_for_array - serializer = ActiveModel::Serializer.serializer_for(@array) - assert_equal ActiveModel::Serializer.config.array_serializer, serializer - end - - def test_overwritten_serializer_for_array - new_array_serializer = Class.new - ActiveModel::Serializer.config.array_serializer = new_array_serializer - serializer = ActiveModel::Serializer.serializer_for(@array) - assert_equal new_array_serializer, serializer - end - end - - class SerializerTest < Minitest::Test - class MyProfile < Profile - end - - def setup - @profile = Profile.new - @my_profile = MyProfile.new - @model = ::Model.new - end - - def test_serializer_for_existing_serializer - serializer = ActiveModel::Serializer.serializer_for(@profile) - assert_equal ProfileSerializer, serializer - end - - def test_serializer_for_not_existing_serializer - serializer = ActiveModel::Serializer.serializer_for(@model) - assert_equal nil, serializer - end - - def test_serializer_inherited_serializer - serializer = ActiveModel::Serializer.serializer_for(@my_profile) - assert_equal ProfileSerializer, serializer - end - end - end - end -end diff --git a/test/serializers/urls_test.rb b/test/serializers/urls_test.rb deleted file mode 100644 index 05b33fa5a..000000000 --- a/test/serializers/urls_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'test_helper' - -module ActiveModel - class Serializer - class UrlsTest < Minitest::Test - - def setup - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - @post = Post.new({ title: 'New Post', body: 'Body' }) - @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) - @post.comments = [@comment] - - @profile_serializer = ProfileSerializer.new(@profile) - @post_serializer = PostSerializer.new(@post) - end - - def test_urls_definition - assert_equal([:posts, :comments], @profile_serializer.class._urls) - end - - def test_url_definition - assert_equal([:comments], @post_serializer.class._urls) - end - end - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index bd41fb6af..000000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,33 +0,0 @@ -require "bundler/setup" - -require 'rails' -require 'action_controller' -require 'action_controller/test_case' -require "active_support/json" -require 'minitest/autorun' -# Ensure backward compatibility with Minitest 4 -Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) - -require "active_model_serializers" - -require 'fixtures/poro' - -module TestHelper - Routes = ActionDispatch::Routing::RouteSet.new - Routes.draw do - get ':controller(/:action(/:id))' - get ':controller(/:action)' - end - - ActionController::Base.send :include, Routes.url_helpers -end - -ActionController::TestCase.class_eval do - def setup - @routes = TestHelper::Routes - end -end - -def def_serializer(&block) - Class.new(ActiveModel::Serializer, &block) -end