Sha256: f9e6e0637bc42b67c1f17f33eeba72862a65ed0d5920c354cdf18a6851a619d4

Contents?: true

Size: 1.55 KB

Versions: 15

Compression:

Stored size: 1.55 KB

Contents

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+ on 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
      self._serialization_scope = :current_user
    end

    def serialization_scope
      send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
    end

    def default_serializer_options
    end

    def _render_option_json(resource, options)
      json = ActiveModel::Serializer.build_json(self, resource, options)

      if json
        super(json, options)
      else
        super
      end
    end

    module ClassMethods
      def serialization_scope(scope)
        self._serialization_scope = scope
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
active_model_serializers-0.8.2 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.5 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.5.alpha1 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.4 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.4.alpha5 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.4.alpha4 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.4.alpha3 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.4.alpha2 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.4.alpha1 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.3.rc1 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.3.alpha2 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.3.alpha1 lib/action_controller/serialization.rb
gpi-active_model_serializers-0.8.2 lib/action_controller/serialization.rb
active_model_serializers-0.8.1 lib/action_controller/serialization.rb
active_model_serializers-0.8.0 lib/action_controller/serialization.rb