From a49e04a6f2a01a7c1fbe9dedf3462aa1f0adb9d8 Mon Sep 17 00:00:00 2001 From: Fab Mackojc Date: Mon, 2 Dec 2013 21:33:28 -0800 Subject: [PATCH 1/2] Generator now properly detects ApplicationSerializer --- .../serializer/generators/serializer/serializer_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_model/serializer/generators/serializer/serializer_generator.rb b/lib/active_model/serializer/generators/serializer/serializer_generator.rb index 7c4c036d..40a8f939 100644 --- a/lib/active_model/serializer/generators/serializer/serializer_generator.rb +++ b/lib/active_model/serializer/generators/serializer/serializer_generator.rb @@ -26,7 +26,7 @@ module Rails if options[:parent] options[:parent] elsif (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) || - defined?(::ApplicationSerializer) + (Object.const_get(:ApplicationSerializer) rescue nil) 'ApplicationSerializer' else 'ActiveModel::Serializer' From 74d3398016ed661db8d24dc90aa0837e1b9b2903 Mon Sep 17 00:00:00 2001 From: Fab Mackojc Date: Thu, 2 Jan 2014 17:25:28 -0700 Subject: [PATCH 2/2] Update docs to explain ApplicationSerializer feature --- README.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fbac6525..d0169822 100644 --- a/README.md +++ b/README.md @@ -71,10 +71,6 @@ Currently `ActiveModel::Serializers` expects objects to implement read\_attribute\_for\_serialization. That's all you need to do to have your POROs supported. -# ActiveModel::Serializer - -All new serializers descend from ActiveModel::Serializer - # render :json In your controllers, when you use `render :json`, Rails will now first search @@ -723,6 +719,29 @@ end The caching interface uses `Rails.cache` under the hood. +# ApplicationSerializer + +By default, new serializers descend from ActiveModel::Serializer. However, if you wish to share behaviour across your serializers you can create an ApplicationSerializer at ```app/serializers/application_serializer.rb```: + +```ruby +class ApplicationSerializer < ActiveModel::Serializer +end +``` + +Any newly generated serializers will automatically descend from ApplicationSerializer. + +``` +$ rails g serializer post +``` + +now generates: + +```ruby +class PostSerializer < ApplicationSerializer + attributes :id +end +```` + # Design and Implementation Guidelines ## Keep it Simple