Skip to content

Commit

Permalink
fix typo in my previous message and add documentation for default beh…
Browse files Browse the repository at this point in the history
…avior
  • Loading branch information
Zooip committed Jul 20, 2022
1 parent 2926ed6 commit b7bb984
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion _site/guides/concepts/resources.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ <h4>
<span class="k">end</span></code></pre></figure>

<p>When guarding the <code class="highlighter-rouge">:readable</code> flag, the method can optionally accept the
model instance and the of the attribute being serialized as arguments:</p>
model instance and the name of the attribute being serialized as arguments:</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">attribute</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">:string</span><span class="p">,</span> <span class="ss">readable: :allowed?</span>
<span class="n">attribute</span> <span class="ss">:age</span><span class="p">,</span> <span class="ss">:integer</span><span class="p">,</span> <span class="ss">readable: :attribute_allowed?</span>
Expand Down Expand Up @@ -261,6 +261,16 @@ <h4>
<span class="nb">self</span><span class="p">.</span><span class="nf">attributes_sortable_by_default</span> <span class="o">=</span> <span class="kp">false</span> <span class="c1"># default true</span>
<span class="nb">self</span><span class="p">.</span><span class="nf">attributes_schema_by_default</span> <span class="o">=</span> <span class="kp">false</span> <span class="c1"># default true</span></code></pre></figure>

<p>As for resource defined guards, you can pass a symbol to guard the
behavior globally. This can be used to globally delegate access control to a
dedicated system.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="nb">self</span><span class="p">.</span><span class="nf">attributes_readable_by_default</span> <span class="o">=</span> <span class="ss">:attribute_readable?</span> <span class="c1"># default true</span>

<span class="k">def</span> <span class="nf">attribute_readable?</span><span class="p">(</span><span class="n">model_instance</span><span class="p">,</span> <span class="n">attribute_name</span><span class="p">)</span>
<span class="no">PolicyChecker</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">model_instance</span><span class="p">).</span><span class="nf">attribute_readable?</span><span class="p">(</span><span class="n">attribute_name</span><span class="p">)</span>
<span class="k">end</span></code></pre></figure>

<a class="anchor" id="customizing-display" />
<a class="header" href="#customizing-display">
<h4>
Expand Down
14 changes: 13 additions & 1 deletion guides/concepts/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ end
{% endhighlight %}

When guarding the `:readable` flag, the method can optionally accept the
model instance and the of the attribute being serialized as arguments:
model instance and the name of the attribute being serialized as arguments:

{% highlight ruby %}
attribute :name, :string, readable: :allowed?
Expand Down Expand Up @@ -142,6 +142,18 @@ self.attributes_sortable_by_default = false # default true
self.attributes_schema_by_default = false # default true
{% endhighlight %}

As for resource defined guards, you can pass a symbol to guard the
behavior globally. This can be used to globally delegate access control to a
dedicated system.

{% highlight ruby %}
self.attributes_readable_by_default = :attribute_readable? # default true

def attribute_readable?(model_instance, attribute_name)
PolicyChecker.new(model_instance).attribute_readable?(attribute_name)
end
{% endhighlight %}

{% include h.html tag="h4" text="2.3 Customizing Display" a="customizing-display" %}

Pass a block to `attribute` to customize display:
Expand Down

0 comments on commit b7bb984

Please sign in to comment.