From 96028a7b9978adb9aa839aa332c35e0c244896aa Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Mon, 1 May 2017 10:17:48 -0500 Subject: [PATCH 1/2] Document new reflection options; support :foreign_key --- docs/general/serializers.md | 3 +++ lib/active_model/serializer/reflection.rb | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 1d968183..0606c51d 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -64,6 +64,9 @@ Where: - `unless:` - `virtual_value:` - `polymorphic:` defines if polymorphic relation type should be nested in serialized association. + - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship. + - `class_name:` used to determine `type` when `type` not given + - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object. - optional: `&block` is a context that returns the association's attributes. - prevents `association_name` method from being called. - return value of block is used as the association value. diff --git a/lib/active_model/serializer/reflection.rb b/lib/active_model/serializer/reflection.rb index d3daab4b..2e5cc2a1 100644 --- a/lib/active_model/serializer/reflection.rb +++ b/lib/active_model/serializer/reflection.rb @@ -9,6 +9,7 @@ module ActiveModel # @example # class PostSerializer < ActiveModel::Serializer # has_one :author, serializer: AuthorSerializer + # belongs_to :boss, type: :users, foreign_key: :boss_id # has_many :comments # has_many :comments, key: :last_comments do # object.comments.last(1) @@ -58,12 +59,13 @@ module ActiveModel class_name = options.fetch(:class_name, name.to_s.camelize.singularize) class_name.underscore.pluralize.to_sym end - @foreign_key = + @foreign_key = options.fetch(:foreign_key) do if collection? "#{name.to_s.singularize}_ids".to_sym else "#{name}_id".to_sym end + end end # @api public From ec7b5859f77882892325840eae0716fcaab6c14f Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Mon, 1 May 2017 10:23:13 -0500 Subject: [PATCH 2/2] Document namespace --- docs/general/serializers.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/general/serializers.md b/docs/general/serializers.md index 0606c51d..5b23ba0f 100644 --- a/docs/general/serializers.md +++ b/docs/general/serializers.md @@ -67,6 +67,7 @@ Where: - `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship. - `class_name:` used to determine `type` when `type` not given - `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object. + - `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details. - optional: `&block` is a context that returns the association's attributes. - prevents `association_name` method from being called. - return value of block is used as the association value.