mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-03-25 08:45:55 +00:00
Added support for Grape 0.10.x.
This commit is contained in:
parent
40e04ef23e
commit
ffd1770ab2
5 changed files with 43 additions and 27 deletions
|
|
@ -180,6 +180,10 @@ Enjoy :)
|
|||
|
||||
## Changelog
|
||||
|
||||
#### Next
|
||||
* Adds support for Grape 0.10.x
|
||||
|
||||
|
||||
#### v1.2.1
|
||||
* Adds support for active model serializer 0.9.x
|
||||
|
||||
|
|
|
|||
|
|
@ -9,12 +9,20 @@ module Grape
|
|||
attr_accessor :controller_name
|
||||
|
||||
def namespace_options
|
||||
if self.respond_to?(:inheritable_setting)
|
||||
inheritable_setting.namespace
|
||||
else
|
||||
settings[:namespace] ? settings[:namespace].options : {}
|
||||
end
|
||||
end
|
||||
|
||||
def route_options
|
||||
if self.respond_to?(:inheritable_setting)
|
||||
inheritable_setting.route
|
||||
else
|
||||
options[:route_options]
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(base)
|
||||
mattr_accessor :_serialization_scope
|
||||
|
|
@ -39,13 +47,9 @@ module Grape
|
|||
private
|
||||
|
||||
def set_meta_and_meta_key(meta)
|
||||
if meta.has_key?(:meta)
|
||||
Formatter::ActiveModelSerializers.meta = meta[:meta]
|
||||
if meta.has_key?(:meta_key)
|
||||
Formatter::ActiveModelSerializers.meta_key = meta[:meta_key]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Endpoint.send(:include, EndpointExtension)
|
||||
|
|
|
|||
|
|
@ -26,25 +26,19 @@ module Grape
|
|||
|
||||
def other_options
|
||||
options = {}
|
||||
if @meta_content_items
|
||||
if @meta_key
|
||||
key_option = @meta_key[:meta_key]
|
||||
@meta_key.delete(:meta_key)
|
||||
options[:meta_key] = key_option if key_option
|
||||
end
|
||||
meta_option = @meta_content_items[:meta]
|
||||
@meta_content_items.delete(:meta)
|
||||
options[key_option || :meta] = meta_option if meta_option
|
||||
end
|
||||
meta = Formatter::ActiveModelSerializers.meta.delete(:meta)
|
||||
meta_key = Formatter::ActiveModelSerializers.meta_key.delete(:meta_key)
|
||||
options[:meta_key] = meta_key if meta && meta_key
|
||||
options[meta_key || :meta] = meta if meta
|
||||
options
|
||||
end
|
||||
|
||||
def meta
|
||||
@meta_content_items || {}
|
||||
@meta || {}
|
||||
end
|
||||
|
||||
def meta=(meta_content)
|
||||
@meta_content_items = { meta: meta_content } if meta_content
|
||||
def meta=(value)
|
||||
@meta = value ? { meta: value } : nil
|
||||
end
|
||||
|
||||
def meta_key
|
||||
|
|
@ -52,7 +46,7 @@ module Grape
|
|||
end
|
||||
|
||||
def meta_key=(key)
|
||||
@meta_key = { meta_key: key } if key
|
||||
@meta_key = key ? { meta_key: key } : nil
|
||||
end
|
||||
|
||||
def build_options_from_endpoint(endpoint)
|
||||
|
|
@ -62,10 +56,14 @@ module Grape
|
|||
# array root is the innermost namespace name ('space') if there is one,
|
||||
# otherwise the route name (e.g. get 'name')
|
||||
def default_root(endpoint)
|
||||
innermost_scope = endpoint.settings.peek
|
||||
innermost_scope = if endpoint.respond_to?(:namespace_stackable)
|
||||
endpoint.namespace_stackable(:namespace).last
|
||||
else
|
||||
endpoint.settings.peek[:namespace]
|
||||
end
|
||||
|
||||
if innermost_scope[:namespace]
|
||||
innermost_scope[:namespace].space
|
||||
if innermost_scope
|
||||
innermost_scope.space
|
||||
else
|
||||
endpoint.options[:path][0].to_s.split('/')[-1]
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,7 +2,11 @@ require 'spec_helper'
|
|||
|
||||
describe 'Grape::EndpointExtension' do
|
||||
|
||||
if Grape::Util.const_defined?('InheritableSetting')
|
||||
subject { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, {path: '/', method: 'foo'}) }
|
||||
else
|
||||
subject { Grape::Endpoint.new({}, {path: '/', method: 'foo'}) }
|
||||
end
|
||||
|
||||
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,13 @@ describe Grape::Formatter::ActiveModelSerializers do
|
|||
|
||||
describe '.fetch_serializer' do
|
||||
let(:user) { User.new(first_name: 'John') }
|
||||
|
||||
if Grape::Util.const_defined?('InheritableSetting')
|
||||
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, {path: '/', method: 'foo'}) }
|
||||
else
|
||||
let(:endpoint) { Grape::Endpoint.new({}, {path: '/', method: 'foo'}) }
|
||||
end
|
||||
|
||||
let(:env) { { 'api.endpoint' => endpoint } }
|
||||
|
||||
before do
|
||||
|
|
|
|||
Loading…
Reference in a new issue