Sha256: 196d2637121ef445839e0e5ee353615cf405e632b44b3fbf9b0d8eba175b747e

Contents?: true

Size: 799 Bytes

Versions: 6

Compression:

Stored size: 799 Bytes

Contents

# frozen_string_literal: true

module Shreddies
  module AsJson
    module ActiveRecordBase
      def as_json(options = {})
        serializer = options.delete(:serializer) || "#{model_name}Serializer"

        if serializer.is_a?(String) || serializer.is_a?(Symbol)
          serializer = serializer.to_s.safe_constantize
        end

        serializer ? serializer.render_as_json(self, options) : super
      end
    end

    module ActiveRecordRelation
      def as_json(options = {})
        serializer = options.delete(:serializer) || "#{model_name}Serializer"

        if serializer.is_a?(String) || serializer.is_a?(Symbol)
          serializer = serializer.to_s.safe_constantize
        end

        serializer ? serializer.render_as_json(self, options) : super
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
shreddies-0.6.0 lib/shreddies/as_json.rb
shreddies-0.5.1 lib/shreddies/as_json.rb
shreddies-0.5.0 lib/shreddies/as_json.rb
shreddies-0.4.0 lib/shreddies/as_json.rb
shreddies-0.3.0 lib/shreddies/as_json.rb
shreddies-0.2.0 lib/shreddies/as_json.rb