dm-is-published 0.0.6 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +22 -0
- data/Guardfile +13 -0
- data/{LICENSE → LICENSE.txt} +1 -1
- data/README.rdoc +37 -5
- data/Rakefile +30 -56
- data/VERSION +1 -1
- data/dm-is-published.gemspec +93 -45
- data/docs/apple-touch-icon.png +0 -0
- data/docs/classes/DataMapper.html +81 -0
- data/docs/classes/DataMapper/Is.html +81 -0
- data/docs/classes/DataMapper/Is/Published.html +364 -0
- data/docs/classes/DataMapper/Is/Published/ClassMethods.html +237 -0
- data/docs/classes/DataMapper/Is/Published/InstanceMethods.html +132 -0
- data/docs/classes/DataMapper/Is/Published/ResourceInstanceMethods.html +133 -0
- data/docs/created.rid +4 -0
- data/docs/css/github.css +129 -0
- data/docs/css/main.css +333 -0
- data/docs/css/panel.css +384 -0
- data/docs/css/reset.css +48 -0
- data/docs/favicon.ico +0 -0
- data/docs/files/README_rdoc.html +226 -0
- data/docs/files/lib/dm-is-published_rb.html +84 -0
- data/docs/files/lib/is/published_rb.html +104 -0
- data/docs/i/arrows.png +0 -0
- data/docs/i/results_bg.png +0 -0
- data/docs/i/tree_bg.png +0 -0
- data/docs/index.html +13 -0
- data/docs/js/highlight.pack.js +1 -0
- data/docs/js/jquery-1.3.2.min.js +19 -0
- data/docs/js/jquery-effect.js +593 -0
- data/docs/js/main.js +24 -0
- data/docs/js/navigation.js +142 -0
- data/docs/js/search_index.js +1 -0
- data/docs/js/searchdoc.js +449 -0
- data/docs/js/searcher.js +228 -0
- data/docs/panel/index.html +73 -0
- data/docs/panel/links.html +12 -0
- data/docs/panel/tree.js +1 -0
- data/lib/dm-is-published.rb +3 -7
- data/lib/{dm-is-published/is → is}/published.rb +26 -10
- data/spec/{integration/published_spec.rb → dm-is-published_spec.rb} +48 -11
- data/spec/spec_helper.rb +12 -9
- metadata +264 -95
- data/.gitignore +0 -33
- data/History.rdoc +0 -4
- data/TODO +0 -0
- data/lib/dm-is-published/is/version.rb +0 -7
@@ -0,0 +1,81 @@
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
3
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<title>DataMapper::Is</title>
|
7
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
8
|
+
<link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
|
9
|
+
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
|
10
|
+
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
|
11
|
+
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
|
12
|
+
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
|
13
|
+
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
|
14
|
+
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>
|
15
|
+
|
16
|
+
</head>
|
17
|
+
|
18
|
+
<body>
|
19
|
+
<div class="banner">
|
20
|
+
|
21
|
+
<h1>
|
22
|
+
<span class="type">Module</span>
|
23
|
+
DataMapper::Is
|
24
|
+
|
25
|
+
</h1>
|
26
|
+
<ul class="files">
|
27
|
+
|
28
|
+
<li><a href="../../files/lib/is/published_rb.html">lib/is/published.rb</a></li>
|
29
|
+
|
30
|
+
</ul>
|
31
|
+
</div>
|
32
|
+
<div id="bodyContent">
|
33
|
+
<div id="content">
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
<!-- Namespace -->
|
46
|
+
<div class="sectiontitle">Namespace</div>
|
47
|
+
<ul>
|
48
|
+
|
49
|
+
<li>
|
50
|
+
<span class="type">MODULE</span>
|
51
|
+
<a href="Is/Published.html">DataMapper::Is::Published</a>
|
52
|
+
</li>
|
53
|
+
|
54
|
+
</ul>
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
<!-- Methods -->
|
77
|
+
</div>
|
78
|
+
|
79
|
+
</div>
|
80
|
+
</body>
|
81
|
+
</html>
|
@@ -0,0 +1,364 @@
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
3
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<title>DataMapper::Is::Published</title>
|
7
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
8
|
+
<link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
|
9
|
+
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
|
10
|
+
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
|
11
|
+
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
|
12
|
+
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
|
13
|
+
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
|
14
|
+
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>
|
15
|
+
|
16
|
+
</head>
|
17
|
+
|
18
|
+
<body>
|
19
|
+
<div class="banner">
|
20
|
+
|
21
|
+
<h1>
|
22
|
+
<span class="type">Module</span>
|
23
|
+
DataMapper::Is::Published
|
24
|
+
|
25
|
+
</h1>
|
26
|
+
<ul class="files">
|
27
|
+
|
28
|
+
<li><a href="../../../files/lib/is/published_rb.html">lib/is/published.rb</a></li>
|
29
|
+
|
30
|
+
</ul>
|
31
|
+
</div>
|
32
|
+
<div id="bodyContent">
|
33
|
+
<div id="content">
|
34
|
+
|
35
|
+
<div class="description">
|
36
|
+
|
37
|
+
<h1 id="label-dm-is-published">dm-is-published</h1>
|
38
|
+
|
39
|
+
<p>This plugin makes it very easy to add different states to your models, like
|
40
|
+
‘draft’ vs ‘live’. By default it also adds validations of the field value.</p>
|
41
|
+
|
42
|
+
<p>Originally inspired by the Rails plugin <code>acts_as_publishable</code> by
|
43
|
+
<strong>fr.ivolo.us</strong>.</p>
|
44
|
+
|
45
|
+
<h2 id="label-Installation">Installation</h2>
|
46
|
+
|
47
|
+
<pre>$ gem install dm-is-published</pre>
|
48
|
+
|
49
|
+
<p>Or add the gem to your Gemfile, and then run <code>bundle install</code>.</p>
|
50
|
+
|
51
|
+
<pre>gem 'dm-is-published', 'CURRENT_VERSION_NUMBER'</pre>
|
52
|
+
|
53
|
+
<h2 id="label-Getting+Started">Getting Started</h2>
|
54
|
+
|
55
|
+
<p>First of all, for a better understanding of this gem, make sure you study
|
56
|
+
the ‘<code>dm-is-published/spec/integration/published_spec.rb</code>’ file.</p>
|
57
|
+
<hr style="height: 2px">
|
58
|
+
|
59
|
+
<p>Require <code>dm-is-published</code> in your app.</p>
|
60
|
+
|
61
|
+
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'data_mapper'</span> <span class="ruby-comment"># must be required first</span>
|
62
|
+
<span class="ruby-identifier">require</span> <span class="ruby-string">'dm-is-published'</span>
|
63
|
+
</pre>
|
64
|
+
|
65
|
+
<p>Lets say we have an Article class, and each Article can have a current
|
66
|
+
state, ie: whether it’s Live, Draft or an Obituary awaiting the death of
|
67
|
+
someone famous (real or rumored)</p>
|
68
|
+
|
69
|
+
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Article</span>
|
70
|
+
<span class="ruby-identifier">include</span> <span class="ruby-constant">DataMapper</span><span class="ruby-operator">::</span><span class="ruby-constant">Resource</span>
|
71
|
+
<span class="ruby-identifier">property</span> :<span class="ruby-identifier">id</span>, <span class="ruby-constant">Serial</span>
|
72
|
+
<span class="ruby-identifier">property</span> :<span class="ruby-identifier">title</span>, <span class="ruby-constant">String</span>
|
73
|
+
<span class="ruby-operator">...</span><span class="ruby-operator"><</span><span class="ruby-identifier">snip</span><span class="ruby-operator">></span>
|
74
|
+
|
75
|
+
<span class="ruby-identifier">is</span> :<span class="ruby-identifier">published</span>
|
76
|
+
|
77
|
+
<span class="ruby-keyword">end</span>
|
78
|
+
</pre>
|
79
|
+
|
80
|
+
<p>Once you have your Article model we can create our Articles just as normal</p>
|
81
|
+
|
82
|
+
<pre class="ruby"><span class="ruby-constant">Article</span>.<span class="ruby-identifier">create</span>(:<span class="ruby-identifier">title</span> =<span class="ruby-operator">></span> <span class="ruby-string">'Example 1'</span>)
|
83
|
+
</pre>
|
84
|
+
|
85
|
+
<p>The instance of <code>Article.get(1)</code> now has the following things
|
86
|
+
for free:</p>
|
87
|
+
<ul><li>
|
88
|
+
<p>a <code>:publish_status</code> attribute with the value
|
89
|
+
<code>'live'</code>. Default choices are <code>[ :live, :draft, :hidden
|
90
|
+
]</code>.</p>
|
91
|
+
</li><li>
|
92
|
+
<p><code>:is_live?, :is_draft? or :is_hidden?</code> methods that returns
|
93
|
+
true/false based upon the state.</p>
|
94
|
+
</li><li>
|
95
|
+
<p><code>:save_as_live</code>, <code>:save_as_draft</code> or
|
96
|
+
<code>:save_as_hidden</code> converts the instance to the state and saves
|
97
|
+
it.</p>
|
98
|
+
</li><li>
|
99
|
+
<p><code>:publishable?</code> method that returns true for models where
|
100
|
+
<code>is :published </code> has been declared, but <strong>false</strong>
|
101
|
+
for those where it has not been declared.</p>
|
102
|
+
</li></ul>
|
103
|
+
|
104
|
+
<p>The Article class also gets a bit of new functionality:</p>
|
105
|
+
|
106
|
+
<pre class="ruby"><span class="ruby-constant">Article</span>.<span class="ruby-identifier">all</span>(:<span class="ruby-identifier">draft</span>) =<span class="ruby-operator">></span> <span class="ruby-identifier">finds</span> <span class="ruby-identifier">all</span> <span class="ruby-constant">Articles</span> <span class="ruby-identifier">with</span> :<span class="ruby-identifier">publish_status</span> = :<span class="ruby-identifier">draft</span>
|
107
|
+
|
108
|
+
<span class="ruby-constant">Article</span>.<span class="ruby-identifier">all</span>(:<span class="ruby-identifier">draft</span>, :<span class="ruby-identifier">author</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@author_joe</span> ) =<span class="ruby-operator">></span> <span class="ruby-identifier">finds</span> <span class="ruby-identifier">all</span> <span class="ruby-constant">Articles</span> <span class="ruby-identifier">with</span> :<span class="ruby-identifier">publish_status</span> = :<span class="ruby-identifier">draft</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">author</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Joe</span>
|
109
|
+
</pre>
|
110
|
+
<dl class="rdoc-list note-list"><dt>Todo
|
111
|
+
<dd>
|
112
|
+
<p>add more documentation here…</p>
|
113
|
+
</dd></dl>
|
114
|
+
|
115
|
+
<h2 id="label-Usage+Scenarios">Usage Scenarios</h2>
|
116
|
+
|
117
|
+
<p>In a Blog/Publishing scenario you could use it like this:</p>
|
118
|
+
|
119
|
+
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Article</span>
|
120
|
+
<span class="ruby-operator">...</span><span class="ruby-operator"><</span><span class="ruby-identifier">snip</span><span class="ruby-operator">></span><span class="ruby-operator">...</span>
|
121
|
+
|
122
|
+
<span class="ruby-identifier">is</span> :<span class="ruby-identifier">published</span> <span class="ruby-operator">:</span><span class="ruby-identifier">live</span>, :<span class="ruby-identifier">draft</span>, :<span class="ruby-identifier">hidden</span>
|
123
|
+
<span class="ruby-keyword">end</span>
|
124
|
+
</pre>
|
125
|
+
|
126
|
+
<p>Whereas in another scenario - like in a MenuItem model for a Restaurant -
|
127
|
+
you could use it like this:</p>
|
128
|
+
|
129
|
+
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">MenuItem</span>
|
130
|
+
<span class="ruby-operator">...</span><span class="ruby-operator"><</span><span class="ruby-identifier">snip</span><span class="ruby-operator">></span><span class="ruby-operator">...</span>
|
131
|
+
|
132
|
+
<span class="ruby-identifier">is</span> :<span class="ruby-identifier">published</span> <span class="ruby-operator">:</span><span class="ruby-identifier">on</span>, :<span class="ruby-identifier">off</span> <span class="ruby-comment"># the item is either on the menu or not</span>
|
133
|
+
<span class="ruby-keyword">end</span>
|
134
|
+
</pre>
|
135
|
+
|
136
|
+
<h2 id="label-RTFM+">RTFM </h2>
|
137
|
+
|
138
|
+
<p>As I said above, for a better understanding of this gem/plugin, make sure
|
139
|
+
you study the
|
140
|
+
‘<code>dm-is-published/spec/integration/published_spec.rb</code>’ file.</p>
|
141
|
+
|
142
|
+
<h2 id="label-Errors+%2F+Bugs">Errors / Bugs</h2>
|
143
|
+
|
144
|
+
<p>If something is not behaving intuitively, it is a bug, and should be
|
145
|
+
reported. Report it here: <a
|
146
|
+
href="http://github.com/kematzy/dm-is-published/issues">github.com/kematzy/dm-is-published/issues</a></p>
|
147
|
+
|
148
|
+
<h2 id="label-Credits">Credits</h2>
|
149
|
+
|
150
|
+
<p>Copyright © 2008 - 2013 [Kematzy at gmail]</p>
|
151
|
+
|
152
|
+
<p>Loosely based on the ActsAsPublishable plugin by [<a
|
153
|
+
href="http://fr.ivolo.us/posts/acts-as-publishable">fr.ivolo.us/posts/acts-as-publishable</a>]</p>
|
154
|
+
|
155
|
+
<h2 id="label-Licence">Licence</h2>
|
156
|
+
|
157
|
+
<p>Released under the MIT license.</p>
|
158
|
+
|
159
|
+
</div>
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
<!-- Namespace -->
|
172
|
+
<div class="sectiontitle">Namespace</div>
|
173
|
+
<ul>
|
174
|
+
|
175
|
+
<li>
|
176
|
+
<span class="type">MODULE</span>
|
177
|
+
<a href="Published/ClassMethods.html">DataMapper::Is::Published::ClassMethods</a>
|
178
|
+
</li>
|
179
|
+
|
180
|
+
<li>
|
181
|
+
<span class="type">MODULE</span>
|
182
|
+
<a href="Published/InstanceMethods.html">DataMapper::Is::Published::InstanceMethods</a>
|
183
|
+
</li>
|
184
|
+
|
185
|
+
<li>
|
186
|
+
<span class="type">MODULE</span>
|
187
|
+
<a href="Published/ResourceInstanceMethods.html">DataMapper::Is::Published::ResourceInstanceMethods</a>
|
188
|
+
</li>
|
189
|
+
|
190
|
+
</ul>
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
<!-- Method ref -->
|
196
|
+
<div class="sectiontitle">Methods</div>
|
197
|
+
<dl class="methods">
|
198
|
+
|
199
|
+
<dt>I</dt>
|
200
|
+
<dd>
|
201
|
+
<ul>
|
202
|
+
|
203
|
+
|
204
|
+
<li>
|
205
|
+
<a href="#method-i-is_published">is_published</a>
|
206
|
+
</li>
|
207
|
+
|
208
|
+
</ul>
|
209
|
+
</dd>
|
210
|
+
|
211
|
+
</dl>
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
<!-- Includes -->
|
216
|
+
<div class="sectiontitle">Included Modules</div>
|
217
|
+
<ul>
|
218
|
+
|
219
|
+
<li>
|
220
|
+
|
221
|
+
<a href="Published/InstanceMethods.html">
|
222
|
+
DataMapper::Is::Published::InstanceMethods
|
223
|
+
</a>
|
224
|
+
|
225
|
+
</li>
|
226
|
+
|
227
|
+
</ul>
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
<!-- Section constants -->
|
240
|
+
<div class="sectiontitle">Constants</div>
|
241
|
+
<table border='0' cellpadding='5'>
|
242
|
+
|
243
|
+
<tr valign='top'>
|
244
|
+
<td class="attr-name">VERSION</td>
|
245
|
+
<td>=</td>
|
246
|
+
<td class="attr-value">IO.read("#{File.dirname(__FILE__)}/../../VERSION").chomp</td>
|
247
|
+
</tr>
|
248
|
+
|
249
|
+
<tr valign='top'>
|
250
|
+
<td> </td>
|
251
|
+
<td colspan="2" class="attr-desc"></td>
|
252
|
+
</tr>
|
253
|
+
|
254
|
+
|
255
|
+
</table>
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
<!-- Methods -->
|
263
|
+
|
264
|
+
<div class="sectiontitle">Instance Public methods</div>
|
265
|
+
|
266
|
+
<div class="method">
|
267
|
+
<div class="title method-title" id="method-i-is_published">
|
268
|
+
|
269
|
+
<b>is_published</b>(*args)
|
270
|
+
|
271
|
+
<a href="../../../classes/DataMapper/Is/Published.html#method-i-is_published" name="method-i-is_published" class="permalink">Link</a>
|
272
|
+
</div>
|
273
|
+
|
274
|
+
|
275
|
+
<div class="description">
|
276
|
+
<p>method that adds a basic published status attribute to your model</p>
|
277
|
+
|
278
|
+
<h2 id="method-i-is_published-label-params+">params </h2>
|
279
|
+
<ul><li>
|
280
|
+
<p><code>states</code> - an array of 'states' as symbols or strings. ie:
|
281
|
+
:live, :draft, :hidden</p>
|
282
|
+
</li></ul>
|
283
|
+
|
284
|
+
<h4 id="method-i-is_published-label-Examples">Examples</h4>
|
285
|
+
|
286
|
+
<pre>is :published :on, :off
|
287
|
+
|
288
|
+
is :published %w(a b c d)</pre>
|
289
|
+
|
290
|
+
<p>@api public</p>
|
291
|
+
</div>
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
<div class="sourcecode">
|
299
|
+
|
300
|
+
<p class="source-link">
|
301
|
+
Source:
|
302
|
+
<a href="javascript:toggleSource('method-i-is_published_source')" id="l_method-i-is_published_source">show</a>
|
303
|
+
|
304
|
+
</p>
|
305
|
+
<div id="method-i-is_published_source" class="dyn-source">
|
306
|
+
<pre><span class="ruby-comment"># File lib/is/published.rb, line 133</span>
|
307
|
+
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">is_published</span>(*<span class="ruby-identifier">args</span>)
|
308
|
+
<span class="ruby-comment"># set default args if none passed in</span>
|
309
|
+
<span class="ruby-identifier">args</span> = [<span class="ruby-value">:live</span>, <span class="ruby-value">:draft</span>, <span class="ruby-value">:hidden</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">blank?</span>
|
310
|
+
<span class="ruby-identifier">args</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>)
|
311
|
+
|
312
|
+
<span class="ruby-comment"># the various publish states accepted.</span>
|
313
|
+
<span class="ruby-ivar">@publish_states</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">collect</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">state</span><span class="ruby-operator">|</span> <span class="ruby-identifier">state</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">downcase</span>.<span class="ruby-identifier">to_sym</span> }
|
314
|
+
<span class="ruby-ivar">@publish_states_for_validation</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">collect</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">state</span><span class="ruby-operator">|</span> <span class="ruby-identifier">state</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">downcase</span> }
|
315
|
+
|
316
|
+
<span class="ruby-identifier">extend</span> <span class="ruby-constant">DataMapper</span><span class="ruby-operator">::</span><span class="ruby-constant">Is</span><span class="ruby-operator">::</span><span class="ruby-constant">Published</span><span class="ruby-operator">::</span><span class="ruby-constant">ClassMethods</span>
|
317
|
+
<span class="ruby-identifier">include</span> <span class="ruby-constant">DataMapper</span><span class="ruby-operator">::</span><span class="ruby-constant">Is</span><span class="ruby-operator">::</span><span class="ruby-constant">Published</span><span class="ruby-operator">::</span><span class="ruby-constant">InstanceMethods</span>
|
318
|
+
|
319
|
+
<span class="ruby-comment"># do we have a :publish_status declared</span>
|
320
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">properties</span>.<span class="ruby-identifier">any?</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">p</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">==</span> <span class="ruby-value">:publish_status</span> }
|
321
|
+
|
322
|
+
<span class="ruby-comment"># set default value to first value in declaration or the given value</span>
|
323
|
+
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">properties</span>[<span class="ruby-value">:publish_status</span>].<span class="ruby-identifier">default</span>.<span class="ruby-identifier">blank?</span> <span class="ruby-operator">?</span> <span class="ruby-ivar">@publish_states</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">properties</span>[<span class="ruby-value">:publish_status</span>].<span class="ruby-identifier">default</span>
|
324
|
+
|
325
|
+
<span class="ruby-comment"># set the length to 10 if missing or if shorter than 5, otherwise use the given value</span>
|
326
|
+
<span class="ruby-identifier">l</span> = <span class="ruby-number">5</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">properties</span>[<span class="ruby-value">:publish_status</span>].<span class="ruby-identifier">length</span>.<span class="ruby-identifier">blank?</span>
|
327
|
+
<span class="ruby-identifier">l</span> = (<span class="ruby-identifier">properties</span>[<span class="ruby-value">:publish_status</span>].<span class="ruby-identifier">length</span> <span class="ruby-operator"><=</span> <span class="ruby-number">10</span> <span class="ruby-operator">?</span> <span class="ruby-number">10</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">properties</span>[<span class="ruby-value">:publish_status</span>].<span class="ruby-identifier">length</span>)
|
328
|
+
|
329
|
+
<span class="ruby-identifier">property</span> <span class="ruby-value">:publish_status</span>, <span class="ruby-constant">String</span>, <span class="ruby-value">:length</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">l</span>, <span class="ruby-value">:default</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">d</span>.<span class="ruby-identifier">to_s</span>
|
330
|
+
<span class="ruby-keyword">else</span>
|
331
|
+
<span class="ruby-comment"># no such property, so adding it with default values</span>
|
332
|
+
<span class="ruby-identifier">property</span> <span class="ruby-value">:publish_status</span>, <span class="ruby-constant">String</span>, <span class="ruby-value">:length</span> =<span class="ruby-operator">></span> <span class="ruby-number">10</span>, <span class="ruby-value">:default</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@publish_states</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">to_s</span>
|
333
|
+
<span class="ruby-keyword">end</span>
|
334
|
+
|
335
|
+
<span class="ruby-comment"># create the state specific instance methods</span>
|
336
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">publish_states</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">state</span><span class="ruby-operator">|</span>
|
337
|
+
<span class="ruby-identifier">define_method</span>(<span class="ruby-node">"is_#{state}?"</span>) <span class="ruby-keyword">do</span>
|
338
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">publish_status</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">state</span>.<span class="ruby-identifier">to_s</span>
|
339
|
+
<span class="ruby-keyword">end</span>
|
340
|
+
<span class="ruby-identifier">define_method</span>(<span class="ruby-node">"save_as_#{state}"</span>) <span class="ruby-keyword">do</span>
|
341
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">publish_status</span> = <span class="ruby-identifier">state</span>.<span class="ruby-identifier">to_s</span>
|
342
|
+
<span class="ruby-identifier">save</span>
|
343
|
+
<span class="ruby-keyword">end</span>
|
344
|
+
<span class="ruby-keyword">end</span>
|
345
|
+
|
346
|
+
<span class="ruby-comment"># ensure we are always saving publish_status values as strings</span>
|
347
|
+
<span class="ruby-identifier">before</span> <span class="ruby-value">:valid?</span> <span class="ruby-keyword">do</span>
|
348
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">publish_status</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">publish_status</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:publish_status</span>)
|
349
|
+
<span class="ruby-keyword">end</span>
|
350
|
+
|
351
|
+
<span class="ruby-identifier">validates_within</span> <span class="ruby-value">:publish_status</span>,
|
352
|
+
<span class="ruby-value">:set</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@publish_states_for_validation</span>,
|
353
|
+
<span class="ruby-value">:message</span> =<span class="ruby-operator">></span> <span class="ruby-node">"The publish_status value can only be one of these values: [ #{@publish_states_for_validation.join(', ')} ]"</span>
|
354
|
+
|
355
|
+
<span class="ruby-keyword">end</span></pre>
|
356
|
+
</div>
|
357
|
+
</div>
|
358
|
+
|
359
|
+
</div>
|
360
|
+
</div>
|
361
|
+
|
362
|
+
</div>
|
363
|
+
</body>
|
364
|
+
</html>
|
@@ -0,0 +1,237 @@
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
3
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<title>DataMapper::Is::Published::ClassMethods</title>
|
7
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
8
|
+
<link rel="stylesheet" href="../../../../css/reset.css" type="text/css" media="screen" />
|
9
|
+
<link rel="stylesheet" href="../../../../css/main.css" type="text/css" media="screen" />
|
10
|
+
<link rel="stylesheet" href="../../../../css/github.css" type="text/css" media="screen" />
|
11
|
+
<script src="../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
|
12
|
+
<script src="../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
|
13
|
+
<script src="../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
|
14
|
+
<script src="../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>
|
15
|
+
|
16
|
+
</head>
|
17
|
+
|
18
|
+
<body>
|
19
|
+
<div class="banner">
|
20
|
+
|
21
|
+
<h1>
|
22
|
+
<span class="type">Module</span>
|
23
|
+
DataMapper::Is::Published::ClassMethods
|
24
|
+
|
25
|
+
</h1>
|
26
|
+
<ul class="files">
|
27
|
+
|
28
|
+
<li><a href="../../../../files/lib/is/published_rb.html">lib/is/published.rb</a></li>
|
29
|
+
|
30
|
+
</ul>
|
31
|
+
</div>
|
32
|
+
<div id="bodyContent">
|
33
|
+
<div id="content">
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
<!-- Method ref -->
|
49
|
+
<div class="sectiontitle">Methods</div>
|
50
|
+
<dl class="methods">
|
51
|
+
|
52
|
+
<dt>A</dt>
|
53
|
+
<dd>
|
54
|
+
<ul>
|
55
|
+
|
56
|
+
|
57
|
+
<li>
|
58
|
+
<a href="#method-i-all">all</a>
|
59
|
+
</li>
|
60
|
+
|
61
|
+
</ul>
|
62
|
+
</dd>
|
63
|
+
|
64
|
+
<dt>P</dt>
|
65
|
+
<dd>
|
66
|
+
<ul>
|
67
|
+
|
68
|
+
|
69
|
+
<li>
|
70
|
+
<a href="#method-i-publish_states_as_json">publish_states_as_json</a>
|
71
|
+
</li>
|
72
|
+
|
73
|
+
</ul>
|
74
|
+
</dd>
|
75
|
+
|
76
|
+
</dl>
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
<!-- Section attributes -->
|
94
|
+
<div class="sectiontitle">Attributes</div>
|
95
|
+
<table border='0' cellpadding='5'>
|
96
|
+
|
97
|
+
<tr valign='top'>
|
98
|
+
<td class='attr-rw'>
|
99
|
+
[R]
|
100
|
+
</td>
|
101
|
+
<td class='attr-name'>publish_states</td>
|
102
|
+
<td class='attr-desc'></td>
|
103
|
+
</tr>
|
104
|
+
|
105
|
+
<tr valign='top'>
|
106
|
+
<td class='attr-rw'>
|
107
|
+
[R]
|
108
|
+
</td>
|
109
|
+
<td class='attr-name'>publish_states_for_validation</td>
|
110
|
+
<td class='attr-desc'></td>
|
111
|
+
</tr>
|
112
|
+
|
113
|
+
</table>
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
<!-- Methods -->
|
118
|
+
|
119
|
+
<div class="sectiontitle">Instance Public methods</div>
|
120
|
+
|
121
|
+
<div class="method">
|
122
|
+
<div class="title method-title" id="method-i-all">
|
123
|
+
|
124
|
+
<b>all</b>(*args)
|
125
|
+
|
126
|
+
<a href="../../../../classes/DataMapper/Is/Published/ClassMethods.html#method-i-all" name="method-i-all" class="permalink">Link</a>
|
127
|
+
</div>
|
128
|
+
|
129
|
+
|
130
|
+
<div class="description">
|
131
|
+
<p>Overriding the normal <a href="ClassMethods.html#method-i-all">all</a>
|
132
|
+
method to add some extra sugar.</p>
|
133
|
+
|
134
|
+
<h4 id="method-i-all-label-Examples">Examples</h4>
|
135
|
+
|
136
|
+
<pre class="ruby"><span class="ruby-constant">Article</span>.<span class="ruby-identifier">all</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">returns</span> <span class="ruby-identifier">all</span> <span class="ruby-constant">Articles</span> <span class="ruby-identifier">as</span> <span class="ruby-identifier">usual</span>
|
137
|
+
|
138
|
+
<span class="ruby-constant">Article</span>.<span class="ruby-identifier">all</span>( :<span class="ruby-identifier">publish_status</span> =<span class="ruby-operator">></span> :<span class="ruby-identifier">live</span> ) =<span class="ruby-operator">></span> <span class="ruby-identifier">returns</span> <span class="ruby-identifier">all</span> <span class="ruby-constant">Articles</span> <span class="ruby-identifier">with</span> :<span class="ruby-identifier">publish_status</span> <span class="ruby-operator">==</span> :<span class="ruby-identifier">lve</span>
|
139
|
+
|
140
|
+
<span class="ruby-constant">Article</span>.<span class="ruby-identifier">all</span>(:<span class="ruby-identifier">draft</span>) =<span class="ruby-operator">></span> <span class="ruby-identifier">returns</span> <span class="ruby-identifier">all</span> <span class="ruby-constant">Articles</span> <span class="ruby-identifier">with</span> :<span class="ruby-identifier">publish_status</span> <span class="ruby-operator">==</span> :<span class="ruby-identifier">draft</span>
|
141
|
+
|
142
|
+
<span class="ruby-constant">Article</span>.<span class="ruby-identifier">all</span>(:<span class="ruby-identifier">draft</span>, :<span class="ruby-identifier">author</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@author_joe</span> ) =<span class="ruby-operator">></span> <span class="ruby-identifier">finds</span> <span class="ruby-identifier">all</span> <span class="ruby-constant">Articles</span> <span class="ruby-identifier">with</span> :<span class="ruby-identifier">publish_status</span> = :<span class="ruby-identifier">draft</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">author</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Joe</span>
|
143
|
+
</pre>
|
144
|
+
|
145
|
+
<p>@api public/private</p>
|
146
|
+
</div>
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
<div class="sourcecode">
|
154
|
+
|
155
|
+
<p class="source-link">
|
156
|
+
Source:
|
157
|
+
<a href="javascript:toggleSource('method-i-all_source')" id="l_method-i-all_source">show</a>
|
158
|
+
|
159
|
+
</p>
|
160
|
+
<div id="method-i-all_source" class="dyn-source">
|
161
|
+
<pre><span class="ruby-comment"># File lib/is/published.rb, line 216</span>
|
162
|
+
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">all</span>(*<span class="ruby-identifier">args</span>)
|
163
|
+
<span class="ruby-comment"># incoming can either be: </span>
|
164
|
+
<span class="ruby-comment"># -- nil (nothing passed in, so just use super )</span>
|
165
|
+
<span class="ruby-comment"># -- (Hash) => all(:key => "value") ( normal operations, so just pass on the Hash ) </span>
|
166
|
+
<span class="ruby-comment"># -- (Symbol) => all(:draft) ( just get the symbol, )</span>
|
167
|
+
<span class="ruby-comment"># -- (Symbol, Hash ) => :draft, { extra options }</span>
|
168
|
+
<span class="ruby-comment"># -- (DataMapper somethings) => leave it alone</span>
|
169
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">empty?</span>
|
170
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">super</span>
|
171
|
+
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
|
172
|
+
<span class="ruby-comment"># set the from args Array, remove first item if Symbol, and then check for 2nd item's presence</span>
|
173
|
+
<span class="ruby-identifier">state</span>, <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>.<span class="ruby-identifier">to_s</span>, (<span class="ruby-identifier">args</span>.<span class="ruby-identifier">blank?</span> <span class="ruby-operator">?</span> {} <span class="ruby-operator">:</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>)
|
174
|
+
<span class="ruby-comment"># puts " and state=[#{state}] and options=[#{options.class}] options.inspect=[#{options.inspect}] [#{__FILE__}:#{__LINE__}]"</span>
|
175
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">super</span>({ <span class="ruby-value">:publish_status</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">state</span> }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>) )
|
176
|
+
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
|
177
|
+
<span class="ruby-comment"># puts "dm-is-published args.first was a HASH ] [#{__FILE__}:#{__LINE__}]"</span>
|
178
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">super</span>(<span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>)
|
179
|
+
<span class="ruby-keyword">else</span>
|
180
|
+
<span class="ruby-comment"># puts "dm-is-published (ELSE) [#{__FILE__}:#{__LINE__}]"</span>
|
181
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">super</span>
|
182
|
+
<span class="ruby-keyword">end</span>
|
183
|
+
<span class="ruby-keyword">end</span></pre>
|
184
|
+
</div>
|
185
|
+
</div>
|
186
|
+
|
187
|
+
</div>
|
188
|
+
|
189
|
+
<div class="method">
|
190
|
+
<div class="title method-title" id="method-i-publish_states_as_json">
|
191
|
+
|
192
|
+
<b>publish_states_as_json</b>()
|
193
|
+
|
194
|
+
<a href="../../../../classes/DataMapper/Is/Published/ClassMethods.html#method-i-publish_states_as_json" name="method-i-publish_states_as_json" class="permalink">Link</a>
|
195
|
+
</div>
|
196
|
+
|
197
|
+
|
198
|
+
<div class="description">
|
199
|
+
<p>Returns a JSON representation of the publish states, where each state is
|
200
|
+
represented as a lowercase key and an uppercase value.</p>
|
201
|
+
|
202
|
+
<h4 id="method-i-publish_states_as_json-label-Examples">Examples</h4>
|
203
|
+
|
204
|
+
<pre class="ruby"><span class="ruby-constant">Model</span>.<span class="ruby-identifier">publish_states_as_json</span>
|
205
|
+
|
206
|
+
=<span class="ruby-operator">></span> { <span class="ruby-string">'live'</span><span class="ruby-operator">:</span> <span class="ruby-string">'LIVE'</span>,<span class="ruby-string">'draft'</span><span class="ruby-operator">:</span> <span class="ruby-string">'DRAFT'</span>,<span class="ruby-string">'hidden'</span><span class="ruby-operator">:</span> <span class="ruby-string">'HIDDEN'</span> }
|
207
|
+
</pre>
|
208
|
+
|
209
|
+
<p>@api public</p>
|
210
|
+
</div>
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
<div class="sourcecode">
|
218
|
+
|
219
|
+
<p class="source-link">
|
220
|
+
Source:
|
221
|
+
<a href="javascript:toggleSource('method-i-publish_states_as_json_source')" id="l_method-i-publish_states_as_json_source">show</a>
|
222
|
+
|
223
|
+
</p>
|
224
|
+
<div id="method-i-publish_states_as_json_source" class="dyn-source">
|
225
|
+
<pre><span class="ruby-comment"># File lib/is/published.rb, line 197</span>
|
226
|
+
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">publish_states_as_json</span>
|
227
|
+
<span class="ruby-node">%Q[{ #{self.publish_states_for_validation.collect{ |state| "'#{state.to_s.downcase}': '#{state.to_s.upcase}'" }.join(',')} }]</span>
|
228
|
+
<span class="ruby-keyword">end</span></pre>
|
229
|
+
</div>
|
230
|
+
</div>
|
231
|
+
|
232
|
+
</div>
|
233
|
+
</div>
|
234
|
+
|
235
|
+
</div>
|
236
|
+
</body>
|
237
|
+
</html>
|