anchormodel 0.1.5 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/Gemfile.lock +6 -6
  4. data/README.md +78 -2
  5. data/VERSION +1 -1
  6. data/anchormodel.gemspec +4 -4
  7. data/doc/Anchormodel/ActiveModelTypeValueMulti.html +410 -0
  8. data/doc/Anchormodel/ActiveModelTypeValueSingle.html +113 -31
  9. data/doc/Anchormodel/Attribute.html +109 -9
  10. data/doc/Anchormodel/ModelMixin.html +3 -92
  11. data/doc/Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html +267 -0
  12. data/doc/Anchormodel/SimpleFormInputs/Helpers.html +124 -0
  13. data/doc/Anchormodel/SimpleFormInputs.html +124 -0
  14. data/doc/Anchormodel/Util.html +131 -15
  15. data/doc/Anchormodel/Version.html +3 -3
  16. data/doc/Anchormodel.html +97 -32
  17. data/doc/AnchormodelCheckBoxesInput.html +140 -0
  18. data/doc/AnchormodelGenerator.html +3 -3
  19. data/doc/AnchormodelInput.html +140 -0
  20. data/doc/AnchormodelRadioButtonsInput.html +140 -0
  21. data/doc/_index.html +16 -4
  22. data/doc/class_list.html +1 -1
  23. data/doc/file.README.html +77 -5
  24. data/doc/frames.html +1 -1
  25. data/doc/index.html +77 -5
  26. data/doc/method_list.html +49 -9
  27. data/doc/top-level-namespace.html +4 -4
  28. data/lib/anchormodel/active_model_type_value_multi.rb +31 -0
  29. data/lib/anchormodel/active_model_type_value_single.rb +4 -2
  30. data/lib/anchormodel/attribute.rb +7 -1
  31. data/lib/anchormodel/model_mixin.rb +7 -0
  32. data/lib/anchormodel/simple_form_inputs/anchormodel_check_boxes_input.rb +23 -0
  33. data/lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb +7 -3
  34. data/lib/anchormodel/util.rb +57 -7
  35. data/lib/anchormodel.rb +7 -0
  36. data/test/active_record_model/user_test.rb +73 -9
  37. data/test/dummy/app/anchormodels/animal.rb +6 -0
  38. data/test/dummy/app/models/user.rb +1 -0
  39. data/test/dummy/db/migrate/20240425182000_add_animals_to_users.rb +5 -0
  40. data/test/dummy/db/schema.rb +2 -1
  41. metadata +13 -2
@@ -21,10 +21,6 @@ class UserTest < Minitest::Test
21
21
  )
22
22
  end
23
23
 
24
- def test_missing_key
25
- assert_raises { Role.find(:does_not_exist) }
26
- end
27
-
28
24
  def test_basic_setters_and_getters
29
25
  u = User.create!(role: 'guest', locale: 'de') # String assignment
30
26
  assert_equal Role.find(:guest), u.role
@@ -57,11 +53,6 @@ class UserTest < Minitest::Test
57
53
  assert Role.find(:moderator) < Role.find(:admin)
58
54
  end
59
55
 
60
- def test_presence_validation
61
- valentine = User.new
62
- assert_raises(ActiveRecord::RecordInvalid) { valentine.save! }
63
- end
64
-
65
56
  def test_alternative_column_name
