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: '07971d74aa507ba0e738c433c955150723fbbe547f3ff009f8b8707a7479c122'
4
- data.tar.gz: 3b5467dbac0e1c8111599a56a5f17b53325274173b25ff500af9966fb3e3eb75
3
+ metadata.gz: b1931a69a1ca4d6916e27182f40285e34d7a6d84d55796ff19c35faa0d91c425
4
+ data.tar.gz: cb2778380cf8c93d0846356562dc98a0d47411c13d686c41561517da208ebaef
5
5
  SHA512:
6
- metadata.gz: 51e164ba36955d17eed4b4d7af629544fcc378d6640485dc6b5b2ae42dff44288b9888aa622fe9798272ab776f3e53a3a505c60336158d76d6cc8bf0bc9a1fbe
7
- data.tar.gz: f24e08c20a3bd2e1a5832aa8916878f993d5f64c10819b294f7815f52bce6e339ae52a94671cb8c9bc94d8416501b449472593db7588043934bf0ef4602bf8bc
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
- polymorphically_belongs_to :commentable, types: [Post, Photo]
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.
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module PolymorphicBelongsTo
5
- VERSION = "0.0.0"
5
+ VERSION = "0.2.0"
6
6
  end
@@ -10,15 +10,15 @@ module PolymorphicBelongsTo
10
10
  extend ActiveSupport::Concern
11
11
 
12
12
  included do
13
- class_attribute :defined_polymorphic_belongs_tos, instance_writer: false
13
+ class_attribute :polymorphic_belongs_to_types, instance_writer: false
14
14
  end
15
15
 
16
16
  module ClassMethods
17
- def polymorphically_belongs_to(name, types:, validate_types: true, **options)
17
+ def polymorphic_belongs_to(name, types:, validate_types: true, **options)
18
18
  name = name.to_sym
19
19
 
20
- self.defined_polymorphic_belongs_tos ||= {}
21
- defined_polymorphic_belongs_tos[name.to_s] = types
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
- validates(foreign_type, inclusion: {in: types.map(&:name)}, allow_nil: !presence_required)
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?(:defined_polymorphic_belongs_tos)
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).defined_polymorphic_belongs_tos
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 `polymorphically_belongs_to` association to ActiveRecord. Relies on Sorbet + Tapioca."
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.0.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 `polymorphically_belongs_to` association
88
+ summary: PolymorphicBelongsTo provides a type-safe `polymorphic_belongs_to` association
89
89
  to ActiveRecord. Relies on Sorbet + Tapioca.
90
90
  test_files: []