Sha256: d9fcf7a656e8bca17993e79e20726037417f1ee18cbe84e350477b37644f5287

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require "active_support"
require "active_support/core_ext/string/inflections"
require "active_model"
require "active_model/serializer"

if defined?(Rails)
  module ActiveModel
    class Railtie < Rails::Railtie
      generators do |app|
        Rails::Generators.configure!(app.config.generators)
        require "generators/resource_override"
      end

      initializer "include_routes.active_model_serializer" do |app|
        ActiveSupport.on_load(:active_model_serializers) do
          include app.routes.url_helpers
        end
      end
    end
  end
end

module ActiveModel::SerializerSupport
  extend ActiveSupport::Concern

  module ClassMethods #:nodoc:
    if "".respond_to?(:safe_constantize)
      def active_model_serializer
        @active_model_serializer ||= "#{self.name}Serializer".safe_constantize
      end
    else
      def active_model_serializer
        return @active_model_serializer if defined?(@active_model_serializer)

        begin
          @active_model_serializer = "#{self.name}Serializer".constantize
        rescue NameError => e
          raise unless e.message =~ /uninitialized constant/
        end
      end
    end
  end

  # Returns a model serializer for this object considering its namespace.
  def active_model_serializer
    self.class.active_model_serializer
  end

  alias :read_attribute_for_serialization :send
end

ActiveSupport.on_load(:active_record) do
  include ActiveModel::SerializerSupport
end

begin
  require 'action_controller'
  require 'action_controller/serialization'

  ActiveSupport.on_load(:action_controller) do
    include ::ActionController::Serialization
  end
rescue LoadError => ex
  # rails on installed, continuing
end

ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_serializers-0.5.1 lib/active_model_serializers.rb