Module: RailsUtil::JsonHelper

Defined in:
lib/rails_util/json_helper.rb

Overview

RailsUtil::JsonHelper contains helper methods for rendering JSON API responses

Defined Under Namespace

Classes: MissingSerializerError

Instance Method Summary collapse

Instance Method Details

#json_collection(resource, **options) ⇒ Array

Renders array of JSON objects, along with other options, or an empty array if the array contains no values

Parameters:

  • resources (Array)

    array of resources

  • options (Symbol=>[Integer, String, Array])

    the key-value option pairs

Returns:

  • (Array)

    array of json objects



32
33
34
35
# File 'lib/rails_util/json_helper.rb', line 32

def json_collection(resource, **options)
  return render json: [], **options unless resource.any?
  json_with(resource, **options)
end

#json_empty(**options) ⇒ Object

Renders empty JSON object, along with other options

Parameters:

  • options (Symbol=>[Integer, String, Array])

    the key-value option pairs

Returns:

  • (Object)

    empty json object



40
41
42
# File 'lib/rails_util/json_helper.rb', line 40

def json_empty(**options)
  render json: {}, **options
end

#json_error(nested_path_or_obj, message = nil, **options) ⇒ Object

Renders JSON error response with 422 status, along with other options

Parameters:

  • nested_path_or_obj (String, Hash)

    the key-value option pairs

  • message (String) (defaults to: nil)

    the message to include in the error object

  • options (Symbol=>[Integer, String, Array])

    the key-value option pairs

Returns:

  • (Object)

    json error object



49
50
51
52
53
54
55
56
# File 'lib/rails_util/json_helper.rb', line 49

def json_error(nested_path_or_obj, message=nil, **options)
  error_obj = set_nested_path(nested_path_or_obj, message)
  render(
    json: { errors: error_obj },
    status: :unprocessable_entity,
    **options
  )
end

#json_success(path_or_obj, message = nil, **options) ⇒ Object

Renders JSON success response, along with other options

Parameters:

  • path_or_obj (String, Hash)

    the key-value option pairs

  • message (String=nil) (defaults to: nil)

    the message to include in the error object

  • options (Symbol=>[Integer, String, Array])

    the key-value option pairs

Returns:

  • (Object)

    json success object



63
64
65
66
67
68
# File 'lib/rails_util/json_helper.rb', line 63

def json_success(path_or_obj, message=nil, **options)
  render(
    json: set_nested_path(path_or_obj, message),
    **options
  )
end

#json_with(resource, **options) ⇒ Object

Renders JSON object, along with other options

Parameters:

  • resource (Object)

    ActiveRecord resource

  • options (Symbol=>[Integer, String, Array])

    the key-value option pairs

Returns:

  • (Object)

    json object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rails_util/json_helper.rb', line 10

def json_with(resource, **options)
  return json_empty(**options) unless resource
  root_key = resource.class.name.split('::').last.underscore

  return json_error(
    { root_key => map_base_to__error(resource.errors.messages) },
    **options
  ) if errors?(resource)

  return json_success(
    { root_key => {} },
    **options
  ) if destroyed?(resource)

  serialize_json_resource(resource, **options)
end

#serialize_json_resource(resource, **options) ⇒ Object

Renders serialized JSON resource

Parameters:

  • resource (Object)

    ActiveRecord resource

  • options (Symbol=>[Integer, String, Array])

    the key-value option pairs

Returns:

  • (Object)

    json resource object

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails_util/json_helper.rb', line 75

def serialize_json_resource(resource, **options)
  serializer_options = options[:serializer_options] || {}
  serializable_resource = ActiveModelSerializers::SerializableResource.new(
    resource,
    serializer_options.merge(scope: serialization_scope)
  )
  raise MissingSerializerError unless serializable_resource.serializer?
  serialized_obj = serializable_resource.serializer_instance.object
  type = options[:resource] || serialized_object_type(serialized_obj)

  data = { type: type, attributes: serializable_resource.serializer_instance }
  data[:additional_data] = options[:additional_data] if options && options[:additional_data]

  render json: { data: data }, **options
end