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
-
#json_collection(resource, **options) ⇒ Array
Renders array of JSON objects, along with other options, or an empty array if the array contains no values.
-
#json_empty(**options) ⇒ Object
Renders empty JSON object, along with other options.
-
#json_error(nested_path_or_obj, message = nil, **options) ⇒ Object
Renders JSON error response with
422status, along with other options. -
#json_success(path_or_obj, message = nil, **options) ⇒ Object
Renders JSON success response, along with other options.
-
#json_with(resource, **options) ⇒ Object
Renders JSON object, along with other options.
-
#serialize_json_resource(resource, **options) ⇒ Object
Renders serialized JSON resource.
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
32 33 34 35 |
# File 'lib/rails_util/json_helper.rb', line 32 def json_collection(resource, **) return render json: [], ** unless resource.any? json_with(resource, **) end |
#json_empty(**options) ⇒ Object
Renders empty JSON object, along with other options
40 41 42 |
# File 'lib/rails_util/json_helper.rb', line 40 def json_empty(**) render json: {}, ** end |
#json_error(nested_path_or_obj, message = nil, **options) ⇒ Object
Renders JSON error response with 422 status, along with other options
49 50 51 52 53 54 55 56 |
# File 'lib/rails_util/json_helper.rb', line 49 def json_error(nested_path_or_obj, =nil, **) error_obj = set_nested_path(nested_path_or_obj, ) render( json: { errors: error_obj }, status: :unprocessable_entity, ** ) end |
#json_success(path_or_obj, message = nil, **options) ⇒ Object
Renders JSON success response, along with other options
63 64 65 66 67 68 |
# File 'lib/rails_util/json_helper.rb', line 63 def json_success(path_or_obj, =nil, **) render( json: set_nested_path(path_or_obj, ), ** ) end |
#json_with(resource, **options) ⇒ Object
Renders JSON object, along with other options
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, **) return json_empty(**) unless resource root_key = resource.class.name.split('::').last.underscore return json_error( { root_key => map_base_to__error(resource.errors.) }, ** ) if errors?(resource) return json_success( { root_key => {} }, ** ) if destroyed?(resource) serialize_json_resource(resource, **) end |
#serialize_json_resource(resource, **options) ⇒ Object
Renders serialized JSON resource
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, **) = [:serializer_options] || {} serializable_resource = ActiveModelSerializers::SerializableResource.new( resource, .merge(scope: serialization_scope) ) raise MissingSerializerError unless serializable_resource.serializer? serialized_obj = serializable_resource.serializer_instance.object type = [:resource] || serialized_object_type(serialized_obj) data = { type: type, attributes: serializable_resource.serializer_instance } data[:additional_data] = [:additional_data] if && [:additional_data] render json: { data: data }, ** end |