Permalink
Browse files

Revert the serializers API as other alternatives are now also under d…

…iscussion
  • Loading branch information...
1 parent 2dab493 commit 0a4035b12a6c59253cb60f9e3456513c6a6a9d33 @josevalim josevalim committed Nov 25, 2011
@@ -31,7 +31,6 @@ module ActionController
autoload :RequestForgeryProtection
autoload :Rescue
autoload :Responder
- autoload :Serialization
autoload :SessionManagement
autoload :Streaming
autoload :Testing
@@ -1,51 +0,0 @@
-module ActionController
- # Action Controller Serialization
- #
- # Overrides render :json to check if the given object implements +active_model_serializer+
- # as a method. If so, use the returned serializer instead of calling +to_json+ in the object.
- #
- # This module also provides a serialization_scope method that allows you to configure the
- # +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
- # to the current user:
- #
- # class ApplicationController < ActionController::Base
- # serialization_scope :current_user
- # end
- #
- # If you need more complex scope rules, you can simply override the serialization_scope:
- #
- # class ApplicationController < ActionController::Base
- # private
- #
- # def serialization_scope
- # current_user
- # end
- # end
- #
- module Serialization
- extend ActiveSupport::Concern
-
- include ActionController::Renderers
-
- included do
- class_attribute :_serialization_scope
- end
-
- def serialization_scope
- send(_serialization_scope)
- end
-
- def _render_option_json(json, options)
- if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
- json = serializer.new(json, serialization_scope)
- end
- super
- end
-
- module ClassMethods
- def serialization_scope(scope)
- self._serialization_scope = scope
- end
- end
- end
-end
@@ -15,36 +15,9 @@ def to_json(options = {})
end
end
- class JsonSerializer
- def initialize(object, scope)
- @object, @scope = object, scope
- end
-
- def as_json(*)
- { :object => @object.as_json, :scope => @scope.as_json }
- end
- end
-
- class JsonSerializable
- def initialize(skip=false)
- @skip = skip
- end
-
- def active_model_serializer
- JsonSerializer unless @skip
- end
-
- def as_json(*)
- { :serializable_object => true }
- end
- end
-
class TestController < ActionController::Base
protect_from_forgery
- serialization_scope :current_user
- attr_reader :current_user
-
def self.controller_path
'test'
end
@@ -88,16 +61,6 @@ def render_json_with_extra_options
def render_json_without_options
render :json => JsonRenderable.new
end
-
- def render_json_with_serializer
- @current_user = Struct.new(:as_json).new(:current_user => true)
- render :json => JsonSerializable.new
- end
-
- def render_json_with_serializer_api_but_without_serializer
- @current_user = Struct.new(:as_json).new(:current_user => true)
- render :json => JsonSerializable.new(true)
- end
end
tests TestController
@@ -169,15 +132,4 @@ def test_render_json_calls_to_json_from_object
get :render_json_without_options
assert_equal '{"a":"b"}', @response.body
end
-
- def test_render_json_with_serializer
- get :render_json_with_serializer
- assert_match '"scope":{"current_user":true}', @response.body
- assert_match '"object":{"serializable_object":true}', @response.body
- end
-
- def test_render_json_with_serializer_api_but_without_serializer
- get :render_json_with_serializer_api_but_without_serializer
- assert_match '{"serializable_object":true}', @response.body
- end
end
@@ -1,7 +1,5 @@
## Rails 3.2.0 (unreleased) ##
-* Add ActiveModel::Serializer that encapsulates an ActiveModel object serialization *José Valim*
-
* Renamed (with a deprecation the following constants):
ActiveModel::Serialization => ActiveModel::Serializable
@@ -29,7 +29,6 @@
module ActiveModel
extend ActiveSupport::Autoload
- autoload :ArraySerializer, 'active_model/serializer'
autoload :AttributeMethods
autoload :BlockValidator, 'active_model/validator'
autoload :Callbacks
@@ -46,7 +45,6 @@ module ActiveModel
autoload :SecurePassword
autoload :Serializable
autoload :Serialization
- autoload :Serializer
autoload :TestCase
autoload :Translation
autoload :Validations
@@ -73,13 +73,6 @@ module Serializable
autoload :JSON, "active_model/serializable/json"
autoload :XML, "active_model/serializable/xml"
- module ClassMethods #:nodoc:
- def active_model_serializer
- return @active_model_serializer if defined?(@active_model_serializer)
- @active_model_serializer = "#{self.name}Serializer".safe_constantize
- end
- end
-
def serializable_hash(options = nil)
options ||= {}
@@ -107,11 +100,6 @@ def serializable_hash(options = nil)
hash
end
- # Returns a model serializer for this object considering its namespace.
- def active_model_serializer
- self.class.active_model_serializer
- end
-
private
# Hook method defining how an attribute value should be retrieved for
Oops, something went wrong.

4 comments on commit 0a4035b

@josevalim
Member

Beloved Rails contributors, could any of you temporarily pluginize this? :)

@carlosantoniodasilva

@josevalim Mate, what happened to this? Was it pluginized by someone? Is it going to make into 4.0? :)

@carlosantoniodasilva

@drogus nice! I missed that, thanks :)

Please sign in to comment.