anchormodel 0.1.4 → 0.2.0
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 +1 -1
- data/README.md +114 -3
- data/VERSION +1 -1
- data/anchormodel.gemspec +5 -5
- data/doc/Anchormodel/ActiveModelTypeValueSingle.html +697 -0
- data/doc/Anchormodel/Attribute.html +109 -9
- data/doc/Anchormodel/ModelMixin.html +75 -3
- data/doc/Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html +269 -0
- data/doc/Anchormodel/SimpleFormInputs/Helpers.html +124 -0
- data/doc/Anchormodel/SimpleFormInputs.html +124 -0
- data/doc/Anchormodel/Util.html +612 -0
- data/doc/Anchormodel/Version.html +3 -3
- data/doc/Anchormodel.html +99 -34
- data/doc/AnchormodelGenerator.html +201 -0
- data/doc/AnchormodelInput.html +140 -0
- data/doc/AnchormodelRadioButtonsInput.html +140 -0
- data/doc/_index.html +63 -4
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +109 -6
- data/doc/frames.html +5 -10
- data/doc/index.html +109 -6
- data/doc/method_list.html +70 -6
- data/doc/top-level-namespace.html +4 -4
- data/lib/anchormodel/active_model_type_value_multi.rb +31 -0
- data/lib/anchormodel/active_model_type_value_single.rb +4 -2
- data/lib/anchormodel/attribute.rb +7 -1
- data/lib/anchormodel/model_mixin.rb +7 -0
- data/lib/anchormodel/simple_form_inputs/anchormodel_check_boxes_input.rb +23 -0
- 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 +54 -0
- data/lib/anchormodel/util.rb +57 -7
- data/lib/anchormodel.rb +10 -0
- data/test/active_record_model/user_test.rb +73 -9
- data/test/dummy/app/anchormodels/animal.rb +6 -0
- data/test/dummy/app/models/user.rb +1 -0
- data/test/dummy/db/migrate/20240425182000_add_animals_to_users.rb +5 -0
- data/test/dummy/db/schema.rb +2 -1
- metadata +18 -4
- data/pkg/anchormodel-0.1.3.gem +0 -0
@@ -0,0 +1,140 @@
|
|
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: AnchormodelRadioButtonsInput
|
8
|
+
|
9
|
+
— Documentation by YARD 0.9.34
|
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 = "AnchormodelRadioButtonsInput";
|
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">AnchormodelRadioButtonsInput</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: AnchormodelRadioButtonsInput
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
</h1>
|
67
|
+
<div class="box_info">
|
68
|
+
|
69
|
+
<dl>
|
70
|
+
<dt>Inherits:</dt>
|
71
|
+
<dd>
|
72
|
+
<span class="inheritName">SimpleForm::Inputs::CollectionRadioButtonsInput</span>
|
73
|
+
|
74
|
+
<ul class="fullTree">
|
75
|
+
<li>Object</li>
|
76
|
+
|
77
|
+
<li class="next">SimpleForm::Inputs::CollectionRadioButtonsInput</li>
|
78
|
+
|
79
|
+
<li class="next">AnchormodelRadioButtonsInput</li>
|
80
|
+
|
81
|
+
</ul>
|
82
|
+
<a href="#" class="inheritanceTree">show all</a>
|
83
|
+
|
84
|
+
</dd>
|
85
|
+
</dl>
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
<dl>
|
93
|
+
<dt>Includes:</dt>
|
94
|
+
<dd><span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html" title="Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon (module)">Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon</a></span></dd>
|
95
|
+
</dl>
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
<dl>
|
103
|
+
<dt>Defined in:</dt>
|
104
|
+
<dd>lib/anchormodel/simple_form_inputs/anchormodel_radio_buttons_input.rb</dd>
|
105
|
+
</dl>
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
<h2>Method Summary</h2>
|
124
|
+
|
125
|
+
<h3 class="inherited">Methods included from <span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html" title="Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon (module)">Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon</a></span></h3>
|
126
|
+
<p class="inherited"><span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html#input-instance_method" title="Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon#input (method)">#input</a></span></p>
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
</div>
|
131
|
+
|
132
|
+
<div id="footer">
|
133
|
+
Generated on Sat Apr 27 10:18:33 2024 by
|
134
|
+
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
135
|
+
0.9.34 (ruby-3.2.2).
|
136
|
+
</div>
|
137
|
+
|
138
|
+
</div>
|
139
|
+
</body>
|
140
|
+
</html>
|
data/doc/_index.html
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
<meta charset="utf-8">
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
6
|
<title>
|
7
|
-
Documentation by YARD 0.9.
|
7
|
+
Documentation by YARD 0.9.34
|
8
8
|
|
9
9
|
</title>
|
10
10
|
|
@@ -52,7 +52,7 @@
|
|
52
52
|
<div class="clear"></div>
|
53
53
|
</div>
|
54
54
|
|
55
|
-
<div id="content"><h1 class="noborder title">Documentation by YARD 0.9.
|
55
|
+
<div id="content"><h1 class="noborder title">Documentation by YARD 0.9.34</h1>
|
56
56
|
<div id="listing">
|
57
57
|
<h1 class="alphaindex">Alphabetic Index</h1>
|
58
58
|
|
@@ -80,6 +80,13 @@
|
|
80
80
|
<li class="letter">A</li>
|
81
81
|
<ul>
|
82
82
|
|
83
|
+
<li>
|
84
|
+
<span class='object_link'><a href="Anchormodel/ActiveModelTypeValueMulti.html" title="Anchormodel::ActiveModelTypeValueMulti (class)">ActiveModelTypeValueMulti</a></span>
|
85
|
+
|
86
|
+
<small>(Anchormodel)</small>
|
87
|
+
|
88
|
+
</li>
|
89
|
+
|
83
90
|
<li>
|
84
91
|
<span class='object_link'><a href="Anchormodel/ActiveModelTypeValueSingle.html" title="Anchormodel::ActiveModelTypeValueSingle (class)">ActiveModelTypeValueSingle</a></span>
|
85
92
|
|
@@ -92,11 +99,33 @@
|
|
92
99
|
|
93
100
|
</li>
|
94
101
|
|
102
|
+
<li>
|
103
|
+
<span class='object_link'><a href="AnchormodelCheckBoxesInput.html" title="AnchormodelCheckBoxesInput (class)">AnchormodelCheckBoxesInput</a></span>
|
104
|
+
|
105
|
+
</li>
|
106
|
+
|
95
107
|
<li>
|
96
108
|
<span class='object_link'><a href="AnchormodelGenerator.html" title="AnchormodelGenerator (class)">AnchormodelGenerator</a></span>
|
97
109
|
|
98
110
|
</li>
|
99
111
|
|
112
|
+
<li>
|
113
|
+
<span class='object_link'><a href="AnchormodelInput.html" title="AnchormodelInput (class)">AnchormodelInput</a></span>
|
114
|
+
|
115
|
+
</li>
|
116
|
+
|
117
|
+
<li>
|
118
|
+
<span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html" title="Anchormodel::SimpleFormInputs::Helpers::AnchormodelInputsCommon (module)">AnchormodelInputsCommon</a></span>
|
119
|
+
|
120
|
+
<small>(Anchormodel::SimpleFormInputs::Helpers)</small>
|
121
|
+
|
122
|
+
</li>
|
123
|
+
|
124
|
+
<li>
|
125
|
+
<span class='object_link'><a href="AnchormodelRadioButtonsInput.html" title="AnchormodelRadioButtonsInput (class)">AnchormodelRadioButtonsInput</a></span>
|
126
|
+
|
127
|
+
</li>
|
128
|
+
|
100
129
|
<li>
|
101
130
|
<span class='object_link'><a href="Anchormodel/Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span>
|
102
131
|
|
@@ -108,6 +137,21 @@
|
|
108
137
|
</ul>
|
109
138
|
|
110
139
|
|
140
|
+
<ul id="alpha_H" class="alpha">
|
141
|
+
<li class="letter">H</li>
|
142
|
+
<ul>
|
143
|
+
|
144
|
+
<li>
|
145
|
+
<span class='object_link'><a href="Anchormodel/SimpleFormInputs/Helpers.html" title="Anchormodel::SimpleFormInputs::Helpers (module)">Helpers</a></span>
|
146
|
+
|
147
|
+
<small>(Anchormodel::SimpleFormInputs)</small>
|
148
|
+
|
149
|
+
</li>
|
150
|
+
|
151
|
+
</ul>
|
152
|
+
</ul>
|
153
|
+
|
154
|
+
|
111
155
|
<ul id="alpha_M" class="alpha">
|
112
156
|
<li class="letter">M</li>
|
113
157
|
<ul>
|
@@ -123,6 +167,21 @@
|
|
123
167
|
</ul>
|
124
168
|
|
125
169
|
|
170
|
+
<ul id="alpha_S" class="alpha">
|
171
|
+
<li class="letter">S</li>
|
172
|
+
<ul>
|
173
|
+
|
174
|
+
<li>
|
175
|
+
<span class='object_link'><a href="Anchormodel/SimpleFormInputs.html" title="Anchormodel::SimpleFormInputs (module)">SimpleFormInputs</a></span>
|
176
|
+
|
177
|
+
<small>(Anchormodel)</small>
|
178
|
+
|
179
|
+
</li>
|
180
|
+
|
181
|
+
</ul>
|
182
|
+
</ul>
|
183
|
+
|
184
|
+
|
126
185
|
<ul id="alpha_U" class="alpha">
|
127
186
|
<li class="letter">U</li>
|
128
187
|
<ul>
|
@@ -161,9 +220,9 @@
|
|
161
220
|
</div>
|
162
221
|
|
163
222
|
<div id="footer">
|
164
|
-
Generated on
|
223
|
+
Generated on Sat Apr 27 10:18:32 2024 by
|
165
224
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
166
|
-
0.9.
|
225
|
+
0.9.34 (ruby-3.2.2).
|
167
226
|
</div>
|
168
227
|
|
169
228
|
</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::ActiveModelTypeValueMulti' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Anchormodel/ActiveModelTypeValueMulti.html" title="Anchormodel::ActiveModelTypeValueMulti (class)">ActiveModelTypeValueMulti</a></span> < ActiveModelTypeValueSingle<small class='search_info'>Anchormodel</small></div></li><li id='object_Anchormodel::ActiveModelTypeValueSingle' class='collapsed even'><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 odd'><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 even'><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 odd'><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 even'><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 odd'><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_AnchormodelCheckBoxesInput' class='even'><div class='item' style='padding-left:30px'><span class='object_link'><a href="AnchormodelCheckBoxesInput.html" title="AnchormodelCheckBoxesInput (class)">AnchormodelCheckBoxesInput</a></span> < CollectionCheckBoxesInput<small class='search_info'>Top Level Namespace</small></div></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
@@ -6,7 +6,7 @@
|
|
6
6
|
<title>
|
7
7
|
File: README
|
8
8
|
|
9
|
-
— Documentation by YARD 0.9.
|
9
|
+
— Documentation by YARD 0.9.34
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
@@ -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
|
|
@@ -159,6 +159,37 @@
|
|
159
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)
|
160
160
|
</span></code></pre>
|
161
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
|
+
|
162
193
|
<h1 id="label-Rails+Enum+style+model+methods">Rails Enum style model methods</h1>
|
163
194
|
|
164
195
|
<p>By default, Anchormodel adds three kinds of methods for each key to the model:</p>
|
@@ -230,15 +261,87 @@
|
|
230
261
|
<p>If you want to have multiple attributes in the same model pointing to the same Anchormodel, you need to disable <code>model_methods</code> for at least one of them (otherwise the model methods will clash in your model class):</p>
|
231
262
|
|
232
263
|
<pre class="code ruby"><code class="ruby"><span class='comment'># app/models/user.rb
|
233
|
-
</span
|
234
|
-
|
264
|
+
</span><span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span>
|
265
|
+
<span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:secondary_role</span><span class='comma'>,</span> <span class='const'>Role</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>false</span>
|
266
|
+
</code></pre>
|
267
|
+
|
268
|
+
<h1 id="label-Attaching+multiple+Anchormodels+to+an+attribute+-28similar+to+a+has_many+collection-29">Attaching multiple Anchormodels to an attribute (similar to a has_many collection)</h1>
|
269
|
+
|
270
|
+
<p>Collections of Anchormodels are supported. Assuming that your <code>User</code> can have multiple <code>Role</code> anchormodels, your code can look as follows:</p>
|
271
|
+
|
272
|
+
<pre class="code ruby"><code class="ruby"><span class='comment'># app/models/user.rb
|
273
|
+
</span><span class='id identifier rubyid_belongs_to_anchormodels'>belongs_to_anchormodels</span> <span class='symbol'>:roles</span>
|
274
|
+
</code></pre>
|
275
|
+
|
276
|
+
<p>The method is deliberately called <code>belongs_to...</code> and not <code>has_many...</code> in order to indicate that the key is stored in a column of the model in which you are calling it. The rule of thumb for using a collection of Anchormodels is:</p>
|
277
|
+
<ul><li>
|
278
|
+
<p>Your column should be of type <code>string</code>, just like with the singular <code>belongs_to_anchormodel</code>.</p>
|
279
|
+
</li><li>
|
280
|
+
<p>Anchormodels will be stored in string form, separated by the <code>,</code> character</p>
|
281
|
+
</li><li>
|
282
|
+
<p>When reading the attribute, you will get a <code>Set</code>, thus duplicates are avoided.</p>
|
283
|
+
</li></ul>
|
284
|
+
|
285
|
+
<p>Example usage for a User model with multiple roles as shown above:</p>
|
286
|
+
|
287
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_u'>u</span> <span class='op'>=</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span>
|
288
|
+
<span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span> <span class='op'>=</span> <span class='qsymbols_beg'>%i[</span><span class='tstring_content'>moderator</span><span class='words_sep'> </span><span class='tstring_content'>admin</span><span class='tstring_end'>]</span></span> <span class='comment'># this will replace the user's roles by the two specified
|
289
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span> <span class='comment'># this will return a set of two Role Anchormodel instances, moderator and role
|
290
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_guest!'>guest!</span> <span class='comment'># this will add the role `guest` to the user's roles
|
291
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_guest?'>guest?</span> <span class='comment'># this will query whether the role `guest` is part of the user's roles
|
292
|
+
</span><span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_moderator'>moderator</span> <span class='comment'># This will return all users that have the moderator role as part of their roles
|
293
|
+
</span></code></pre>
|
294
|
+
|
295
|
+
<p>For modifying a collection of Anchormodels, the following methods are implemented, the first three accepting a String, Symbol or Anchormodel:</p>
|
296
|
+
|
297
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span><span class='period'>.</span><span class='id identifier rubyid_add'>add</span><span class='lparen'>(</span><span class='symbol'>:moderator</span><span class='rparen'>)</span> <span class='comment'># same as u.moderator!
|
298
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span> <span class='op'><<</span> <span class='symbol'>:moderator</span> <span class='comment'># alias of add
|
299
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='symbol'>:moderator</span><span class='rparen'>)</span>
|
300
|
+
<span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span><span class='period'>.</span><span class='id identifier rubyid_clear'>clear</span>
|
301
|
+
</code></pre>
|
302
|
+
|
303
|
+
<p>Note that no other methods of Set are overwritten at this point - if you use any other methods mutating the underlying Set, your changes will not be applied.</p>
|
304
|
+
|
305
|
+
<h2 id="label-Basic+rails+form+for+a+collection+of+Anchormodels">Basic rails form for a collection of Anchormodels</h2>
|
306
|
+
|
307
|
+
<pre class="code ruby"><code class="ruby"><%= form_with(model: user) do |form| %>
|
308
|
+
<%# ... %>
|
309
|
+
<%= form.collection_select :role, Role.all, :key, :label, multiple: true %>
|
310
|
+
<%# ... %>
|
311
|
+
<% end %>
|
312
|
+
</code></pre>
|
313
|
+
|
314
|
+
<p>If you get an error due to unpermitted params, make sure, you are allowing array-style parameters: <code>params.require(:user).permit(roles: [])</code></p>
|
315
|
+
|
316
|
+
<h2 id="label-SimpleForm+for+a+collection+of+Anchormodels">SimpleForm for a collection of Anchormodels</h2>
|
317
|
+
|
318
|
+
<p>Anchormodel’s <a href="https://github.com/heartcombo/simple_form">simple_form</a> support also includes collections of Anchormodels.</p>
|
319
|
+
|
320
|
+
<p>Just like in the single Anchormodel implementation, a select input can be provided with:</p>
|
321
|
+
|
322
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
323
|
+
<%# ... %>
|
324
|
+
<%= f.input :role %>
|
325
|
+
<%# ... %>
|
326
|
+
<% end %>
|
327
|
+
</code></pre>
|
328
|
+
|
329
|
+
<p>The input figures out automatically that it is operating on a collection, so the form code is the same as for a single Anchormodel.</p>
|
330
|
+
|
331
|
+
<p>However, radio buttons are unsuitable for collections, so use check boxes instead:</p>
|
332
|
+
|
333
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
334
|
+
<%# ... %>
|
335
|
+
<%= f.input :role, as: :anchormodel_check_boxes %>
|
336
|
+
<%# ... %>
|
337
|
+
<% end %>
|
235
338
|
</code></pre>
|
236
339
|
</div></div>
|
237
340
|
|
238
341
|
<div id="footer">
|
239
|
-
Generated on
|
342
|
+
Generated on Sat Apr 27 10:18:33 2024 by
|
240
343
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
241
|
-
0.9.
|
344
|
+
0.9.34 (ruby-3.2.2).
|
242
345
|
</div>
|
243
346
|
|
244
347
|
</div>
|
data/doc/frames.html
CHANGED
@@ -2,18 +2,13 @@
|
|
2
2
|
<html>
|
3
3
|
<head>
|
4
4
|
<meta charset="utf-8">
|
5
|
-
<title>Documentation by YARD 0.9.
|
5
|
+
<title>Documentation by YARD 0.9.34</title>
|
6
6
|
</head>
|
7
7
|
<script type="text/javascript">
|
8
|
-
var
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
var url = new URL(name, location.href);
|
13
|
-
window.top.location.replace(url.origin === location.origin ? name : mainUrl);
|
14
|
-
} catch (e) {
|
15
|
-
window.top.location.replace(mainUrl);
|
16
|
-
}
|
8
|
+
var match = unescape(window.location.hash).match(/^#!(.+)/);
|
9
|
+
var name = match ? match[1] : 'index.html';
|
10
|
+
name = name.replace(/^(\w+):\/\//, '').replace(/^\/\//, '');
|
11
|
+
window.top.location = name;
|
17
12
|
</script>
|
18
13
|
<noscript>
|
19
14
|
<h1>Oops!</h1>
|
data/doc/index.html
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
<title>
|
7
7
|
File: README
|
8
8
|
|
9
|
-
— Documentation by YARD 0.9.
|
9
|
+
— Documentation by YARD 0.9.34
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
@@ -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
|
|
@@ -159,6 +159,37 @@
|
|
159
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)
|
160
160
|
</span></code></pre>
|
161
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
|
+
|
162
193
|
<h1 id="label-Rails+Enum+style+model+methods">Rails Enum style model methods</h1>
|
163
194
|
|
164
195
|
<p>By default, Anchormodel adds three kinds of methods for each key to the model:</p>
|
@@ -230,15 +261,87 @@
|
|
230
261
|
<p>If you want to have multiple attributes in the same model pointing to the same Anchormodel, you need to disable <code>model_methods</code> for at least one of them (otherwise the model methods will clash in your model class):</p>
|
231
262
|
|
232
263
|
<pre class="code ruby"><code class="ruby"><span class='comment'># app/models/user.rb
|
233
|
-
</span
|
234
|
-
|
264
|
+
</span><span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span>
|
265
|
+
<span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:secondary_role</span><span class='comma'>,</span> <span class='const'>Role</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>false</span>
|
266
|
+
</code></pre>
|
267
|
+
|
268
|
+
<h1 id="label-Attaching+multiple+Anchormodels+to+an+attribute+-28similar+to+a+has_many+collection-29">Attaching multiple Anchormodels to an attribute (similar to a has_many collection)</h1>
|
269
|
+
|
270
|
+
<p>Collections of Anchormodels are supported. Assuming that your <code>User</code> can have multiple <code>Role</code> anchormodels, your code can look as follows:</p>
|
271
|
+
|
272
|
+
<pre class="code ruby"><code class="ruby"><span class='comment'># app/models/user.rb
|
273
|
+
</span><span class='id identifier rubyid_belongs_to_anchormodels'>belongs_to_anchormodels</span> <span class='symbol'>:roles</span>
|
274
|
+
</code></pre>
|
275
|
+
|
276
|
+
<p>The method is deliberately called <code>belongs_to...</code> and not <code>has_many...</code> in order to indicate that the key is stored in a column of the model in which you are calling it. The rule of thumb for using a collection of Anchormodels is:</p>
|
277
|
+
<ul><li>
|
278
|
+
<p>Your column should be of type <code>string</code>, just like with the singular <code>belongs_to_anchormodel</code>.</p>
|
279
|
+
</li><li>
|
280
|
+
<p>Anchormodels will be stored in string form, separated by the <code>,</code> character</p>
|
281
|
+
</li><li>
|
282
|
+
<p>When reading the attribute, you will get a <code>Set</code>, thus duplicates are avoided.</p>
|
283
|
+
</li></ul>
|
284
|
+
|
285
|
+
<p>Example usage for a User model with multiple roles as shown above:</p>
|
286
|
+
|
287
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_u'>u</span> <span class='op'>=</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span>
|
288
|
+
<span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span> <span class='op'>=</span> <span class='qsymbols_beg'>%i[</span><span class='tstring_content'>moderator</span><span class='words_sep'> </span><span class='tstring_content'>admin</span><span class='tstring_end'>]</span></span> <span class='comment'># this will replace the user's roles by the two specified
|
289
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span> <span class='comment'># this will return a set of two Role Anchormodel instances, moderator and role
|
290
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_guest!'>guest!</span> <span class='comment'># this will add the role `guest` to the user's roles
|
291
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_guest?'>guest?</span> <span class='comment'># this will query whether the role `guest` is part of the user's roles
|
292
|
+
</span><span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_moderator'>moderator</span> <span class='comment'># This will return all users that have the moderator role as part of their roles
|
293
|
+
</span></code></pre>
|
294
|
+
|
295
|
+
<p>For modifying a collection of Anchormodels, the following methods are implemented, the first three accepting a String, Symbol or Anchormodel:</p>
|
296
|
+
|
297
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span><span class='period'>.</span><span class='id identifier rubyid_add'>add</span><span class='lparen'>(</span><span class='symbol'>:moderator</span><span class='rparen'>)</span> <span class='comment'># same as u.moderator!
|
298
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span> <span class='op'><<</span> <span class='symbol'>:moderator</span> <span class='comment'># alias of add
|
299
|
+
</span><span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='symbol'>:moderator</span><span class='rparen'>)</span>
|
300
|
+
<span class='id identifier rubyid_u'>u</span><span class='period'>.</span><span class='id identifier rubyid_roles'>roles</span><span class='period'>.</span><span class='id identifier rubyid_clear'>clear</span>
|
301
|
+
</code></pre>
|
302
|
+
|
303
|
+
<p>Note that no other methods of Set are overwritten at this point - if you use any other methods mutating the underlying Set, your changes will not be applied.</p>
|
304
|
+
|
305
|
+
<h2 id="label-Basic+rails+form+for+a+collection+of+Anchormodels">Basic rails form for a collection of Anchormodels</h2>
|
306
|
+
|
307
|
+
<pre class="code ruby"><code class="ruby"><%= form_with(model: user) do |form| %>
|
308
|
+
<%# ... %>
|
309
|
+
<%= form.collection_select :role, Role.all, :key, :label, multiple: true %>
|
310
|
+
<%# ... %>
|
311
|
+
<% end %>
|
312
|
+
</code></pre>
|
313
|
+
|
314
|
+
<p>If you get an error due to unpermitted params, make sure, you are allowing array-style parameters: <code>params.require(:user).permit(roles: [])</code></p>
|
315
|
+
|
316
|
+
<h2 id="label-SimpleForm+for+a+collection+of+Anchormodels">SimpleForm for a collection of Anchormodels</h2>
|
317
|
+
|
318
|
+
<p>Anchormodel’s <a href="https://github.com/heartcombo/simple_form">simple_form</a> support also includes collections of Anchormodels.</p>
|
319
|
+
|
320
|
+
<p>Just like in the single Anchormodel implementation, a select input can be provided with:</p>
|
321
|
+
|
322
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
323
|
+
<%# ... %>
|
324
|
+
<%= f.input :role %>
|
325
|
+
<%# ... %>
|
326
|
+
<% end %>
|
327
|
+
</code></pre>
|
328
|
+
|
329
|
+
<p>The input figures out automatically that it is operating on a collection, so the form code is the same as for a single Anchormodel.</p>
|
330
|
+
|
331
|
+
<p>However, radio buttons are unsuitable for collections, so use check boxes instead:</p>
|
332
|
+
|
333
|
+
<pre class="code ruby"><code class="ruby"><%= simple_form_for user do |f| %>
|
334
|
+
<%# ... %>
|
335
|
+
<%= f.input :role, as: :anchormodel_check_boxes %>
|
336
|
+
<%# ... %>
|
337
|
+
<% end %>
|
235
338
|
</code></pre>
|
236
339
|
</div></div>
|
237
340
|
|
238
341
|
<div id="footer">
|
239
|
-
Generated on
|
342
|
+
Generated on Sat Apr 27 10:18:33 2024 by
|
240
343
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
241
|
-
0.9.
|
344
|
+
0.9.34 (ruby-3.2.2).
|
242
345
|
</div>
|
243
346
|
|
244
347
|
</div>
|