anchormodel 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/Gemfile.lock +1 -1
- data/README.md +8 -0
- data/anchormodel.gemspec +16 -31
- data/doc/Anchormodel/ActiveModelTypeValue.html +13 -9
- data/doc/Anchormodel/Attribute.html +1 -1
- data/doc/Anchormodel/ModelMixin.html +4 -4
- data/doc/Anchormodel/Version.html +2 -2
- data/doc/Anchormodel.html +1 -1
- data/doc/_index.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/top-level-namespace.html +1 -1
- data/lib/anchormodel/active_model_type_value.rb +2 -0
- data/lib/anchormodel/model_mixin.rb +3 -3
- data/lib/anchormodel/version.rb +1 -1
- data/lib/generators/anchormodel/USAGE +8 -0
- data/lib/generators/anchormodel/anchormodel_generator.rb +11 -0
- data/lib/generators/anchormodel/templates/anchormodel.rb.erb +3 -0
- data/test/active_record_model/user_test.rb +9 -3
- data/test/dummy/db/schema.rb +8 -9
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '01259cd9abb8c08dd46ee9ebc39697fbd8eb17128bd09d8e2ffd20aca88b3c68'
|
4
|
+
data.tar.gz: 93eb6c57fae4d705cee6d92e9a5e5a3c5cfd56e3cc60306e906d5654521ae586
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 564faa96c2eea1cc8bcbb3c05dbe9b7c86dd0a62618cff2d3ed56611a3c4a1431e1a2e1bfdaa30e7ccbd1a2a4723648aeb269ed6ef656c44daa01d21488516a3
|
7
|
+
data.tar.gz: 18d9556c138412dfa107e9958a3f4bfd8e992b9a26c85bd3516d3bc7afc3bee700c72b7d915fde69787087334be8692bf6afbb77d3dab1bc233c4be7b27ee6d2
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# 0.1.3 (19.05.2023)
|
2
|
+
|
3
|
+
- Add Rails generator
|
4
|
+
|
5
|
+
# 0.1.2 (25.01.2023)
|
6
|
+
|
7
|
+
- Consider empty string as nil (this is necessary to make Rails assignments work)
|
8
|
+
|
9
|
+
# 0.1.1 (24.01.2023)
|
2
10
|
|
3
11
|
- Add boolean accessor directly to all anchormodels
|
4
12
|
- Add ActiveRecord::Enum style readers, writers and scopes to the model
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -50,6 +50,14 @@ entries of an Anchormodel.
|
|
50
50
|
1. Add gem to Gemfile: `gem 'anchormodel'`
|
51
51
|
2. In `application_record.rb`, add in the class body: `include Anchormodel::ModelMixin`
|
52
52
|
|
53
|
+
# Generator
|
54
|
+
|
55
|
+
For convenience, Anchormodel provides a Rails generator:
|
56
|
+
|
57
|
+
`rails generate anchormodel Role`
|
58
|
+
|
59
|
+
This will create `app/anchormodels/role.rb`.
|
60
|
+
|
53
61
|
# Basic example
|
54
62
|
|
55
63
|
`app/anchormodels/role.rb`:
|
data/anchormodel.gemspec
CHANGED
@@ -2,48 +2,33 @@
|
|
2
2
|
# This file is auto-generated via: 'rake gemspec'.
|
3
3
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: anchormodel 0.1.
|
5
|
+
# stub: anchormodel 0.1.3 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "anchormodel".freeze
|
9
|
-
s.version = "0.1.
|
9
|
+
s.version = "0.1.3"
|
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 = "2023-
|
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, "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.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.rb".freeze, "lib/anchormodel/version.rb".freeze, "logo.svg".freeze, "test/active_record_model/user_test.rb".freeze, "test/dummy/.gitignore".freeze, "test/dummy/Rakefile".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/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]
|
14
|
+
s.date = "2023-05-19"
|
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, "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.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.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/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/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.
|
19
|
+
s.rubygems_version = "3.4.13".freeze
|
20
20
|
s.summary = "Bringing object-oriented programming to Rails enums".freeze
|
21
21
|
|
22
|
-
|
23
|
-
s.specification_version = 4
|
24
|
-
end
|
22
|
+
s.specification_version = 4
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
s.add_development_dependency(%q<sqlite3>.freeze, ["~> 1.6.0"])
|
37
|
-
else
|
38
|
-
s.add_dependency(%q<rails>.freeze, ["~> 7.0"])
|
39
|
-
s.add_dependency(%q<rake>.freeze, ["~> 13.0.6"])
|
40
|
-
s.add_dependency(%q<pry>.freeze, ["~> 0.14.1"])
|
41
|
-
s.add_dependency(%q<rubocop>.freeze, ["~> 1.43.0"])
|
42
|
-
s.add_dependency(%q<rubocop-rails>.freeze, ["~> 2.17.4"])
|
43
|
-
s.add_dependency(%q<yard>.freeze, ["~> 0.9.28"])
|
44
|
-
s.add_dependency(%q<yard-activesupport-concern>.freeze, ["~> 0.0.1"])
|
45
|
-
s.add_dependency(%q<minitest>.freeze, ["~> 5.17.0"])
|
46
|
-
s.add_dependency(%q<minitest-reporters>.freeze, ["~> 1.5.0"])
|
47
|
-
s.add_dependency(%q<sqlite3>.freeze, ["~> 1.6.0"])
|
48
|
-
end
|
24
|
+
s.add_runtime_dependency(%q<rails>.freeze, ["~> 7.0"])
|
25
|
+
s.add_development_dependency(%q<rake>.freeze, ["~> 13.0.6"])
|
26
|
+
s.add_development_dependency(%q<pry>.freeze, ["~> 0.14.1"])
|
27
|
+
s.add_development_dependency(%q<rubocop>.freeze, ["~> 1.43.0"])
|
28
|
+
s.add_development_dependency(%q<rubocop-rails>.freeze, ["~> 2.17.4"])
|
29
|
+
s.add_development_dependency(%q<yard>.freeze, ["~> 0.9.28"])
|
30
|
+
s.add_development_dependency(%q<yard-activesupport-concern>.freeze, ["~> 0.0.1"])
|
31
|
+
s.add_development_dependency(%q<minitest>.freeze, ["~> 5.17.0"])
|
32
|
+
s.add_development_dependency(%q<minitest-reporters>.freeze, ["~> 1.5.0"])
|
33
|
+
s.add_development_dependency(%q<sqlite3>.freeze, ["~> 1.6.0"])
|
49
34
|
end
|
@@ -381,12 +381,12 @@
|
|
381
381
|
<pre class="lines">
|
382
382
|
|
383
383
|
|
384
|
-
|
385
|
-
|
386
|
-
|
384
|
+
36
|
385
|
+
37
|
386
|
+
38</pre>
|
387
387
|
</td>
|
388
388
|
<td>
|
389
|
-
<pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line
|
389
|
+
<pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 36</span>
|
390
390
|
|
391
391
|
<span class='kw'>def</span> <span class='id identifier rubyid_changed?'>changed?</span><span class='lparen'>(</span><span class='id identifier rubyid_old_value'>old_value</span><span class='comma'>,</span> <span class='id identifier rubyid_new_value'>new_value</span><span class='comma'>,</span> <span class='id identifier rubyid__new_value_before_type_cast'>_new_value_before_type_cast</span><span class='rparen'>)</span>
|
392
392
|
<span class='kw'>return</span> <span class='id identifier rubyid_deserialize'>deserialize</span><span class='lparen'>(</span><span class='id identifier rubyid_old_value'>old_value</span><span class='rparen'>)</span> <span class='op'>!=</span> <span class='id identifier rubyid_deserialize'>deserialize</span><span class='lparen'>(</span><span class='id identifier rubyid_new_value'>new_value</span><span class='rparen'>)</span>
|
@@ -420,15 +420,17 @@
|
|
420
420
|
<pre class="lines">
|
421
421
|
|
422
422
|
|
423
|
-
29
|
424
423
|
30
|
425
424
|
31
|
426
|
-
32
|
425
|
+
32
|
426
|
+
33
|
427
|
+
34</pre>
|
427
428
|
</td>
|
428
429
|
<td>
|
429
|
-
<pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line
|
430
|
+
<pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 30</span>
|
430
431
|
|
431
432
|
<span class='kw'>def</span> <span class='id identifier rubyid_deserialize'>deserialize</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
433
|
+
<span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_presence'>presence</span>
|
432
434
|
<span class='kw'>return</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='ivar'>@attribute</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='rparen'>)</span>
|
433
435
|
<span class='kw'>return</span> <span class='ivar'>@attribute</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
434
436
|
<span class='kw'>end</span></pre>
|
@@ -477,12 +479,14 @@
|
|
477
479
|
24
|
478
480
|
25
|
479
481
|
26
|
480
|
-
27
|
482
|
+
27
|
483
|
+
28</pre>
|
481
484
|
</td>
|
482
485
|
<td>
|
483
486
|
<pre class="code"><span class="info file"># File 'lib/anchormodel/active_model_type_value.rb', line 13</span>
|
484
487
|
|
485
488
|
<span class='kw'>def</span> <span class='id identifier rubyid_serialize'>serialize</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
489
|
+
<span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_presence'>presence</span>
|
486
490
|
<span class='kw'>return</span> <span class='kw'>case</span> <span class='id identifier rubyid_value'>value</span>
|
487
491
|
<span class='kw'>when</span> <span class='const'>Symbol</span><span class='comma'>,</span> <span class='const'>String</span>
|
488
492
|
<span class='kw'>unless</span> <span class='ivar'>@attribute</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_valid_keys'>valid_keys</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span>
|
@@ -507,7 +511,7 @@
|
|
507
511
|
</div>
|
508
512
|
|
509
513
|
<div id="footer">
|
510
|
-
Generated on
|
514
|
+
Generated on Wed Jan 25 12:36:40 2023 by
|
511
515
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
512
516
|
0.9.28 (ruby-3.1.3).
|
513
517
|
</div>
|
@@ -496,7 +496,7 @@
|
|
496
496
|
</div>
|
497
497
|
|
498
498
|
<div id="footer">
|
499
|
-
Generated on
|
499
|
+
Generated on Wed Jan 25 12:36:40 2023 by
|
500
500
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
501
501
|
0.9.28 (ruby-3.1.3).
|
502
502
|
</div>
|
@@ -382,7 +382,7 @@
|
|
382
382
|
<pre class="code"><span class="info file"># File 'lib/anchormodel/model_mixin.rb', line 20</span>
|
383
383
|
|
384
384
|
<span class='kw'>def</span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='label'>optional:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>model_readers:</span> <span class='kw'>true</span><span class='comma'>,</span>
|
385
|
-
|
385
|
+
<span class='label'>model_writers:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_scopes:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
386
386
|
<span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span> <span class='op'>||=</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_classify'>classify</span><span class='period'>.</span><span class='id identifier rubyid_constantize'>constantize</span>
|
387
387
|
<span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
|
388
388
|
<span class='id identifier rubyid_attribute'>attribute</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="../Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="Attribute.html#initialize-instance_method" title="Anchormodel::Attribute#initialize (method)">new</a></span></span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='comma'>,</span> <span class='id identifier rubyid_optional'>optional</span><span class='rparen'>)</span>
|
@@ -440,7 +440,7 @@
|
|
440
440
|
<span class='id identifier rubyid_fail'>fail</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Anchormodel writer </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>! already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_writers: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
|
441
441
|
<span class='kw'>end</span>
|
442
442
|
<span class='id identifier rubyid_define_method'>define_method</span><span class='lparen'>(</span><span class='symbol'>:"#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>!</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span> <span class='kw'>do</span>
|
443
|
-
<span class='id identifier rubyid_public_send'>public_send</span><span class='lparen'>(</span><span class='symbol'>:"#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='
|
443
|
+
<span class='id identifier rubyid_public_send'>public_send</span><span class='lparen'>(</span><span class='symbol'>:"#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>=</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='id identifier rubyid_entry'>entry</span><span class='rparen'>)</span>
|
444
444
|
<span class='kw'>end</span>
|
445
445
|
<span class='kw'>end</span>
|
446
446
|
<span class='kw'>end</span>
|
@@ -452,7 +452,7 @@
|
|
452
452
|
<span class='kw'>if</span> <span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
|
453
453
|
<span class='id identifier rubyid_fail'>fail</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Anchormodel scope </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'> already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_scopes: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span>
|
454
454
|
<span class='kw'>end</span>
|
455
|
-
<span class='id identifier rubyid_scope'>scope</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='tlambda'>-></span
|
455
|
+
<span class='id identifier rubyid_scope'>scope</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='tlambda'>-></span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=></span> <span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span> <span class='rbrace'>}</span><span class='rparen'>)</span>
|
456
456
|
<span class='kw'>end</span>
|
457
457
|
<span class='kw'>end</span>
|
458
458
|
<span class='kw'>end</span></pre>
|
@@ -466,7 +466,7 @@
|
|
466
466
|
</div>
|
467
467
|
|
468
468
|
<div id="footer">
|
469
|
-
Generated on
|
469
|
+
Generated on Wed Jan 25 12:36:40 2023 by
|
470
470
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
471
471
|
0.9.28 (ruby-3.1.3).
|
472
472
|
</div>
|
@@ -141,7 +141,7 @@
|
|
141
141
|
|
142
142
|
</div>
|
143
143
|
</dt>
|
144
|
-
<dd><pre class="code"><span class='int'>
|
144
|
+
<dd><pre class="code"><span class='int'>2</span></pre></dd>
|
145
145
|
|
146
146
|
<dt id="EDGE-constant" class="">EDGE =
|
147
147
|
<div class="docstring">
|
@@ -185,7 +185,7 @@
|
|
185
185
|
</div>
|
186
186
|
|
187
187
|
<div id="footer">
|
188
|
-
Generated on
|
188
|
+
Generated on Wed Jan 25 12:36:39 2023 by
|
189
189
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
190
190
|
0.9.28 (ruby-3.1.3).
|
191
191
|
</div>
|
data/doc/Anchormodel.html
CHANGED
@@ -934,7 +934,7 @@
|
|
934
934
|
</div>
|
935
935
|
|
936
936
|
<div id="footer">
|
937
|
-
Generated on
|
937
|
+
Generated on Wed Jan 25 12:36:40 2023 by
|
938
938
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
939
939
|
0.9.28 (ruby-3.1.3).
|
940
940
|
</div>
|
data/doc/_index.html
CHANGED
@@ -141,7 +141,7 @@
|
|
141
141
|
</div>
|
142
142
|
|
143
143
|
<div id="footer">
|
144
|
-
Generated on
|
144
|
+
Generated on Wed Jan 25 12:36:39 2023 by
|
145
145
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
146
146
|
0.9.28 (ruby-3.1.3).
|
147
147
|
</div>
|
data/doc/file.README.html
CHANGED
@@ -228,7 +228,7 @@
|
|
228
228
|
</div></div>
|
229
229
|
|
230
230
|
<div id="footer">
|
231
|
-
Generated on
|
231
|
+
Generated on Wed Jan 25 12:36:39 2023 by
|
232
232
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
233
233
|
0.9.28 (ruby-3.1.3).
|
234
234
|
</div>
|
data/doc/index.html
CHANGED
@@ -228,7 +228,7 @@
|
|
228
228
|
</div></div>
|
229
229
|
|
230
230
|
<div id="footer">
|
231
|
-
Generated on
|
231
|
+
Generated on Wed Jan 25 12:36:39 2023 by
|
232
232
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
233
233
|
0.9.28 (ruby-3.1.3).
|
234
234
|
</div>
|
@@ -100,7 +100,7 @@
|
|
100
100
|
</div>
|
101
101
|
|
102
102
|
<div id="footer">
|
103
|
-
Generated on
|
103
|
+
Generated on Wed Jan 25 12:36:39 2023 by
|
104
104
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
105
105
|
0.9.28 (ruby-3.1.3).
|
106
106
|
</div>
|
@@ -11,6 +11,7 @@ class Anchormodel::ActiveModelTypeValue < ActiveModel::Type::Value
|
|
11
11
|
|
12
12
|
# Implementing this instead of cast to force key validation in any case
|
13
13
|
def serialize(value)
|
14
|
+
value = value.presence
|
14
15
|
return case value
|
15
16
|
when Symbol, String
|
16
17
|
unless @attribute.anchormodel_class.valid_keys.include?(value.to_sym)
|
@@ -27,6 +28,7 @@ class Anchormodel::ActiveModelTypeValue < ActiveModel::Type::Value
|
|
27
28
|
end
|
28
29
|
|
29
30
|
def deserialize(value)
|
31
|
+
value = value.presence
|
30
32
|
return value if value.is_a?(@attribute.anchormodel_class)
|
31
33
|
return @attribute.anchormodel_class.find(value)
|
32
34
|
end
|
@@ -18,7 +18,7 @@ module Anchormodel::ModelMixin
|
|
18
18
|
# @param model_scopes [Boolean] If true, the model is given an ActiveRecord::Enum style scope `MyModel.mykey` for each key in the anchormodel
|
19
19
|
# @param model_methods [Boolean, NilClass] If non-nil, this mass-assigns and overrides `model_readers`, `model_writers` and `model_scopes`
|
20
20
|
def belongs_to_anchormodel(attribute_name, anchormodel_class = nil, optional: false, model_readers: true,
|
21
|
-
|
21
|
+
model_writers: true, model_scopes: true, model_methods: nil)
|
22
22
|
anchormodel_class ||= attribute_name.to_s.classify.constantize
|
23
23
|
attribute_name = attribute_name.to_sym
|
24
24
|
attribute = Anchormodel::Attribute.new(self, attribute_name, anchormodel_class, optional)
|
@@ -76,7 +76,7 @@ module Anchormodel::ModelMixin
|
|
76
76
|
fail("Anchormodel writer #{entry.key}! already defined for #{self}, add `model_writers: false` to `belongs_to_anchormodel :#{attribute_name}`.")
|
77
77
|
end
|
78
78
|
define_method(:"#{entry.key}!") do
|
79
|
-
public_send(:"#{attribute_name
|
79
|
+
public_send(:"#{attribute_name}=", entry)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
@@ -88,7 +88,7 @@ module Anchormodel::ModelMixin
|
|
88
88
|
if respond_to?(entry.key)
|
89
89
|
fail("Anchormodel scope #{entry.key} already defined for #{self}, add `model_scopes: false` to `belongs_to_anchormodel :#{attribute_name}`.")
|
90
90
|
end
|
91
|
-
scope(entry.key, ->{where(attribute_name => entry.key)})
|
91
|
+
scope(entry.key, -> { where(attribute_name => entry.key) })
|
92
92
|
end
|
93
93
|
end
|
94
94
|
end
|
data/lib/anchormodel/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
class AnchormodelGenerator < Rails::Generators::NamedBase
|
2
|
+
source_root File.expand_path('templates', __dir__)
|
3
|
+
|
4
|
+
def add_anchormodel
|
5
|
+
fail('NAME must be present.') if name.blank?
|
6
|
+
@klass = @name.camelize
|
7
|
+
@filename = @name.underscore
|
8
|
+
|
9
|
+
template 'anchormodel.rb.erb', "app/anchormodels/#{@filename}.rb"
|
10
|
+
end
|
11
|
+
end
|
@@ -53,7 +53,7 @@ class UserTest < Minitest::Test
|
|
53
53
|
def test_custom_comparison
|
54
54
|
assert_equal(-1, Role.find(:moderator) <=> Role.find(:admin))
|
55
55
|
assert_equal(1, Role.find(:moderator) <=> Role.find(:guest))
|
56
|
-
assert_equal(0, Role.find(:moderator) <=> Role.find(
|
56
|
+
assert_equal(0, Role.find(:moderator) <=> Role.find('moderator'))
|
57
57
|
assert Role.find(:moderator) < Role.find(:admin)
|
58
58
|
end
|
59
59
|
|
@@ -64,9 +64,9 @@ class UserTest < Minitest::Test
|
|
64
64
|
|
65
65
|
def test_alternative_column_name
|
66
66
|
ben = User.create!(
|
67
|
-
role:
|
67
|
+
role: Role.find(:moderator),
|
68
68
|
secondary_role: Role.find(:admin),
|
69
|
-
locale:
|
69
|
+
locale: Locale.find(:de)
|
70
70
|
)
|
71
71
|
assert_equal(Role.find(:moderator), ben.role)
|
72
72
|
assert_equal(Role.find(:admin), ben.secondary_role)
|
@@ -112,4 +112,10 @@ class UserTest < Minitest::Test
|
|
112
112
|
assert_equal 1, User.fr.count
|
113
113
|
assert_equal 0, User.en.count
|
114
114
|
end
|
115
|
+
|
116
|
+
def test_rails_blank_assignment
|
117
|
+
u = User.new(role: :admin, secondary_role: :admin, locale: :en, preferred_locale: :en)
|
118
|
+
u.secondary_role = ''
|
119
|
+
assert_nil u.secondary_role
|
120
|
+
end
|
115
121
|
end
|
data/test/dummy/db/schema.rb
CHANGED
@@ -10,14 +10,13 @@
|
|
10
10
|
#
|
11
11
|
# It's strongly recommended that you check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema[7.0].define(version:
|
14
|
-
create_table
|
15
|
-
t.string
|
16
|
-
t.string
|
17
|
-
t.datetime
|
18
|
-
t.datetime
|
19
|
-
t.string
|
20
|
-
t.string
|
13
|
+
ActiveRecord::Schema[7.0].define(version: 20_230_124_084_241) do
|
14
|
+
create_table 'users', force: :cascade do |t|
|
15
|
+
t.string 'role'
|
16
|
+
t.string 'locale'
|
17
|
+
t.datetime 'created_at', null: false
|
18
|
+
t.datetime 'updated_at', null: false
|
19
|
+
t.string 'secondary_role'
|
20
|
+
t.string 'preferred_locale', default: 'en', null: false
|
21
21
|
end
|
22
|
-
|
23
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anchormodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sandro Kalbermatter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -191,6 +191,9 @@ files:
|
|
191
191
|
- lib/anchormodel/attribute.rb
|
192
192
|
- lib/anchormodel/model_mixin.rb
|
193
193
|
- lib/anchormodel/version.rb
|
194
|
+
- lib/generators/anchormodel/USAGE
|
195
|
+
- lib/generators/anchormodel/anchormodel_generator.rb
|
196
|
+
- lib/generators/anchormodel/templates/anchormodel.rb.erb
|
194
197
|
- logo.svg
|
195
198
|
- test/active_record_model/user_test.rb
|
196
199
|
- test/dummy/.gitignore
|
@@ -245,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
245
248
|
- !ruby/object:Gem::Version
|
246
249
|
version: '0'
|
247
250
|
requirements: []
|
248
|
-
rubygems_version: 3.
|
251
|
+
rubygems_version: 3.4.13
|
249
252
|
signing_key:
|
250
253
|
specification_version: 4
|
251
254
|
summary: Bringing object-oriented programming to Rails enums
|