66
57
  ben = User.create!(
67
58
  role: Role.find(:moderator),
@@ -119,6 +110,19 @@ class UserTest < Minitest::Test
119
110
  assert_nil u.secondary_role
120
111
  end
121
112
 
113
+ ###---
114
+ # Testing failures
115
+ ###---
116
+
117
+ def test_presence_validation
118
+ valentine = User.new
119
+ assert_raises(ActiveRecord::RecordInvalid) { valentine.save! }
120
+ end
121
+
122
+ def test_missing_key
123
+ assert_raises { Role.find(:does_not_exist) }
124
+ end
125
+
122
126
  # Attempting to create a model with an invalid constant name should fail
123
127
  def test_invalid_key_update
124
128
  assert_raises(RuntimeError) { User.create!(role: :admin, locale: :de, preferred_locale: :invalid) }
@@ -137,4 +141,64 @@ class UserTest < Minitest::Test
137
141
  ActiveRecord::Base.connection.execute(sql)
138
142
  assert_raises(RuntimeError) { User.first.role }
139
143
  end
144
+
145
+ ###---
146
+ # Testing multiple anchormodel associations
147
+ ###---
148
+
149
+ def test_multi_basics
150
+ u = User.create!(role: 'guest', locale: 'de')
151
+ assert_equal(Set.new, u.animals)
152
+ # Adding
153
+ u.animals << 'cat'
154
+ assert_equal(Set.new([Animal.find(:cat)]), u.animals)
155
+ u.animals.add :dog
156
+ assert_equal(Set.new([Animal.find(:cat), Animal.find(:dog)]), u.animals)
157
+ u.animals.add Animal.find(:horse)
158
+ assert_equal(Set.new([Animal.find(:cat), Animal.find(:dog), Animal.find(:horse)]), u.animals)
159
+ # Removing
160
+ u.animals.delete 'cat'
161
+ assert_equal(Set.new([Animal.find(:dog), Animal.find(:horse)]), u.animals)
162
+ u.animals.delete :dog
163
+ assert_equal(Set.new([Animal.find(:horse)]), u.animals)
164
+ u.animals.delete Animal.find(:horse)
165
+ assert_equal(false, u.animals.any?)
166
+ # Setting
167
+ u.animals = %i[cat dog]
168
+ assert_equal(Set.new([Animal.find(:cat), Animal.find(:dog)]), u.animals)
169
+ # Clearing
170
+ u.animals.clear
171
+ assert_equal(false, u.animals.any?)
172
+ end
173
+
174
+ def test_multi_save_load
175
+ u = User.create!(role: 'guest', locale: 'de')
176
+ u.animals = %i[cat dog]
177
+ u.save!
178
+ freshly_loaded_u = User.first
179
+ assert_equal(Set.new([Animal.find(:cat), Animal.find(:dog)]), freshly_loaded_u.animals)
180
+ end
181
+
182
+ def test_multi_model_readers_and_writers
183
+ u = User.create!(role: 'guest', locale: 'de')
184
+ u.cat!
185
+ u.cat!
186
+ assert_equal(Set.new([Animal.find(:cat)]), u.animals) # tolerate no duplicate cat
187
+ assert_equal(true, u.cat?)
188
+ u.dog!
189
+ assert_equal(true, u.cat?)
190
+ assert_equal(true, u.dog?)
191
+ assert_equal(false, u.horse?)
192
+ end
193
+
194
+ def test_multi_model_scopes
195
+ u = User.create!(role: 'guest', locale: 'fr', animals: %w[dog cat])
196
+ v = User.create!(role: 'guest', locale: 'it', animals: %w[dog horse])
197
+ assert_equal(0, User.rat.count)
198
+ assert_equal(1, User.cat.count)
199
+ assert_equal(1, User.horse.count)
200
+ assert_equal(2, User.dog.count)
201
+ assert_equal(u.id, User.cat.first.id)
202
+ assert_equal(v.id, User.horse.first.id)
203
+ end
140
204
  end
@@ -0,0 +1,6 @@
1
+ class Animal < Anchormodel
2
+ new :cat
3
+ new :dog
4
+ new :horse
5
+ new :rat
6
+ end
@@ -3,4 +3,5 @@ class User < ApplicationRecord
3
3
  belongs_to_anchormodel :secondary_role, Role, optional: true, model_readers: false, model_writers: false, model_scopes: false
4
4
  belongs_to_anchormodel :locale, model_methods: false
5
5
  belongs_to_anchormodel :preferred_locale, Locale
6
+ belongs_to_anchormodels :animals
6
7
  end
@@ -0,0 +1,5 @@
1
+ class AddAnimalsToUsers < ActiveRecord::Migration[7.0]
2
+ def change
3
+ add_column :users, :animals, :string, default: '', null: false
4
+ end
5
+ end
@@ -10,7 +10,7 @@
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: 20_230_124_084_241) do
13
+ ActiveRecord::Schema[7.0].define(version: 20_240_425_182_000) do
14
14
  create_table 'users', force: :cascade do |t|
