decent_exposure 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/decent_exposure.rb +5 -2
- data/rails/init.rb +1 -0
- data/spec/lib/decent_exposure_spec.rb +1 -0
- metadata +2 -4
- data/README.html +0 -96
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/decent_exposure.rb
CHANGED
@@ -4,11 +4,14 @@ module DecentExposure
|
|
4
4
|
klass.class_eval do
|
5
5
|
default_exposure(&closured_exposure)
|
6
6
|
end
|
7
|
+
super
|
7
8
|
end
|
8
9
|
|
10
|
+
attr_accessor :_default_exposure
|
11
|
+
|
9
12
|
def default_exposure(&block)
|
10
|
-
|
11
|
-
|
13
|
+
self._default_exposure = block if block_given?
|
14
|
+
_default_exposure
|
12
15
|
end
|
13
16
|
|
14
17
|
def expose(name, &block)
|
data/rails/init.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decent_exposure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Caudill
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-02-
|
13
|
+
date: 2010-02-25 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -40,7 +40,6 @@ executables: []
|
|
40
40
|
extensions: []
|
41
41
|
|
42
42
|
extra_rdoc_files:
|
43
|
-
- README.html
|
44
43
|
- README.md
|
45
44
|
files:
|
46
45
|
- COPYING
|
@@ -48,7 +47,6 @@ files:
|
|
48
47
|
- VERSION
|
49
48
|
- lib/decent_exposure.rb
|
50
49
|
- rails/init.rb
|
51
|
-
- README.html
|
52
50
|
has_rdoc: true
|
53
51
|
homepage: http://github.com/voxdolo/decent_exposure
|
54
52
|
licenses: []
|
data/README.html
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
2
|
-
<!DOCTYPE html PUBLIC
|
3
|
-
"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
|
4
|
-
"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
|
5
|
-
<html xmlns:svg='http://www.w3.org/2000/svg' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
|
6
|
-
<head><meta content='application/xhtml+xml;charset=utf-8' http-equiv='Content-type' /><title>DecentExposure</title></head>
|
7
|
-
<body>
|
8
|
-
<h1 id='decentexposure'>DecentExposure</h1>
|
9
|
-
|
10
|
-
<p><em>Copying over instance variables is bad, mmm-kay?</em></p>
|
11
|
-
|
12
|
-
<p>DecentExposure helps you program to an interface, rather than an implementation in your Rails controllers.</p>
|
13
|
-
|
14
|
-
<p>Sharing state via instance variables in controllers promotes close coupling with views. DecentExposure gives you a declarative manner of exposing an interface to the state that controllers contain, thereby decreasing coupling and improving your testability and overall design.</p>
|
15
|
-
|
16
|
-
<h2 id='installation'>Installation</h2>
|
17
|
-
|
18
|
-
<pre><code>gem install decent_exposure</code></pre>
|
19
|
-
|
20
|
-
<p>Configure your application to use it:</p>
|
21
|
-
|
22
|
-
<p>In <code>config/environment.rb</code>:</p>
|
23
|
-
|
24
|
-
<pre><code>config.gem 'decent_exposure'</code></pre>
|
25
|
-
|
26
|
-
<h2 id='the_particulars'>The Particulars</h2>
|
27
|
-
|
28
|
-
<p><code>expose</code> creates a method with the given name, evaluates the provided block (or intuits a value when no block is passed) and memoizes the result. This method is then declared as a <code>helper_method</code> so that views may have access to it and is made unroutable as an action.</p>
|
29
|
-
|
30
|
-
<h2 id='examples'>Examples</h2>
|
31
|
-
|
32
|
-
<h3 id='in_your_controllers'>In your controllers</h3>
|
33
|
-
|
34
|
-
<p>When no block is given, <code>expose</code> attempts to intuit which resource you want to acquire:</p>
|
35
|
-
|
36
|
-
<pre><code># Category.find(params[:category_id] || params[:id])
|
37
|
-
expose(:category)</code></pre>
|
38
|
-
|
39
|
-
<p>As the example shows, the symbol passed is used to guess the class name of the object you want an instance of. Almost every controller has one of these. In the RESTful controller paradigm, you might use this in <code>#show</code>, <code>#edit</code>, <code>#update</code> or <code>#destroy</code>.</p>
|
40
|
-
|
41
|
-
<p>In the slightly more complicated scenario, you need to find an instance of an object which doesn’t map cleanly to <code>Object#find</code>:</p>
|
42
|
-
|
43
|
-
<pre><code>expose(:product){ category.products.find(params[:id]) }</code></pre>
|
44
|
-
|
45
|
-
<p>In the RESTful controller paradigm, you’ll again find yourself using this in <code>#show</code>, <code>#edit</code>, <code>#update</code> or <code>#destroy</code>.</p>
|
46
|
-
|
47
|
-
<p>When the code has become complex enough to surpass a single line (and is not appropriate to extract into a model method), use the <code>do...end</code> style of block:</p>
|
48
|
-
|
49
|
-
<pre><code>expose(:associated_products) do
|
50
|
-
product.associated.tap do |associated_products|
|
51
|
-
present(associated_products, :with => AssociatedProductPresenter)
|
52
|
-
end
|
53
|
-
end</code></pre>
|
54
|
-
|
55
|
-
<h3 id='in_your_views'>In your views</h3>
|
56
|
-
|
57
|
-
<p>Use the product of those assignments like you would an instance variable or any other method you might normally have access to:</p>
|
58
|
-
|
59
|
-
<pre><code>= render bread_crumbs_for(category)
|
60
|
-
%h3#product_title= product.title
|
61
|
-
= render product
|
62
|
-
%h3 Associated Products
|
63
|
-
%ul
|
64
|
-
- associated_products.each do |associated_product|
|
65
|
-
%li= link_to(associated_product.title,product_path(associated_product))</code></pre>
|
66
|
-
|
67
|
-
<h3 id='custom_defaults'>Custom defaults</h3>
|
68
|
-
|
69
|
-
<p>DecentExposure provides opinionated default logic when <code>expose</code> is invoked without a block. It’s possible, however, to override this with your own custom default logic by passing a block accepting a single argument to the <code>default_exposure</code> method inside of a controller. The argument will be the string or symbol passed in to the <code>expose</code> call.</p>
|
70
|
-
|
71
|
-
<pre><code>class MyController < ApplicationController
|
72
|
-
default_exposure do |name|
|
73
|
-
ObjectCache.load(name.to_s)
|
74
|
-
end
|
75
|
-
end</code></pre>
|
76
|
-
|
77
|
-
<p>The given block will be invoked in the context of a controller instance. It is possible to provide a custom default for a descendant class without disturbing its ancestor classes in an inheritance heirachy.</p>
|
78
|
-
|
79
|
-
<p><strong>Caveat</strong>: Note that the simplest way to provide custom default <code>expose</code> logic for all of your controllers is to invoke <code>default_exposure</code> inside of <code>ApplicationController</code>. Due to the order of Rails’ initialization logic, naïve attempts to invoke it in <code>ActionController::Base</code> will have no affect. Use an initializer if you need this behavior.</p>
|
80
|
-
|
81
|
-
<h2 id='beware'>Beware</h2>
|
82
|
-
|
83
|
-
<p>This is an exceptionally simple tool, which provides a solitary solution. It must be used in conjunction with solid design approaches (“Program to an interface, not an implementation.”) and accepted best practices (e.g. Fat Model, Skinny Controller). In itself, it won’t heal a bad design. It is meant only to be a tool to use in improving the overall design of a Ruby on Rails system and moreover to provide a standard implementation for an emerging best practice.</p>
|
84
|
-
|
85
|
-
<h2 id='development'>Development</h2>
|
86
|
-
|
87
|
-
<h3 id='running_specs'>Running specs</h3>
|
88
|
-
|
89
|
-
<p><code>DecentExposure</code> has been developed with the philosophy that Ruby developers shouldn’t force their choice in RubyGems package managers on people consuming their code. As a side effect of that, if you attempt to run the specs on this application, you might get <code>no such file to load</code> errors. The short answer is that you can <code>export RUBYOPT='rubygems'</code> and be on about your way (for the long answer, see Ryan Tomayko’s <a href='http://tomayko.com/writings/require-rubygems-antipattern'>excellent treatise</a> on the subject).</p>
|
90
|
-
|
91
|
-
<h3 id='documentation_todo'>Documentation TODO</h3>
|
92
|
-
|
93
|
-
<ul>
|
94
|
-
<li>walk-through of an actual implementation (using an existing, popular OSS Rails app as an example refactor).</li>
|
95
|
-
</ul>
|
96
|
-
</body></html>
|