polymorphic_belongs_to 0.0.0 → 0.2.0
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.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b1931a69a1ca4d6916e27182f40285e34d7a6d84d55796ff19c35faa0d91c425
|
|
4
|
+
data.tar.gz: cb2778380cf8c93d0846356562dc98a0d47411c13d686c41561517da208ebaef
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e6cb3727467ec304122f5c0ca97f9b38a79f23d7837ff68f2eee2fcc3971f826d67f3498eb07ae83deac3c3f6edb131f011936b33f1ab13ea7dcff87c355067c
|
|
7
|
+
data.tar.gz: e47a40c40f3a9c092e2592d5b4d4154b47650a28b4cd076fe0a29da7f8d316707a868966a9df59056a04ab960b35593834e26590c79d9618ad5d9bfd5637b5c8
|
data/README.md
CHANGED
|
@@ -16,12 +16,33 @@ bundle add polymorphic_belongs_to
|
|
|
16
16
|
class Comment < ApplicationRecord
|
|
17
17
|
include PolymorphicBelongsTo
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
polymorphic_belongs_to :commentable, types: [Post, Photo]
|
|
20
|
+
|
|
21
|
+
# Disable the "type inclusion" validator
|
|
22
|
+
polymorphic_belongs_to :commentable, types: [Post, Photo], validate_types: false
|
|
23
|
+
|
|
24
|
+
# Customize the "type inclusion" validator
|
|
25
|
+
polymorphic_belongs_to :commentable,
|
|
26
|
+
types: [Post, Photo],
|
|
27
|
+
validate_types: { in: ["Post", "Photo", "Repost"], message: "..." }
|
|
20
28
|
end
|
|
21
29
|
```
|
|
22
30
|
|
|
23
31
|
Run `bin/tapioca dsl` to regenerate RBIs.
|
|
24
32
|
|
|
33
|
+
### Options
|
|
34
|
+
|
|
35
|
+
`polymorphic_belongs_to(name, types:, validate_types: true, optional: nil, **options)`
|
|
36
|
+
|
|
37
|
+
- `name` — The association name, e.g. `:commentable`.
|
|
38
|
+
- `types:` — An array of allowed target classes. Used both as the source of truth for the type-inclusion validator and by the Tapioca DSL compiler when generating RBIs.
|
|
39
|
+
- `validate_types:` — Controls the validation of the "type" field:
|
|
40
|
+
- `true`: (Default) installs the validator with the list of allowed target classes: `validates :"#{name}_type", inclusion: ...`
|
|
41
|
+
- `false`: Skips the type-inclusion validator
|
|
42
|
+
- `{...}`: A Hash is passed straight through to `validates ..., inclusion:`, so you can override `in:`, supply a custom `message:`, etc.
|
|
43
|
+
- `optional:` — Forwarded to `belongs_to`, see Rails documentation. This also controls the type inclusion validator's `allow_nil` option.
|
|
44
|
+
- `**options` — Any remaining keyword arguments are forwarded to the underlying `belongs_to(name, polymorphic: true, ...)` call (e.g. `inverse_of:`, `dependent:`).
|
|
45
|
+
|
|
25
46
|
## Development
|
|
26
47
|
|
|
27
48
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -10,15 +10,15 @@ module PolymorphicBelongsTo
|
|
|
10
10
|
extend ActiveSupport::Concern
|
|
11
11
|
|
|
12
12
|
included do
|
|
13
|
-
class_attribute :
|
|
13
|
+
class_attribute :polymorphic_belongs_to_types, instance_writer: false
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
module ClassMethods
|
|
17
|
-
def
|
|
17
|
+
def polymorphic_belongs_to(name, types:, validate_types: true, **options)
|
|
18
18
|
name = name.to_sym
|
|
19
19
|
|
|
20
|
-
self.
|
|
21
|
-
|
|
20
|
+
self.polymorphic_belongs_to_types ||= {}
|
|
21
|
+
polymorphic_belongs_to_types[name.to_s] = types
|
|
22
22
|
|
|
23
23
|
belongs_to(name, polymorphic: true, **options)
|
|
24
24
|
|
|
@@ -26,7 +26,8 @@ module PolymorphicBelongsTo
|
|
|
26
26
|
foreign_type = reflect_on_association(name).foreign_type.to_sym
|
|
27
27
|
presence_required = validators_on(name).any? { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator) }
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
inclusion_options = validate_types.is_a?(Hash) ? validate_types : {in: types.map(&:name)}
|
|
30
|
+
validates(foreign_type, inclusion: inclusion_options, allow_nil: !presence_required)
|
|
30
31
|
end
|
|
31
32
|
end
|
|
32
33
|
end
|
|
@@ -15,12 +15,12 @@ module Tapioca
|
|
|
15
15
|
def self.gather_constants
|
|
16
16
|
all_classes.select do |klass|
|
|
17
17
|
klass < ActiveRecord::Base &&
|
|
18
|
-
klass.respond_to?(:
|
|
18
|
+
klass.respond_to?(:polymorphic_belongs_to_types)
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def decorate
|
|
23
|
-
defined = T.unsafe(constant).
|
|
23
|
+
defined = T.unsafe(constant).polymorphic_belongs_to_types
|
|
24
24
|
return unless defined&.any?
|
|
25
25
|
|
|
26
26
|
root.create_path(constant) do |model|
|
|
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
|
9
9
|
spec.authors = ["Zach Ahn"]
|
|
10
10
|
spec.email = ["engineering@zachahn.com"]
|
|
11
11
|
|
|
12
|
-
spec.summary = "PolymorphicBelongsTo provides a type-safe `
|
|
12
|
+
spec.summary = "PolymorphicBelongsTo provides a type-safe `polymorphic_belongs_to` association to ActiveRecord. Relies on Sorbet + Tapioca."
|
|
13
13
|
spec.homepage = "https://github.com/zachahn/polymorphic_belongs_to"
|
|
14
14
|
spec.license = "MIT"
|
|
15
15
|
spec.required_ruby_version = ">= 3.2.0"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: polymorphic_belongs_to
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Zach Ahn
|
|
@@ -85,6 +85,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
85
85
|
requirements: []
|
|
86
86
|
rubygems_version: 3.6.9
|
|
87
87
|
specification_version: 4
|
|
88
|
-
summary: PolymorphicBelongsTo provides a type-safe `
|
|
88
|
+
summary: PolymorphicBelongsTo provides a type-safe `polymorphic_belongs_to` association
|
|
89
89
|
to ActiveRecord. Relies on Sorbet + Tapioca.
|
|
90
90
|
test_files: []
|