15
15
  t.string 'role'
16
16
  t.string 'locale'
@@ -18,5 +18,6 @@ ActiveRecord::Schema[7.0].define(version: 20_230_124_084_241) do
18
18
  t.datetime 'updated_at', null: false
19
19
  t.string 'secondary_role'
20
20
  t.string 'preferred_locale', default: 'en', null: false
21
+ t.string 'animals', default: '', null: false
21
22
  end
22
23
  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.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Kalbermatter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-24 00:00:00.000000000 Z
11
+ date: 2024-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -170,12 +170,19 @@ files:
170
170
  - bin/rails
171
171
  - doc/Anchormodel.html
172
172
  - doc/Anchormodel/ActiveModelTypeValue.html
173
+ - doc/Anchormodel/ActiveModelTypeValueMulti.html
173
174
  - doc/Anchormodel/ActiveModelTypeValueSingle.html
174
175
  - doc/Anchormodel/Attribute.html
175
176
  - doc/Anchormodel/ModelMixin.html
177
+ - doc/Anchormodel/SimpleFormInputs.html
178
+ - doc/Anchormodel/SimpleFormInputs/Helpers.html
179
+ - doc/Anchormodel/SimpleFormInputs/Helpers/AnchormodelInputsCommon.html
176
180
  - doc/Anchormodel/Util.html
177
181
  - doc/Anchormodel/Version.html
182
+ - doc/AnchormodelCheckBoxesInput.html
178
183
  - doc/AnchormodelGenerator.html
184
+ - doc/AnchormodelInput.html
185
+ - doc/AnchormodelRadioButtonsInput.html
179
186
  - doc/_index.html
180
187
  - doc/class_list.html
181
188
  - doc/css/common.css
@@ -191,9 +198,11 @@ files:
191
198
  - doc/method_list.html
192
199
  - doc/top-level-namespace.html
193
200
  - lib/anchormodel.rb
201
+ - lib/anchormodel/active_model_type_value_multi.rb
194
202
  - lib/anchormodel/active_model_type_value_single.rb
195
203
  - lib/anchormodel/attribute.rb
196
204
  - lib/anchormodel/model_mixin.rb
205
+ - lib/anchormodel/simple_form_inputs/anchormodel_check_boxes_input.rb
197
206
  - lib/anchormodel/simple_form_inputs/anchormodel_input.rb
198
207
  - lib/anchormodel/simple_form_inputs/anchormodel_radio_buttons_input.rb
199
208
  - lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb
@@ -206,6 +215,7 @@ files:
206
215
  - test/active_record_model/user_test.rb
207
216
  - test/dummy/.gitignore
208
217
  - test/dummy/Rakefile
218
+ - test/dummy/app/anchormodels/animal.rb
209
219
  - test/dummy/app/anchormodels/locale.rb
210
220
  - test/dummy/app/anchormodels/role.rb
211
221
  - test/dummy/app/helpers/application_helper.rb
@@ -230,6 +240,7 @@ files:
230
240
  - test/dummy/config/puma.rb
231
241
  - test/dummy/config/routes.rb
232
242
  - test/dummy/db/migrate/20230107173151_create_users.rb
243
+ - test/dummy/db/migrate/20240425182000_add_animals_to_users.rb
233
244
  - test/dummy/db/schema.rb
234
245
  - test/dummy/db/seeds.rb
235
246
  - test/dummy/lib/tasks/.keep