Sha256: 5ad21f98c8c88244a8e61ff173d1a2ab192de191d8cb8823940f511d6f409bff

Contents?: true

Size: 1.43 KB

Versions: 23

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Praxis
  module Extensions
    module Rendering
      extend ActiveSupport::Concern
      include FieldExpansion

      def render(object, include_nil: false)
        loaded = media_type.load(object)
        renderer = Praxis::Renderer.new(include_nil: include_nil)
        renderer.render(loaded, expanded_fields)
      rescue Attributor::DumpError
        if media_type.domain_model == Object
          warn "Detected the rendering of an object of type #{media_type} without having a domain object model set.\n" \
               'Did you forget to define it?'
        end
        raise
      end

      def display(object, include_nil: false, encoder: default_encoder)
        identifier = Praxis::MediaTypeIdentifier.load(media_type.identifier)
        identifier += encoder unless encoder.blank?
        response.headers['Content-Type'] = identifier.to_s
        response.body = render(object, include_nil: include_nil)
        response
      rescue Praxis::Renderer::CircularRenderingError => e
        Praxis::Application.instance.validation_handler.handle!(
          summary: 'Circular Rendering Error when rendering response. ' \
                   'Please especify a view to narrow the dependent fields, or narrow your field set.',
          exception: e,
          request: request,
          stage: :action,
          errors: nil
        )
      end

      def default_encoder
        ''
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
praxis-2.0.0 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.40 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.39 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.38 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.37 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.36 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.35 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.34 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.33 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.32 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.31 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.30 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.29 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.28 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.27 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.26 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.25 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.24 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.23 lib/praxis/extensions/rendering.rb
praxis-2.0.pre.22 lib/praxis/extensions/rendering.rb