mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
352 lines
13 KiB
HTML
352 lines
13 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>
|
|
Module: ActiveModelSerializers::Jsonapi
|
|
|
|
— Documentation by YARD 0.8.7.6
|
|
|
|
</title>
|
|
|
|
<link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />
|
|
|
|
<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
hasFrames = window.top.frames.main ? true : false;
|
|
relpath = '../';
|
|
framesUrl = "../frames.html#!ActiveModelSerializers/Jsonapi.html";
|
|
</script>
|
|
|
|
|
|
<script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
|
|
|
|
<script type="text/javascript" charset="utf-8" src="../js/app.js"></script>
|
|
|
|
|
|
</head>
|
|
<body>
|
|
<div id="header">
|
|
<div id="menu">
|
|
|
|
<a href="../_index.html">Index (J)</a> »
|
|
<span class='title'><span class='object_link'><a href="../ActiveModelSerializers.html" title="ActiveModelSerializers (module)">ActiveModelSerializers</a></span></span>
|
|
»
|
|
<span class="title">Jsonapi</span>
|
|
|
|
|
|
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
|
</div>
|
|
|
|
<div id="search">
|
|
|
|
<a class="full_list_link" id="class_list_link"
|
|
href="../class_list.html">
|
|
Class List
|
|
</a>
|
|
|
|
<a class="full_list_link" id="method_list_link"
|
|
href="../method_list.html">
|
|
Method List
|
|
</a>
|
|
|
|
<a class="full_list_link" id="file_list_link"
|
|
href="../file_list.html">
|
|
File List
|
|
</a>
|
|
|
|
</div>
|
|
<div class="clear"></div>
|
|
</div>
|
|
|
|
<iframe id="search_frame"></iframe>
|
|
|
|
<div id="content"><h1>Module: ActiveModelSerializers::Jsonapi
|
|
|
|
|
|
|
|
</h1>
|
|
|
|
<dl class="box">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<dt class="r1 last">Defined in:</dt>
|
|
<dd class="r1 last">lib/active_model_serializers/register_jsonapi_renderer.rb</dd>
|
|
|
|
</dl>
|
|
<div class="clear"></div>
|
|
|
|
<h2>Overview</h2><div class="docstring">
|
|
<div class="discussion">
|
|
|
|
<p>Based on discussion in <a
|
|
href="https://github.com/rails/rails/pull/23712#issuecomment-184977238">github.com/rails/rails/pull/23712#issuecomment-184977238</a>,
|
|
the JSON API media type will have its own format/renderer.</p>
|
|
|
|
<p>> We recommend the media type be registered on its own as jsonapi when a
|
|
jsonapi Renderer and deserializer (Http::Parameters::DEFAULT_PARSERS) are
|
|
added.</p>
|
|
|
|
<p>Usage:</p>
|
|
|
|
<p>ActiveSupport.on_load(:action_controller) do</p>
|
|
|
|
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>active_model_serializers/register_jsonapi_renderer</span><span class='tstring_end'>'</span></span>
|
|
</code></pre>
|
|
|
|
<p>end</p>
|
|
|
|
<p>And then in controllers, use `render jsonapi: model` rather than `render
|
|
json: model, adapter: :json_api`.</p>
|
|
|
|
<p>For example, in a controller action, we can: respond_to do |format|</p>
|
|
|
|
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_format'>format</span><span class='period'>.</span><span class='id identifier rubyid_jsonapi'>jsonapi</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_render'>render</span> <span class='label'>jsonapi:</span> <span class='id identifier rubyid_model'>model</span> <span class='rbrace'>}</span>
|
|
</code></pre>
|
|
|
|
<p>end</p>
|
|
|
|
<p>or</p>
|
|
|
|
<p>render jsonapi: model</p>
|
|
|
|
<p>No wrapper format needed as it does not apply (i.e. no `wrap_parameters
|
|
format: [jsonapi]`)</p>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
<div class="tags">
|
|
|
|
|
|
</div><h2>Defined Under Namespace</h2>
|
|
<p class="children">
|
|
|
|
|
|
<strong class="modules">Modules:</strong> <span class='object_link'><a href="Jsonapi/ControllerSupport.html" title="ActiveModelSerializers::Jsonapi::ControllerSupport (module)">ControllerSupport</a></span>
|
|
|
|
|
|
|
|
|
|
</p>
|
|
|
|
<h2>Constant Summary</h2>
|
|
|
|
<dl class="constants">
|
|
|
|
<dt id="MEDIA_TYPE-constant" class="">MEDIA_TYPE =
|
|
|
|
</dt>
|
|
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>application/vnd.api+json</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
|
|
|
|
<dt id="HEADERS-constant" class="">HEADERS =
|
|
|
|
</dt>
|
|
<dd><pre class="code"><span class='lbrace'>{</span>
|
|
<span class='label'>response:</span> <span class='lbrace'>{</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>CONTENT_TYPE</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span> <span class='op'>=></span> <span class='const'>MEDIA_TYPE</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
|
<span class='label'>request:</span> <span class='lbrace'>{</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>ACCEPT</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span> <span class='op'>=></span> <span class='const'>MEDIA_TYPE</span> <span class='rbrace'>}</span>
|
|
<span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h2>
|
|
Class Method Summary
|
|
<small>(<a href="#" class="summary_toggle">collapse</a>)</small>
|
|
</h2>
|
|
|
|
<ul class="summary">
|
|
|
|
<li class="public ">
|
|
<span class="summary_signature">
|
|
|
|
<a href="#install-class_method" title="install (class method)">+ (Object) <strong>install</strong> </a>
|
|
|
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<span class="summary_desc"><div class='inline'></div></span>
|
|
|
|
</li>
|
|
|
|
|
|
<li class="public ">
|
|
<span class="summary_signature">
|
|
|
|
<a href="#parser-class_method" title="parser (class method)">+ (Object) <strong>parser</strong> </a>
|
|
|
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<span class="summary_desc"><div class='inline'>
|
|
<p>Proposal: should actually deserialize the JSON API params to the hash
|
|
format expected by `ActiveModel::Serializers::JSON`
|
|
actionpack/lib/action_dispatch/http/parameters.rb.</p>
|
|
</div></span>
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<div id="class_method_details" class="method_details_list">
|
|
<h2>Class Method Details</h2>
|
|
|
|
|
|
<div class="method_details first">
|
|
<h3 class="signature first" id="install-class_method">
|
|
|
|
+ (<tt>Object</tt>) <strong>install</strong>
|
|
|
|
|
|
|
|
|
|
|
|
</h3><table class="source_code">
|
|
<tr>
|
|
<td>
|
|
<pre class="lines">
|
|
|
|
|
|
32
|
|
33
|
|
34
|
|
35
|
|
36
|
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
|
42
|
|
43
|
|
44
|
|
45
|
|
46
|
|
47
|
|
48</pre>
|
|
</td>
|
|
<td>
|
|
<pre class="code"><span class="info file"># File 'lib/active_model_serializers/register_jsonapi_renderer.rb', line 32</span>
|
|
|
|
<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_install'>install</span>
|
|
<span class='comment'># actionpack/lib/action_dispatch/http/mime_types.rb
|
|
</span> <span class='const'>Mime</span><span class='op'>::</span><span class='const'>Type</span><span class='period'>.</span><span class='id identifier rubyid_register'>register</span> <span class='const'>MEDIA_TYPE</span><span class='comma'>,</span> <span class='symbol'>:jsonapi</span>
|
|
|
|
<span class='kw'>if</span> <span class='const'>Rails</span><span class='op'>::</span><span class='const'>VERSION</span><span class='op'>::</span><span class='const'>MAJOR</span> <span class='op'>>=</span> <span class='int'>5</span>
|
|
<span class='const'>ActionDispatch</span><span class='op'>::</span><span class='const'>Request</span><span class='period'>.</span><span class='id identifier rubyid_parameter_parsers'>parameter_parsers</span><span class='lbracket'>[</span><span class='symbol'>:jsonapi</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_parser'>parser</span>
|
|
<span class='kw'>else</span>
|
|
<span class='const'>ActionDispatch</span><span class='op'>::</span><span class='const'>ParamsParser</span><span class='op'>::</span><span class='const'>DEFAULT_PARSERS</span><span class='lbracket'>[</span><span class='const'>Mime</span><span class='lbracket'>[</span><span class='symbol'>:jsonapi</span><span class='rbracket'>]</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_parser'>parser</span>
|
|
<span class='kw'>end</span>
|
|
|
|
<span class='comment'># ref https://github.com/rails/rails/pull/21496
|
|
</span> <span class='const'>ActionController</span><span class='op'>::</span><span class='const'>Renderers</span><span class='period'>.</span><span class='id identifier rubyid_add'>add</span> <span class='symbol'>:jsonapi</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_json'>json</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='op'>|</span>
|
|
<span class='id identifier rubyid_json'>json</span> <span class='op'>=</span> <span class='id identifier rubyid_serialize_jsonapi'>serialize_jsonapi</span><span class='lparen'>(</span><span class='id identifier rubyid_json'>json</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span> <span class='kw'>unless</span> <span class='id identifier rubyid_json'>json</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span>
|
|
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_content_type'>content_type</span> <span class='op'>||=</span> <span class='const'>Mime</span><span class='lbracket'>[</span><span class='symbol'>:jsonapi</span><span class='rbracket'>]</span>
|
|
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_response_body'>response_body</span> <span class='op'>=</span> <span class='id identifier rubyid_json'>json</span>
|
|
<span class='kw'>end</span>
|
|
<span class='kw'>end</span></pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="method_details ">
|
|
<h3 class="signature " id="parser-class_method">
|
|
|
|
+ (<tt>Object</tt>) <strong>parser</strong>
|
|
|
|
|
|
|
|
|
|
|
|
</h3><div class="docstring">
|
|
<div class="discussion">
|
|
|
|
<p>Proposal: should actually deserialize the JSON API params to the hash
|
|
format expected by `ActiveModel::Serializers::JSON`
|
|
actionpack/lib/action_dispatch/http/parameters.rb</p>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
<div class="tags">
|
|
|
|
|
|
</div><table class="source_code">
|
|
<tr>
|
|
<td>
|
|
<pre class="lines">
|
|
|
|
|
|
53
|
|
54
|
|
55
|
|
56
|
|
57
|
|
58
|
|
59</pre>
|
|
</td>
|
|
<td>
|
|
<pre class="code"><span class="info file"># File 'lib/active_model_serializers/register_jsonapi_renderer.rb', line 53</span>
|
|
|
|
<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_parser'>parser</span>
|
|
<span class='id identifier rubyid_lambda'>lambda</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_body'>body</span><span class='op'>|</span>
|
|
<span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_body'>body</span><span class='rparen'>)</span>
|
|
<span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='lbrace'>{</span> <span class='symbol'>:_json</span> <span class='op'>=></span> <span class='id identifier rubyid_data'>data</span> <span class='rbrace'>}</span> <span class='kw'>unless</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
|
|
<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_with_indifferent_access'>with_indifferent_access</span>
|
|
<span class='kw'>end</span>
|
|
<span class='kw'>end</span></pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="footer">
|
|
Generated on Thu Jun 16 09:05:10 2016 by
|
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
0.8.7.6 (ruby-2.2.4).
|
|
</div>
|
|
|
|
</body>
|
|
</html> |