anchormodel 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +26 -24
- data/README.md +36 -1
- data/VERSION +1 -0
- data/anchormodel.gemspec +15 -15
- data/doc/Anchormodel/ActiveModelTypeValueSingle.html +615 -0
- data/doc/Anchormodel/Attribute.html +2 -2
- data/doc/Anchormodel/ModelMixin.html +18 -283
- data/doc/Anchormodel/Util.html +496 -0
- data/doc/Anchormodel/Version.html +3 -59
- data/doc/Anchormodel.html +5 -5
- data/doc/AnchormodelGenerator.html +201 -0
- data/doc/_index.html +70 -3
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +45 -6
- data/doc/index.html +45 -6
- data/doc/method_list.html +50 -18
- data/doc/top-level-namespace.html +3 -3
- data/lib/anchormodel/{active_model_type_value.rb → active_model_type_value_single.rb} +22 -9
- data/lib/anchormodel/model_mixin.rb +4 -82
- data/lib/anchormodel/simple_form_inputs/anchormodel_input.rb +21 -0
- data/lib/anchormodel/simple_form_inputs/anchormodel_radio_buttons_input.rb +23 -0
- data/lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb +49 -0
- data/lib/anchormodel/util.rb +93 -0
- data/lib/anchormodel/version.rb +1 -7
- data/lib/anchormodel.rb +5 -1
- data/test/active_record_model/user_test.rb +19 -0
- metadata +17 -9
@@ -0,0 +1,201 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>
|
7
|
+
Class: AnchormodelGenerator
|
8
|
+
|
9
|
+
— Documentation by YARD 0.9.28
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
16
|
+
|
17
|
+
<script type="text/javascript">
|
18
|
+
pathId = "AnchormodelGenerator";
|
19
|
+
relpath = '';
|
20
|
+
</script>
|
21
|
+
|
22
|
+
|
23
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
24
|
+
|
25
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
26
|
+
|
27
|
+
|
28
|
+
</head>
|
29
|
+
<body>
|
30
|
+
<div class="nav_wrap">
|
31
|
+
<iframe id="nav" src="class_list.html?1"></iframe>
|
32
|
+
<div id="resizer"></div>
|
33
|
+
</div>
|
34
|
+
|
35
|
+
<div id="main" tabindex="-1">
|
36
|
+
<div id="header">
|
37
|
+
<div id="menu">
|
38
|
+
|
39
|
+
<a href="_index.html">Index (A)</a> »
|
40
|
+
|
41
|
+
|
42
|
+
<span class="title">AnchormodelGenerator</span>
|
43
|
+
|
44
|
+
</div>
|
45
|
+
|
46
|
+
<div id="search">
|
47
|
+
|
48
|
+
<a class="full_list_link" id="class_list_link"
|
49
|
+
href="class_list.html">
|
50
|
+
|
51
|
+
<svg width="24" height="24">
|
52
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
54
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
55
|
+
</svg>
|
56
|
+
</a>
|
57
|
+
|
58
|
+
</div>
|
59
|
+
<div class="clear"></div>
|
60
|
+
</div>
|
61
|
+
|
62
|
+
<div id="content"><h1>Class: AnchormodelGenerator
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
</h1>
|
67
|
+
<div class="box_info">
|
68
|
+
|
69
|
+
<dl>
|
70
|
+
<dt>Inherits:</dt>
|
71
|
+
<dd>
|
72
|
+
<span class="inheritName">Rails::Generators::NamedBase</span>
|
73
|
+
|
74
|
+
<ul class="fullTree">
|
75
|
+
<li>Object</li>
|
76
|
+
|
77
|
+
<li class="next">Rails::Generators::NamedBase</li>
|
78
|
+
|
79
|
+
<li class="next">AnchormodelGenerator</li>
|
80
|
+
|
81
|
+
</ul>
|
82
|
+
<a href="#" class="inheritanceTree">show all</a>
|
83
|
+
|
84
|
+
</dd>
|
85
|
+
</dl>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
<dl>
|
98
|
+
<dt>Defined in:</dt>
|
99
|
+
<dd>lib/generators/anchormodel/anchormodel_generator.rb</dd>
|
100
|
+
</dl>
|
101
|
+
|
102
|
+
</div>
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
<h2>
|
113
|
+
Instance Method Summary
|
114
|
+
<small><a href="#" class="summary_toggle">collapse</a></small>
|
115
|
+
</h2>
|
116
|
+
|
117
|
+
<ul class="summary">
|
118
|
+
|
119
|
+
<li class="public ">
|
120
|
+
<span class="summary_signature">
|
121
|
+
|
122
|
+
<a href="#add_anchormodel-instance_method" title="#add_anchormodel (instance method)">#<strong>add_anchormodel</strong> ⇒ Object </a>
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
</span>
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
<span class="summary_desc"><div class='inline'></div></span>
|
137
|
+
|
138
|
+
</li>
|
139
|
+
|
140
|
+
|
141
|
+
</ul>
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
<div id="instance_method_details" class="method_details_list">
|
148
|
+
<h2>Instance Method Details</h2>
|
149
|
+
|
150
|
+
|
151
|
+
<div class="method_details first">
|
152
|
+
<h3 class="signature first" id="add_anchormodel-instance_method">
|
153
|
+
|
154
|
+
#<strong>add_anchormodel</strong> ⇒ <tt>Object</tt>
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
</h3><table class="source_code">
|
161
|
+
<tr>
|
162
|
+
<td>
|
163
|
+
<pre class="lines">
|
164
|
+
|
165
|
+
|
166
|
+
4
|
167
|
+
5
|
168
|
+
6
|
169
|
+
7
|
170
|
+
8
|
171
|
+
9
|
172
|
+
10</pre>
|
173
|
+
</td>
|
174
|
+
<td>
|
175
|
+
<pre class="code"><span class="info file"># File 'lib/generators/anchormodel/anchormodel_generator.rb', line 4</span>
|
176
|
+
|
177
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_add_anchormodel'>add_anchormodel</span>
|
178
|
+
<span class='id identifier rubyid_fail'>fail</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>NAME must be present.</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
|
179
|
+
<span class='ivar'>@klass</span> <span class='op'>=</span> <span class='ivar'>@name</span><span class='period'>.</span><span class='id identifier rubyid_camelize'>camelize</span>
|
180
|
+
<span class='ivar'>@filename</span> <span class='op'>=</span> <span class='ivar'>@name</span><span class='period'>.</span><span class='id identifier rubyid_underscore'>underscore</span>
|
181
|
+
|
182
|
+
<span class='id identifier rubyid_template'>template</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>anchormodel.rb.erb</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>app/anchormodels/</span><span class='embexpr_beg'>#{</span><span class='ivar'>@filename</span><span class='embexpr_end'>}</span><span class='tstring_content'>.rb</span><span class='tstring_end'>"</span></span>
|
183
|
+
<span class='kw'>end</span></pre>
|
184
|
+
</td>
|
185
|
+
</tr>
|
186
|
+
</table>
|
187
|
+
</div>
|
188
|
+
|
189
|
+
</div>
|
190
|
+
|
191
|
+
</div>
|
192
|
+
|
193
|
+
<div id="footer">
|
194
|
+
Generated on Wed Apr 24 17:01:46 2024 by
|
195
|
+
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
196
|
+
0.9.28 (ruby-3.2.2).
|
197
|
+
</div>
|
198
|
+
|
199
|
+
</div>
|
200
|
+
</body>
|
201
|
+
</html>
|
data/doc/_index.html
CHANGED
@@ -81,7 +81,7 @@
|
|
81
81
|
<ul>
|
82
82
|
|
83
83
|
<li>
|
84
|
-
<span class='object_link'><a href="Anchormodel/
|
84
|
+
<span class='object_link'><a href="Anchormodel/ActiveModelTypeValueSingle.html" title="Anchormodel::ActiveModelTypeValueSingle (class)">ActiveModelTypeValueSingle</a></span>
|
85
85
|
|
86
86
|
<small>(Anchormodel)</small>
|
87
87
|
|
@@ -92,6 +92,28 @@
|
|
92
92
|
|
93
93
|
</li>
|
94
94
|
|
95
|
+
<li>
|
96
|
+
<span class='object_link'><a href="AnchormodelGenerator.html" title="AnchormodelGenerator (class)">AnchormodelGenerator</a></span>
|
97
|
+
|
98
|
+
</li>
|
99
|
+
|
100
|
+
<li>
|
101
|
+
<span class='object_link'><a href="AnchormodelInput.html" title="AnchormodelInput (class)">AnchormodelInput</a></span>
|
102
|
+
|
103
|
+
</li>
|
104
|
+
|
105
|
+
<li>
|
106
|
+
<span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html" title="Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon (module)">AnchormodelInputsCommon</a></span>
|
107
|
+
|
108
|
+
<small>(Anchormodel::SimpleFormInputs::Helpers)</small>
|
109
|
+
|
110
|
+
</li>
|
111
|
+
|
112
|
+
<li>
|
113
|
+
<span class='object_link'><a href="AnchormodelRadioButtonsInput.html" title="AnchormodelRadioButtonsInput (class)">AnchormodelRadioButtonsInput</a></span>
|
114
|
+
|
115
|
+
</li>
|
116
|
+
|
95
117
|
<li>
|
96
118
|
<span class='object_link'><a href="Anchormodel/Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span>
|
97
119
|
|
@@ -103,6 +125,21 @@
|
|
103
125
|
</ul>
|
104
126
|
|
105
127
|
|
128
|
+
<ul id="alpha_H" class="alpha">
|
129
|
+
<li class="letter">H</li>
|
130
|
+
<ul>
|
131
|
+
|
132
|
+
<li>
|
133
|
+
<span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers.html" title="Anchormodel::SimpleFormInputs::Helpers (module)">Helpers</a></span>
|
134
|
+
|
135
|
+
<small>(Anchormodel::SimpleFormInputs)</small>
|
136
|
+
|
137
|
+
</li>
|
138
|
+
|
139
|
+
</ul>
|
140
|
+
</ul>
|
141
|
+
|
142
|
+
|
106
143
|
<ul id="alpha_M" class="alpha">
|
107
144
|
<li class="letter">M</li>
|
108
145
|
<ul>
|
@@ -118,6 +155,36 @@
|
|
118
155
|
</ul>
|
119
156
|
|
120
157
|
|
158
|
+
<ul id="alpha_S" class="alpha">
|
159
|
+
<li class="letter">S</li>
|
160
|
+
<ul>
|
161
|
+
|
162
|
+
<li>
|
163
|
+
<span class='object_link'><a href="Anchormodel/SimpleFormInputs.html" title="Anchormodel::SimpleFormInputs (module)">SimpleFormInputs</a></span>
|
164
|
+
|
165
|
+
<small>(Anchormodel)</small>
|
166
|
+
|
167
|
+
</li>
|
168
|
+
|
169
|
+
</ul>
|
170
|
+
</ul>
|
171
|
+
|
172
|
+
|
173
|
+
<ul id="alpha_U" class="alpha">
|
174
|
+
<li class="letter">U</li>
|
175
|
+
<ul>
|
176
|
+
|
177
|
+
<li>
|
178
|
+
<span class='object_link'><a href="Anchormodel/Util.html" title="Anchormodel::Util (module)">Util</a></span>
|
179
|
+
|
180
|
+
<small>(Anchormodel)</small>
|
181
|
+
|
182
|
+
</li>
|
183
|
+
|
184
|
+
</ul>
|
185
|
+
</ul>
|
186
|
+
|
187
|
+
|
121
188
|
<ul id="alpha_V" class="alpha">
|
122
189
|
<li class="letter">V</li>
|
123
190
|
<ul>
|
@@ -141,9 +208,9 @@
|
|
141
208
|
</div>
|
142
209
|
|
143
210
|
<div id="footer">
|
144
|
-
Generated on Wed
|
211
|
+
Generated on Wed Apr 24 17:01:45 2024 by
|
145
212
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
146
|
-
0.9.28 (ruby-3.
|
213
|
+
0.9.28 (ruby-3.2.2).
|
147
214
|
</div>
|
148
215
|
|
149
216
|
</div>
|
data/doc/class_list.html
CHANGED
@@ -43,7 +43,7 @@
|
|
43
43
|
|
44
44
|
<ul id="full_list" class="class">
|
45
45
|
<li id="object_" class="odd"><div class="item" style="padding-left:30px"><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></div></li>
|
46
|
-
<li id='object_Anchormodel' class='even'><div class='item' style='padding-left:30px'><a class='toggle'></a> <span class='object_link'><a href="Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span> < Object<small class='search_info'>Top Level Namespace</small></div><ul><li id='object_Anchormodel::
|
46
|
+
<li id='object_Anchormodel' class='even'><div class='item' style='padding-left:30px'><a class='toggle'></a> <span class='object_link'><a href="Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span> < Object<small class='search_info'>Top Level Namespace</small></div><ul><li id='object_Anchormodel::ActiveModelTypeValueSingle' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Anchormodel/ActiveModelTypeValueSingle.html" title="Anchormodel::ActiveModelTypeValueSingle (class)">ActiveModelTypeValueSingle</a></span> < Value<small class='search_info'>Anchormodel</small></div></li><li id='object_Anchormodel::Attribute' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Anchormodel/Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span> < Object<small class='search_info'>Anchormodel</small></div></li><li id='object_Anchormodel::ModelMixin' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Anchormodel/ModelMixin.html" title="Anchormodel::ModelMixin (module)">ModelMixin</a></span><small class='search_info'>Anchormodel</small></div></li><li id='object_Anchormodel::SimpleFormInputs' class='collapsed even'><div class='item' style='padding-left:45px'><a class='toggle'></a> <span class='object_link'><a href="Anchormodel/SimpleFormInputs.html" title="Anchormodel::SimpleFormInputs (module)">SimpleFormInputs</a></span><small class='search_info'>Anchormodel</small></div><ul><li id='object_Anchormodel::SimpleFormInputs::Helpers' class='collapsed'><div class='item' style='padding-left:60px'><a class='toggle'></a> <span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers.html" title="Anchormodel::SimpleFormInputs::Helpers (module)">Helpers</a></span><small class='search_info'>Anchormodel::SimpleFormInputs</small></div><ul><li id='object_Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon' class='collapsed'><div class='item' style='padding-left:75px'><span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html" title="Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon (module)">AnchormodelInputsCommon</a></span><small class='search_info'>Anchormodel::SimpleFormInputs::Helpers</small></div></li></ul></li></ul></li><li id='object_Anchormodel::Util' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Anchormodel/Util.html" title="Anchormodel::Util (module)">Util</a></span><small class='search_info'>Anchormodel</small></div></li><li id='object_Anchormodel::Version' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Anchormodel/Version.html" title="Anchormodel::Version (module)">Version</a></span><small class='search_info'>Anchormodel</small></div></li></ul></li><li id='object_AnchormodelGenerator' class='odd'><div class='item' style='padding-left:30px'><span class='object_link'><a href="AnchormodelGenerator.html" title="AnchormodelGenerator (class)">AnchormodelGenerator</a></span> < NamedBase<small class='search_info'>Top Level Namespace</small></div></li><li id='object_AnchormodelInput' class='even'><div class='item' style='padding-left:30px'><span class='object_link'><a href="AnchormodelInput.html" title="AnchormodelInput (class)">AnchormodelInput</a></span> < CollectionSelectInput<small class='search_info'>Top Level Namespace</small></div></li><li id='object_AnchormodelRadioButtonsInput' class='odd'><div class='item' style='padding-left:30px'><span class='object_link'><a href="AnchormodelRadioButtonsInput.html" title="AnchormodelRadioButtonsInput (class)">AnchormodelRadioButtonsInput</a></span> < CollectionRadioButtonsInput<small class='search_info'>Top Level Namespace</small></div></li>
|
47
47
|
|
48
48
|
</ul>
|
49
49
|
</div>
|
data/doc/file.README.html
CHANGED
@@ -68,11 +68,11 @@
|
|
68
68
|
|
69
69
|
<p>Typically, a Rails application consists of three kinds of state:</p>
|
70
70
|
<ul><li>
|
71
|
-
<p>The code, which we can consider static within a given version. Code can
|
71
|
+
<p>The code, which we can consider static within a given version. Code can reference to other code, e.g. <code>node.parent = other_node</code>.</p>
|
72
72
|
</li><li>
|
73
|
-
<p>The database contents, which can fluctuate within the bounds of the DB schema.
|
73
|
+
<p>The database contents, which can fluctuate within the bounds of the DB schema. Data can reference to other data, ideally via foreign keys.</p>
|
74
74
|
</li><li>
|
75
|
-
<p>A mix of the two, where code needs to be specifically tailored for some kind
|
75
|
+
<p>A mix of the two, where code needs to be specifically tailored for some kind of data. A prominent example of such a mix would for instance be user roles: roles must be hardcoded in the code because security logic is tied to them. However, as users are assigned to roles in the database, roles also need to be persisted in the database. This is where Anchormodel comes into play.</p>
|
76
76
|
</li></ul>
|
77
77
|
|
78
78
|
<h2 id="label-Alternatives+coviering+the+same+use+case">Alternatives coviering the same use case</h2>
|
@@ -85,7 +85,7 @@
|
|
85
85
|
|
86
86
|
<h3 id="label-ActiveEnum">ActiveEnum</h3>
|
87
87
|
|
88
|
-
<p>The gem
|
88
|
+
<p>The gem <a href="https://github.com/adzap/active_enum">ActiveEnum</a> allows to create Enum-like classes that can be extended. However it only supports Integer keys. I find this unsatisfactory, as debugging with tools like <code>psql</code> or <code>mysql</code> is made unnecessarily hard when you only see numbers. Keys for enums should be meaningful, making you immediately understand what they stand for.</p>
|
89
89
|
|
90
90
|
<p>This is why Anchormodel is strictly relying on String keys corresponding to the entries of an Anchormodel.</p>
|
91
91
|
|
@@ -96,6 +96,14 @@
|
|
96
96
|
<p>In <code>application_record.rb</code>, add in the class body: <code>include Anchormodel::ModelMixin</code></p>
|
97
97
|
</li></ol>
|
98
98
|
|
99
|
+
<h1 id="label-Generator">Generator</h1>
|
100
|
+
|
101
|
+
<p>For convenience, Anchormodel provides a Rails generator:</p>
|
102
|
+
|
103
|
+
<p><code>rails generate anchormodel Role</code></p>
|
104
|
+
|
105
|
+
<p>This will create <code>app/anchormodels/role.rb</code>.</p>
|
106
|
+
|
99
107
|
<h1 id="label-Basic+example">Basic example</h1>
|
100
108
|
|
101
109
|
<p><code>app/anchormodels/role.rb</code>:</p>
|
@@ -151,6 +159,37 @@
|
|
151
159
|
</span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_role'>role</span><span class='period'>.</span><span class='id identifier rubyid_admin?'>admin?</span> <span class='comment'># true if and only if the role is admin (false otherwise)
|
152
160
|
</span></code></pre>
|
153
161
|
|
162
|
+
<p>Your form could look something like this:</p>
|
163
|
+
|
164
|
+
<pre class="code ruby"><code class="ruby"><%= form_with(model: user) do |form| %>
|
165
|
+
<%# ... %>
|
166
|
+
<%= form.collection_select :role, Role.all, :key, :label %>
|
167
|
+
<%# ... %>
|
168
|
+
<% end %>
|
169
|
+
</code></pre>
|
170
|
+
|
171
|
+
<h2 id="label-Using+Anchormodel+with+Simpleform">Using Anchormodel with Simpleform</h2>
|
172
|
+
|
173
|
+
<p>Anchormodel has built-in support for the <a href="https://github.com/heartcombo/simple_form">simple_form</a> gem by providing an input for the type <code>:anchormodel</code> which displays anchormodel attributes as a collection select.</p>
|
174
|
+
|
175
|
+
<p>After SimpleForm is installed, you can write your form as:</p>
|
176
|
+
|
177
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
178
|
+
<%# ... %>
|
179
|
+
<%= f.input :role %>
|
180
|
+
<%# ... %>
|
181
|
+
<% end %>
|
182
|
+
</code></pre>
|
183
|
+
|
184
|
+
<p>Or, if you prefer radio buttons instead:</p>
|
185
|
+
|
186
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
187
|
+
<%# ... %>
|
188
|
+
<%= f.input :role, as: :anchormodel_radio_buttons %>
|
189
|
+
<%# ... %>
|
190
|
+
<% end %>
|
191
|
+
</code></pre>
|
192
|
+
|
154
193
|
<h1 id="label-Rails+Enum+style+model+methods">Rails Enum style model methods</h1>
|
155
194
|
|
156
195
|
<p>By default, Anchormodel adds three kinds of methods for each key to the model:</p>
|
@@ -228,9 +267,9 @@
|
|
228
267
|
</div></div>
|
229
268
|
|
230
269
|
<div id="footer">
|
231
|
-
Generated on Wed
|
270
|
+
Generated on Wed Apr 24 17:01:46 2024 by
|
232
271
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
233
|
-
0.9.28 (ruby-3.
|
272
|
+
0.9.28 (ruby-3.2.2).
|
234
273
|
</div>
|
235
274
|
|
236
275
|
</div>
|
data/doc/index.html
CHANGED
@@ -68,11 +68,11 @@
|
|
68
68
|
|
69
69
|
<p>Typically, a Rails application consists of three kinds of state:</p>
|
70
70
|
<ul><li>
|
71
|
-
<p>The code, which we can consider static within a given version. Code can
|
71
|
+
<p>The code, which we can consider static within a given version. Code can reference to other code, e.g. <code>node.parent = other_node</code>.</p>
|
72
72
|
</li><li>
|
73
|
-
<p>The database contents, which can fluctuate within the bounds of the DB schema.
|
73
|
+
<p>The database contents, which can fluctuate within the bounds of the DB schema. Data can reference to other data, ideally via foreign keys.</p>
|
74
74
|
</li><li>
|
75
|
-
<p>A mix of the two, where code needs to be specifically tailored for some kind
|
75
|
+
<p>A mix of the two, where code needs to be specifically tailored for some kind of data. A prominent example of such a mix would for instance be user roles: roles must be hardcoded in the code because security logic is tied to them. However, as users are assigned to roles in the database, roles also need to be persisted in the database. This is where Anchormodel comes into play.</p>
|
76
76
|
</li></ul>
|
77
77
|
|
78
78
|
<h2 id="label-Alternatives+coviering+the+same+use+case">Alternatives coviering the same use case</h2>
|
@@ -85,7 +85,7 @@
|
|
85
85
|
|
86
86
|
<h3 id="label-ActiveEnum">ActiveEnum</h3>
|
87
87
|
|
88
|
-
<p>The gem
|
88
|
+
<p>The gem <a href="https://github.com/adzap/active_enum">ActiveEnum</a> allows to create Enum-like classes that can be extended. However it only supports Integer keys. I find this unsatisfactory, as debugging with tools like <code>psql</code> or <code>mysql</code> is made unnecessarily hard when you only see numbers. Keys for enums should be meaningful, making you immediately understand what they stand for.</p>
|
89
89
|
|
90
90
|
<p>This is why Anchormodel is strictly relying on String keys corresponding to the entries of an Anchormodel.</p>
|
91
91
|
|
@@ -96,6 +96,14 @@
|
|
96
96
|
<p>In <code>application_record.rb</code>, add in the class body: <code>include Anchormodel::ModelMixin</code></p>
|
97
97
|
</li></ol>
|
98
98
|
|
99
|
+
<h1 id="label-Generator">Generator</h1>
|
100
|
+
|
101
|
+
<p>For convenience, Anchormodel provides a Rails generator:</p>
|
102
|
+
|
103
|
+
<p><code>rails generate anchormodel Role</code></p>
|
104
|
+
|
105
|
+
<p>This will create <code>app/anchormodels/role.rb</code>.</p>
|
106
|
+
|
99
107
|
<h1 id="label-Basic+example">Basic example</h1>
|
100
108
|
|
101
109
|
<p><code>app/anchormodels/role.rb</code>:</p>
|
@@ -151,6 +159,37 @@
|
|
151
159
|
</span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_role'>role</span><span class='period'>.</span><span class='id identifier rubyid_admin?'>admin?</span> <span class='comment'># true if and only if the role is admin (false otherwise)
|
152
160
|
</span></code></pre>
|
153
161
|
|
162
|
+
<p>Your form could look something like this:</p>
|
163
|
+
|
164
|
+
<pre class="code ruby"><code class="ruby"><%= form_with(model: user) do |form| %>
|
165
|
+
<%# ... %>
|
166
|
+
<%= form.collection_select :role, Role.all, :key, :label %>
|
167
|
+
<%# ... %>
|
168
|
+
<% end %>
|
169
|
+
</code></pre>
|
170
|
+
|
171
|
+
<h2 id="label-Using+Anchormodel+with+Simpleform">Using Anchormodel with Simpleform</h2>
|
172
|
+
|
173
|
+
<p>Anchormodel has built-in support for the <a href="https://github.com/heartcombo/simple_form">simple_form</a> gem by providing an input for the type <code>:anchormodel</code> which displays anchormodel attributes as a collection select.</p>
|
174
|
+
|
175
|
+
<p>After SimpleForm is installed, you can write your form as:</p>
|
176
|
+
|
177
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
178
|
+
<%# ... %>
|
179
|
+
<%= f.input :role %>
|
180
|
+
<%# ... %>
|
181
|
+
<% end %>
|
182
|
+
</code></pre>
|
183
|
+
|
184
|
+
<p>Or, if you prefer radio buttons instead:</p>
|
185
|
+
|
186
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
187
|
+
<%# ... %>
|
188
|
+
<%= f.input :role, as: :anchormodel_radio_buttons %>
|
189
|
+
<%# ... %>
|
190
|
+
<% end %>
|
191
|
+
</code></pre>
|
192
|
+
|
154
193
|
<h1 id="label-Rails+Enum+style+model+methods">Rails Enum style model methods</h1>
|
155
194
|
|
156
195
|
<p>By default, Anchormodel adds three kinds of methods for each key to the model:</p>
|
@@ -228,9 +267,9 @@
|
|
228
267
|
</div></div>
|
229
268
|
|
230
269
|
<div id="footer">
|
231
|
-
Generated on Wed
|
270
|
+
Generated on Wed Apr 24 17:01:46 2024 by
|
232
271
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
233
|
-
0.9.28 (ruby-3.
|
272
|
+
0.9.28 (ruby-3.2.2).
|
234
273
|
</div>
|
235
274
|
|
236
275
|
</div>
|