model_loader 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ *.sqlite3
6
+ .project
data/Rakefile CHANGED
@@ -1,2 +1,21 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+ desc "Test the model_loader plugin."
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << "test"
11
+ t.test_files = FileList['test/*_test.rb']
12
+ end
13
+
14
+ desc "Generate documentation for the model_loader plugin."
15
+ Rake::RDocTask.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ModelLoader'
18
+ rdoc.options << '--line-numbers' << '--inline-source'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
data/lib/model_loader.rb CHANGED
@@ -53,9 +53,9 @@ module ModelLoader
53
53
  self.class_eval <<-METHOD, __FILE__, __LINE__ + 1
54
54
  def find_#{name}
55
55
  if params[:#{name}_id]
56
- @#{name} = #{name.capitalize}.find(params[:#{name}_id])
56
+ @#{name} = #{name.classify}.find(params[:#{name}_id])
57
57
  else
58
- @#{name} = #{name.capitalize}.find(params[:id])
58
+ @#{name} = #{name.classify}.find(params[:id])
59
59
  end
60
60
  rescue
61
61
  render :file => "#{::Rails.root.to_s}/public/404.html", :layout => false, :status => 404
@@ -1,3 +1,3 @@
1
1
  module ModelLoader
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,171 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: ModelLoader</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">ModelLoader</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/model_loader/railtie_rb.html">
59
+ lib/model_loader/railtie.rb
60
+ </a>
61
+ <br />
62
+ <a href="../files/lib/model_loader/version_rb.html">
63
+ lib/model_loader/version.rb
64
+ </a>
65
+ <br />
66
+ <a href="../files/lib/model_loader_rb.html">
67
+ lib/model_loader.rb
68
+ </a>
69
+ <br />
70
+ </td>
71
+ </tr>
72
+
73
+ </table>
74
+ </div>
75
+ <!-- banner header -->
76
+
77
+ <div id="bodyContent">
78
+
79
+
80
+
81
+ <div id="contextContent">
82
+
83
+ <div id="description">
84
+ <p>
85
+ Abstracts a common (at least for me) pattern in Rails applications. When
86
+ creating controllers that interact with models it is common to have methods
87
+ called <tt>find_&lt;model_name&gt;</tt> for your models. This module
88
+ defines those <tt>find_*</tt> methods for you.
89
+ </p>
90
+ <p>
91
+ When executing the <tt>find_*</tt> methods, if a matching record can not be
92
+ found, the user is redirected to the 404 page. The find method first tries
93
+ to find the object using the <tt>&lt;model_name&gt;_id</tt> parameter if it
94
+ exists. If not, it tries to find the object using the <tt>id</tt>
95
+ parameter.
96
+ </p>
97
+ <p>
98
+ For example, given a model named Post, <tt>find_post</tt> will first check
99
+ to see if <tt>params[:post_id]</tt> exists. If it does, it will look up the
100
+ model with the value of that parameter. If it doesn&#8216;t exist,
101
+ <tt>params[:id]</tt> will be used insted.
102
+ </p>
103
+ <p>
104
+ The module is activated by calling the <tt>can_find_models</tt> method:
105
+ </p>
106
+ <pre>
107
+ class ApplicationController &lt; ActionController::Base
108
+ can_find_models
109
+ end
110
+ </pre>
111
+ <p>
112
+ Valid options are :only and :except:
113
+ </p>
114
+ <pre>
115
+ class ApplicationController &lt; ActionController
116
+ can_find_models :only =&gt; 'Person'
117
+ end
118
+ </pre>
119
+
120
+ </div>
121
+
122
+
123
+ </div>
124
+
125
+
126
+ </div>
127
+
128
+
129
+ <!-- if includes -->
130
+
131
+ <div id="section">
132
+
133
+ <div id="class-list">
134
+ <h3 class="section-bar">Classes and Modules</h3>
135
+
136
+ Module <a href="ModelLoader/ClassMethods.html" class="link">ModelLoader::ClassMethods</a><br />
137
+ Class <a href="ModelLoader/Railtie.html" class="link">ModelLoader::Railtie</a><br />
138
+
139
+ </div>
140
+
141
+ <div id="constants-list">
142
+ <h3 class="section-bar">Constants</h3>
143
+
144
+ <div class="name-list">
145
+ <table summary="Constants">
146
+ <tr class="top-aligned-row context-row">
147
+ <td class="context-item-name">VERSION</td>
148
+ <td>=</td>
149
+ <td class="context-item-value">&quot;0.0.2&quot;</td>
150
+ </tr>
151
+ </table>
152
+ </div>
153
+ </div>
154
+
155
+
156
+
157
+
158
+
159
+
160
+ <!-- if method_list -->
161
+
162
+
163
+ </div>
164
+
165
+
166
+ <div id="validator-badges">
167
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
168
+ </div>
169
+
170
+ </body>
171
+ </html>
@@ -0,0 +1,165 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: ModelLoader::ClassMethods</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">ModelLoader::ClassMethods</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../../files/lib/model_loader_rb.html">
59
+ lib/model_loader.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+
76
+
77
+ </div>
78
+
79
+ <div id="method-list">
80
+ <h3 class="section-bar">Methods</h3>
81
+
82
+ <div class="name-list">
83
+ <a href="#M000001">can_find_models</a>&nbsp;&nbsp;
84
+ </div>
85
+ </div>
86
+
87
+ </div>
88
+
89
+
90
+ <!-- if includes -->
91
+
92
+ <div id="section">
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ <!-- if method_list -->
102
+ <div id="methods">
103
+ <h3 class="section-bar">Public Instance methods</h3>
104
+
105
+ <div id="method-M000001" class="method-detail">
106
+ <a name="M000001"></a>
107
+
108
+ <div class="method-heading">
109
+ <a href="#M000001" class="method-signature">
110
+ <span class="method-name">can_find_models</span><span class="method-args">(options = {})</span>
111
+ </a>
112
+ </div>
113
+
114
+ <div class="method-description">
115
+ <p>
116
+ Defines methods named find_&lt;model_name&gt; for each model in the
117
+ application. The actual classes the methods are generated for can be
118
+ controlled using the only or except options.
119
+ </p>
120
+ <p><a class="source-toggle" href="#"
121
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
122
+ <div class="method-source-code" id="M000001-source">
123
+ <pre>
124
+ <span class="ruby-comment cmt"># File lib/model_loader.rb, line 33</span>
125
+ 33: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">can_find_models</span>(<span class="ruby-identifier">options</span> = {})
126
+ 34: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>
127
+ 35:
128
+ 36: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:only</span>]
129
+ 37: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:only</span>] = [<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:only</span>]].<span class="ruby-identifier">flatten</span>.<span class="ruby-identifier">compact</span>
130
+ 38: <span class="ruby-identifier">models</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:only</span>].<span class="ruby-identifier">map</span> <span class="ruby-operator">&amp;</span><span class="ruby-identifier">:to_s</span>
131
+ 39: <span class="ruby-keyword kw">else</span>
132
+ 40: <span class="ruby-identifier">models</span> = <span class="ruby-constant">Dir</span>[<span class="ruby-node">&quot;#{::Rails.root.to_s}/app/models/*&quot;</span>].<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
133
+ 41: <span class="ruby-constant">File</span>.<span class="ruby-identifier">basename</span>(<span class="ruby-identifier">file</span>, <span class="ruby-value str">&quot;.*&quot;</span>).<span class="ruby-identifier">classify</span>
134
+ 42: <span class="ruby-keyword kw">end</span>
135
+ 43:
136
+ 44: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:except</span>]
137
+ 45: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:except</span>] = [<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:except</span>]].<span class="ruby-identifier">flatten</span>.<span class="ruby-identifier">compact</span>
138
+ 46: <span class="ruby-identifier">models</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:except</span>].<span class="ruby-identifier">map</span> <span class="ruby-operator">&amp;</span><span class="ruby-identifier">:to_s</span>
139
+ 47: <span class="ruby-keyword kw">end</span>
140
+ 48: <span class="ruby-keyword kw">end</span>
141
+ 49:
142
+ 50: <span class="ruby-identifier">models</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">model</span><span class="ruby-operator">|</span>
143
+ 51: <span class="ruby-identifier">name</span> = <span class="ruby-identifier">model</span>.<span class="ruby-identifier">underscore</span>
144
+ 52: <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-node">&quot;find_#{name}&quot;</span>
145
+ 53: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-value str">&quot;def find_\#{name}\nif params[:\#{name}_id]\n@\#{name} = \#{name.capitalize}.find(params[:\#{name}_id])\nelse\n@\#{name} = \#{name.capitalize}.find(params[:id])\nend\nrescue\nrender :file =&gt; \&quot;\#{::Rails.root.to_s}/public/404.html\&quot;, :layout =&gt; false, :status =&gt; 404\nend\n&quot;</span>, <span class="ruby-keyword kw">__FILE__</span>, <span class="ruby-keyword kw">__LINE__</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
146
+ 54: <span class="ruby-keyword kw">end</span>
147
+ 55: <span class="ruby-keyword kw">end</span>
148
+ </pre>
149
+ </div>
150
+ </div>
151
+ </div>
152
+
153
+
154
+ </div>
155
+
156
+
157
+ </div>
158
+
159
+
160
+ <div id="validator-badges">
161
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
162
+ </div>
163
+
164
+ </body>
165
+ </html>
@@ -0,0 +1,146 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Class: ModelLoader::Railtie</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Class</strong></td>
53
+ <td class="class-name-in-header">ModelLoader::Railtie</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../../files/lib/model_loader/railtie_rb.html">
59
+ lib/model_loader/railtie.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ <tr class="top-aligned-row">
66
+ <td><strong>Parent:</strong></td>
67
+ <td>
68
+ Rails::Railtie
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </div>
73
+ <!-- banner header -->
74
+
75
+ <div id="bodyContent">
76
+
77
+
78
+
79
+ <div id="contextContent">
80
+
81
+
82
+
83
+ </div>
84
+
85
+ <div id="method-list">
86
+ <h3 class="section-bar">Methods</h3>
87
+
88
+ <div class="name-list">
89
+ <a href="#M000002">insert</a>&nbsp;&nbsp;
90
+ </div>
91
+ </div>
92
+
93
+ </div>
94
+
95
+
96
+ <!-- if includes -->
97
+
98
+ <div id="section">
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ <!-- if method_list -->
108
+ <div id="methods">
109
+ <h3 class="section-bar">Public Class methods</h3>
110
+
111
+ <div id="method-M000002" class="method-detail">
112
+ <a name="M000002"></a>
113
+
114
+ <div class="method-heading">
115
+ <a href="#M000002" class="method-signature">
116
+ <span class="method-name">insert</span><span class="method-args">()</span>
117
+ </a>
118
+ </div>
119
+
120
+ <div class="method-description">
121
+ <p><a class="source-toggle" href="#"
122
+ onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
123
+ <div class="method-source-code" id="M000002-source">
124
+ <pre>
125
+ <span class="ruby-comment cmt"># File lib/model_loader/railtie.rb, line 15</span>
126
+ 15: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">insert</span>
127
+ 16: <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">:include</span>, <span class="ruby-constant">ModelLoader</span>)
128
+ 17: <span class="ruby-keyword kw">end</span>
129
+ </pre>
130
+ </div>
131
+ </div>
132
+ </div>
133
+
134
+
135
+ </div>
136
+
137
+
138
+ </div>
139
+
140
+
141
+ <div id="validator-badges">
142
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
143
+ </div>
144
+
145
+ </body>
146
+ </html>