-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
66 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class Meta | ||
def initialize(serializer) | ||
@object = serializer.object | ||
@scope = serializer.scope | ||
|
||
# Use the return value of the block unless it is nil. | ||
if serializer._meta.respond_to?(:call) | ||
@value = instance_eval(&serializer._meta) | ||
else | ||
@value = serializer._meta | ||
end | ||
end | ||
|
||
def as_json | ||
@value | ||
end | ||
|
||
protected | ||
|
||
attr_reader :object, :scope | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Meta | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
with_options instance_writer: false, instance_reader: true do |serializer| | ||
serializer.class_attribute :_meta # @api private | ||
end | ||
|
||
extend ActiveSupport::Autoload | ||
end | ||
|
||
module ClassMethods | ||
# Set the JSON API meta attribute of a serializer. | ||
# @example | ||
# class AdminAuthorSerializer < ActiveModel::Serializer | ||
# meta { stuff: 'value' } | ||
# @example | ||
# meta do | ||
# { comment_count: object.comments.count } | ||
# end | ||
def meta(value = nil, &block) | ||
self._meta = block || value | ||
end | ||
end | ||
end | ||
end | ||
end |