Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
feat(meta): Add metadata to rendered resources.
Browse files Browse the repository at this point in the history
Adding a root metadata key to responses. By default this will count the number
of items in the response collection. You are also able to pass in a `meta` key
to `#render_resource`. This will be merged into the response's `meta` key.
  • Loading branch information
maddiesch committed Feb 24, 2019
1 parent 577f825 commit 2916226
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Version 1.2.2

## Released February 23, 2019

###

- Feat: Allow passing in a metadata object to `#render_resource`.

---

# Version 1.2.1

## Released February 23, 2019
Expand Down
2 changes: 2 additions & 0 deletions lib/onsi/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ def inherited(subclass)
#
# - The other keys for opts will be passed directly the #render method.
def render_resource(resource, opts = {})
metadata = opts.delete(:meta)
version = opts.delete(:version) || self.class.render_version || Model::DEFAULT_API_VERSION
payload = Resource.render(resource, version)
payload[Resource::META_KEY].merge!(metadata) if metadata
render_options = {}
render_options[:json] = payload
render_options.merge!(opts)
Expand Down
11 changes: 7 additions & 4 deletions lib/onsi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ class Resource
RELATIONSHIPS_KEY = 'relationships'.freeze

##
# Root object meta key
# Root object data key
#
# @private
META_KEY = 'meta'.freeze
DATA_KEY = 'data'.freeze

##
# Root object data key
# Metadata key
#
# @private
DATA_KEY = 'data'.freeze
META_KEY = 'meta'.freeze

##
# Root object included key
Expand Down Expand Up @@ -95,6 +95,9 @@ def render(resource, version)
if included.any?
root[INCLUDED_KEY] = included
end
root[META_KEY] = {}.tap do |meta|
meta[:count] = resources.count if resources.respond_to?(:count)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/onsi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Onsi
##
# The current version of Onsi
VERSION = '1.2.1'.freeze
VERSION = '1.2.2'.freeze
end

0 comments on commit 2916226

Please sign in to comment.