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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f7343b6009976e35fa3354eeabe119ad354dd4f103af1d1440251cf178de989
|
4
|
+
data.tar.gz: b971498fc5b0028c2f43994130abf55b6edfb9be0a327113e5cf099911c5ed78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3a0b3e80ea3fa4fdec3fa3ade5ce8536a0e8ff00f17019a291646bc307ec6a84a8c5b12c6e247c8bfad51d6bf80df1bec3114ea9edf9b208537c5db28a1a523
|
7
|
+
data.tar.gz: 14af2506a7729e82c7ed664c61a0523e7dcf2e2595c83078986b6a8d590013dcb543b8e6d641f0b742dc23a9f785cd3585286e5066fb10a6a74dc463cd67ac35
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# 0.2.0 (27.04.2024)
|
2
|
+
|
3
|
+
- Add support for multiple anchormodels (`belongs_to_anchormodels`)
|
4
|
+
- Implement YourAnchormodelclass.form_collection
|
5
|
+
|
6
|
+
# 0.1.5 (24.04.2024)
|
7
|
+
|
8
|
+
- Enhance documentation
|
9
|
+
- Add support for simple_form
|
10
|
+
|
1
11
|
# 0.1.4 (23.04.2024)
|
2
12
|
|
3
13
|
- Stick closer to the Rails API of Value
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -35,7 +35,7 @@ are regular classes and can easily be extended.
|
|
35
35
|
|
36
36
|
### ActiveEnum
|
37
37
|
|
38
|
-
The gem (
|
38
|
+
The gem [ActiveEnum](https://github.com/adzap/active_enum) allows to create
|
39
39
|
Enum-like classes that can be extended. However it only supports Integer keys. I
|
40
40
|
find this unsatisfactory, as debugging with tools like `psql` or `mysql` is made
|
41
41
|
unnecessarily hard when you only see numbers. Keys for enums should be
|
@@ -116,6 +116,41 @@ puts("User #{@user.name} has role #{@user.role.label}")
|
|
116
116
|
@user.role.admin? # true if and only if the role is admin (false otherwise)
|
117
117
|
```
|
118
118
|
|
119
|
+
Your form could look something like this:
|
120
|
+
|
121
|
+
```erb
|
122
|
+
<%= form_with(model: user) do |form| %>
|
123
|
+
<%# ... %>
|
124
|
+
<%= form.collection_select :role, Role.all, :key, :label %>
|
125
|
+
<%# ... %>
|
126
|
+
<% end %>
|
127
|
+
```
|
128
|
+
|
129
|
+
## Using Anchormodel with Simpleform
|
130
|
+
|
131
|
+
Anchormodel has built-in support for the [simple_form](https://github.com/heartcombo/simple_form) gem by providing an input for the type `:anchormodel` which displays anchormodel attributes as a collection select.
|
132
|
+
|
133
|
+
After SimpleForm is installed, you can write your form as:
|
134
|
+
|
135
|
+
```erb
|
136
|
+
<%= simple_form_for user do |f| %>
|
137
|
+
<%# ... %>
|
138
|
+
<%= f.input :role %>
|
139
|
+
<%# ... %>
|
140
|
+
<% end %>
|
141
|
+
```
|
142
|
+
|
143
|
+
Or, if you prefer radio buttons instead:
|
144
|
+
|
145
|
+
|
146
|
+
```erb
|
147
|
+
<%= simple_form_for user do |f| %>
|
148
|
+
<%# ... %>
|
149
|
+
<%= f.input :role, as: :anchormodel_radio_buttons %>
|
150
|
+
<%# ... %>
|
151
|
+
<% end %>
|
152
|
+
```
|
153
|
+
|
119
154
|
# Rails Enum style model methods
|
120
155
|
|
121
156
|
By default, Anchormodel adds three kinds of methods for each key to the model:
|
@@ -188,6 +223,82 @@ If you want to have multiple attributes in the same model pointing to the same A
|
|
188
223
|
|
189
224
|
```ruby
|
190
225
|
# app/models/user.rb
|
191
|
-
|
192
|
-
|
226
|
+
belongs_to_anchormodel :role
|
227
|
+
belongs_to_anchormodel :secondary_role, Role, model_methods: false
|
228
|
+
```
|
229
|
+
|
230
|
+
# Attaching multiple Anchormodels to an attribute (similar to a has_many collection)
|
231
|
+
|
232
|
+
Collections of Anchormodels are supported. Assuming that your `User` can have multiple `Role` anchormodels, your code can look as follows:
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
# app/models/user.rb
|
236
|
+
belongs_to_anchormodels :roles
|
237
|
+
```
|
238
|
+
|
239
|
+
The method is deliberately called `belongs_to...` and not `has_many...` 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:
|
240
|
+
|
241
|
+
- Your column should be of type `string`, just like with the singular `belongs_to_anchormodel`.
|
242
|
+
- Anchormodels will be stored in string form, separated by the `,` character
|
243
|
+
- When reading the attribute, you will get a `Set`, thus duplicates are avoided.
|
244
|
+
|
245
|
+
Example usage for a User model with multiple roles as shown above:
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
u = User.first
|
249
|
+
u.roles = %i[moderator admin] # this will replace the user's roles by the two specified
|
250
|
+
u.roles # this will return a set of two Role Anchormodel instances, moderator and role
|
251
|
+
u.guest! # this will add the role `guest` to the user's roles
|
252
|
+
u.guest? # this will query whether the role `guest` is part of the user's roles
|
253
|
+
User.moderator # This will return all users that have the moderator role as part of their roles
|
254
|
+
```
|
255
|
+
|
256
|
+
For modifying a collection of Anchormodels, the following methods are implemented, the first three accepting a String, Symbol or Anchormodel:
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
u.roles.add(:moderator) # same as u.moderator!
|
260
|
+
u.roles << :moderator # alias of add
|
261
|
+
u.roles.delete(:moderator)
|
262
|
+
u.roles.clear
|
263
|
+
```
|
264
|
+
|
265
|
+
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.
|
266
|
+
|
267
|
+
## Basic rails form for a collection of Anchormodels
|
268
|
+
|
269
|
+
```erb
|
270
|
+
<%= form_with(model: user) do |form| %>
|
271
|
+
<%# ... %>
|
272
|
+
<%= form.collection_select :role, Role.all, :key, :label, multiple: true %>
|
273
|
+
<%# ... %>
|
274
|
+
<% end %>
|
275
|
+
```
|
276
|
+
|
277
|
+
If you get an error due to unpermitted params, make sure, you are allowing array-style parameters: `params.require(:user).permit(roles: [])`
|
278
|
+
|
279
|
+
## SimpleForm for a collection of Anchormodels
|
280
|
+
|
281
|
+
Anchormodel's [simple_form](https://github.com/heartcombo/simple_form) support also includes collections of Anchormodels.
|
282
|
+
|
283
|
+
Just like in the single Anchormodel implementation, a select input can be provided with:
|
284
|
+
|
285
|
+
```erb
|
286
|
+
<%= simple_form_for user do |f| %>
|
287
|
+
<%# ... %>
|
288
|
+
<%= f.input :role %>
|
289
|
+
<%# ... %>
|
290
|
+
<% end %>
|
291
|
+
```
|
292
|
+
|
293
|
+
The input figures out automatically that it is operating on a collection, so the form code is the same as for a single Anchormodel.
|
294
|
+
|
295
|
+
However, radio buttons are unsuitable for collections, so use check boxes instead:
|
296
|
+
|
297
|
+
|
298
|
+
```erb
|
299
|
+
<%= simple_form_for user do |f| %>
|
300
|
+
<%# ... %>
|
301
|
+
<%= f.input :role, as: :anchormodel_check_boxes %>
|
302
|
+
<%# ... %>
|
303
|
+
<% end %>
|
193
304
|
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/anchormodel.gemspec
CHANGED
@@ -2,21 +2,21 @@
|
|
2
2
|
# This file is auto-generated via: 'rake gemspec'.
|
3
3
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: anchormodel 0.
|
5
|
+
# stub: anchormodel 0.2.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "anchormodel".freeze
|
9
|
-
s.version = "0.
|
9
|
+
s.version = "0.2.0".freeze
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Sandro Kalbermatter".freeze]
|
14
|
-
s.date = "2024-04-
|
15
|
-
s.files = [".gitignore".freeze, ".ruby-version".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "Gemfile.lock".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "anchormodel.gemspec".freeze, "bin/rails".freeze, "doc/Anchormodel.html".freeze, "doc/Anchormodel/ActiveModelTypeValue.html".freeze, "doc/Anchormodel/Attribute.html".freeze, "doc/Anchormodel/ModelMixin.html".freeze, "doc/Anchormodel/Version.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/anchormodel.rb".freeze, "lib/anchormodel/active_model_type_value_single.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.rb".freeze, "lib/anchormodel/util.rb".freeze, "lib/anchormodel/version.rb".freeze, "lib/generators/anchormodel/USAGE".freeze, "lib/generators/anchormodel/anchormodel_generator.rb".freeze, "lib/generators/anchormodel/templates/anchormodel.rb.erb".freeze, "logo.svg".freeze, "
|
14
|
+
s.date = "2024-04-27"
|
15
|
+
s.files = [".gitignore".freeze, ".ruby-version".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "Gemfile.lock".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "anchormodel.gemspec".freeze, "bin/rails".freeze, "doc/Anchormodel.html".freeze, "doc/Anchormodel/ActiveModelTypeValue.html".freeze, "doc/Anchormodel/ActiveModelTypeValueSingle.html".freeze, "doc/Anchormodel/Attribute.html".freeze, "doc/Anchormodel/ModelMixin.html".freeze, "doc/Anchormodel/SimpleFormInputs.html".freeze, "doc/Anchormodel/SimpleFormInputs/Helpers.html".freeze, "doc/Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html".freeze, "doc/Anchormodel/Util.html".freeze, "doc/Anchormodel/Version.html".freeze, "doc/AnchormodelGenerator.html".freeze, "doc/AnchormodelInput.html".freeze, "doc/AnchormodelRadioButtonsInput.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/anchormodel.rb".freeze, "lib/anchormodel/active_model_type_value_multi.rb".freeze, "lib/anchormodel/active_model_type_value_single.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.rb".freeze, "lib/anchormodel/simple_form_inputs/anchormodel_check_boxes_input.rb".freeze, "lib/anchormodel/simple_form_inputs/anchormodel_input.rb".freeze, "lib/anchormodel/simple_form_inputs/anchormodel_radio_buttons_input.rb".freeze, "lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb".freeze, "lib/anchormodel/util.rb".freeze, "lib/anchormodel/version.rb".freeze, "lib/generators/anchormodel/USAGE".freeze, "lib/generators/anchormodel/anchormodel_generator.rb".freeze, "lib/generators/anchormodel/templates/anchormodel.rb.erb".freeze, "logo.svg".freeze, "test/active_record_model/user_test.rb".freeze, "test/dummy/.gitignore".freeze, "test/dummy/Rakefile".freeze, "test/dummy/app/anchormodels/animal.rb".freeze, "test/dummy/app/anchormodels/locale.rb".freeze, "test/dummy/app/anchormodels/role.rb".freeze, "test/dummy/app/helpers/application_helper.rb".freeze, "test/dummy/app/models/application_record.rb".freeze, "test/dummy/app/models/concerns/.keep".freeze, "test/dummy/app/models/user.rb".freeze, "test/dummy/bin/rails".freeze, "test/dummy/bin/rake".freeze, "test/dummy/bin/setup".freeze, "test/dummy/config.ru".freeze, "test/dummy/config/application.rb".freeze, "test/dummy/config/boot.rb".freeze, "test/dummy/config/credentials.yml.enc".freeze, "test/dummy/config/database.yml".freeze, "test/dummy/config/environment.rb".freeze, "test/dummy/config/environments/test.rb".freeze, "test/dummy/config/initializers/content_security_policy.rb".freeze, "test/dummy/config/initializers/filter_parameter_logging.rb".freeze, "test/dummy/config/initializers/inflections.rb".freeze, "test/dummy/config/initializers/permissions_policy.rb".freeze, "test/dummy/config/locales/en.yml".freeze, "test/dummy/config/puma.rb".freeze, "test/dummy/config/routes.rb".freeze, "test/dummy/db/migrate/20230107173151_create_users.rb".freeze, "test/dummy/db/migrate/20240425182000_add_animals_to_users.rb".freeze, "test/dummy/db/schema.rb".freeze, "test/dummy/db/seeds.rb".freeze, "test/dummy/lib/tasks/.keep".freeze, "test/dummy/log/.keep".freeze, "test/dummy/tmp/.keep".freeze, "test/dummy/tmp/pids/.keep".freeze, "test/test_helper.rb".freeze]
|
16
16
|
s.homepage = "https://github.com/kalsan/anchormodel".freeze
|
17
17
|
s.licenses = ["LGPL-3.0-or-later".freeze]
|
18
18
|
s.required_ruby_version = Gem::Requirement.new(">= 3.0.0".freeze)
|
19
|
-
s.rubygems_version = "3.5.
|
19
|
+
s.rubygems_version = "3.5.9".freeze
|
20
20
|
s.summary = "Bringing object-oriented programming to Rails enums".freeze
|
21
21
|
|
22
22
|
s.specification_version = 4
|