Module: RestingPug::Render

Defined in:
lib/resting_pug/render.rb

Overview

Contains methods responsible for rendering response

Used in:

Instance Method Summary collapse

Instance Method Details

#decide_what_to_renderObject (protected)

Note:

Override render_subject and render_errors to change rendering behaviour

Calls #render_subject if subject is valid and #render_errors otherwise

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a decide_what_to_render to render something special if @subject id is 3
  def decide_what_to_render
    if @subject.valid?
      @subject.id == 3 ? render_something_special : render_subject
    else
      render_errors
    end
  end
end

Used in:

Use:


27
28
29
30
31
32
33
# File 'lib/resting_pug/render.rb', line 27

def decide_what_to_render
  if @subject.valid?
    render_subject
  else
    render_errors
  end
end

#render_errorsObject (protected)

Note:

Override this method to add more complicated logic of rendering.

Renders errors

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a render_errors to render with status 400
  def render_errors
    render json: { errors: @subject.errors }, status: 400
  end
end

Used in:


108
109
110
# File 'lib/resting_pug/render.rb', line 108

def render_errors
  render json: { errors: @subject.errors }, status: 422
end

#render_not_foundObject (protected)

Note:

Override this method to add more complicated logic of rendering.

Renders error when required params are missing

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a render_not_found to render with message 'Not found'
  def render_not_found
    render json: { message: 'Not found' }, status: 404
  end
end

Used in:


142
143
144
# File 'lib/resting_pug/render.rb', line 142

def render_not_found
  render json: {}, status: 404
end

#render_nothingObject (protected)

Note:

Override this method to add more complicated logic of rendering.

Renders empty response

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a render_nothing to render with status 200
  def render_nothing
    render json: {}, status: 200
  end
end

Used in:


91
92
93
# File 'lib/resting_pug/render.rb', line 91

def render_nothing
  render json: {}, status: 204
end

#render_param_missingObject (protected)

Note:

Override this method to add more complicated logic of rendering.

Renders error when required params are missing

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a render_param_missing to render with status 422
  def render_param_missing
    render json: { errors: { subject_model_sym => ['param is missing'] } }, status: 422
  end
end

Used in:


125
126
127
# File 'lib/resting_pug/render.rb', line 125

def render_param_missing
  render json: { errors: { subject_model_sym => ['param is missing'] } }, status: 400
end

#render_subjectObject (protected)

Note:

Override this method to add more complicated logic of rendering. Override subject_model_sym to change root attribute. Override permitted_fields_for_show to change shown attributes.

Renders json with a subject

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a render_subject to render without a root object
  def render_subject
    render json: @subject.as_json(only: permitted_fields_for_show)
  end
end

Used in:

Use:


53
54
55
# File 'lib/resting_pug/render.rb', line 53

def render_subject
  render json: { subject_model_sym => @subject.as_json(only: permitted_fields_for_show) }
end

#render_subjectsObject (protected)

Note:

Override this method to add more complicated logic of rendering. Override subject_model_sym_plural to change root attribute. Override permitted_fields_for_show to change shown attributes.

Renders json with subjects

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a render_subjects to render with 'items' as a root object
  def render_subjects
    render json: { items: @subjects.as_json(only: permitted_fields_for_show) }
  end
end

Used in:

Use:


74
75
76
# File 'lib/resting_pug/render.rb', line 74

def render_subjects
  render json: { subject_model_sym_plural => @subjects.as_json(only: permitted_fields_for_show) }
end