Skip to content

Commit b7bb984

Browse files
committed
fix typo in my previous message and add documentation for default behavior
1 parent 2926ed6 commit b7bb984

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

_site/guides/concepts/resources.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ <h4>
231231
<span class="k">end</span></code></pre></figure>
232232

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

236236
<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>
237237
<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>
@@ -261,6 +261,16 @@ <h4>
261261
<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>
262262
<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>
263263

264+
<p>As for resource defined guards, you can pass a symbol to guard the
265+
behavior globally. This can be used to globally delegate access control to a
266+
dedicated system.</p>
267+
268+
<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>
269+
270+
<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>
271+
<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>
272+
<span class="k">end</span></code></pre></figure>
273+
264274
<a class="anchor" id="customizing-display" />
265275
<a class="header" href="#customizing-display">
266276
<h4>

guides/concepts/resources.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ end
113113
{% endhighlight %}
114114

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

118118
{% highlight ruby %}
119119
attribute :name, :string, readable: :allowed?
@@ -142,6 +142,18 @@ self.attributes_sortable_by_default = false # default true
142142
self.attributes_schema_by_default = false # default true
143143
{% endhighlight %}
144144

145+
As for resource defined guards, you can pass a symbol to guard the
146+
behavior globally. This can be used to globally delegate access control to a
147+
dedicated system.
148+
149+
{% highlight ruby %}
150+
self.attributes_readable_by_default = :attribute_readable? # default true
151+
152+
def attribute_readable?(model_instance, attribute_name)
153+
PolicyChecker.new(model_instance).attribute_readable?(attribute_name)
154+
end
155+
{% endhighlight %}
156+
145157
{% include h.html tag="h4" text="2.3 Customizing Display" a="customizing-display" %}
146158

147159
Pass a block to `attribute` to customize display:

0 commit comments

Comments
 (0)