polymorphic_belongs_to 0.0.0 → 0.1.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: 290d53bdaab6800f56b0edf83ea87a690baf62ca3a271780b6825c95528dd05f
4
+ data.tar.gz: bfa1cc5b60ede65a5d744447858dff62830ceef4fe33c8f3d82758792cb725c3
5
5
  SHA512:
6
- metadata.gz: 51e164ba36955d17eed4b4d7af629544fcc378d6640485dc6b5b2ae42dff44288b9888aa622fe9798272ab776f3e53a3a505c60336158d76d6cc8bf0bc9a1fbe
7
- data.tar.gz: f24e08c20a3bd2e1a5832aa8916878f993d5f64c10819b294f7815f52bce6e339ae52a94671cb8c9bc94d8416501b449472593db7588043934bf0ef4602bf8bc
6
+ metadata.gz: d871be11f602c84d9be9a4aa4bf1c5f16bf47f8b79150ff88e878cb433eb1d8a5825baff9d9e19e7256788029925e8a0d28a9b6e30b63cf6f422d7e371100e55
7
+ data.tar.gz: 14139961b813f91033316cc9ea3eaaf1220b28bd8afc4cfdadd145a84285489c4799a0908e14a37c53a5ab3ae7de320ecf3f80507cc11be86348fa6a88188245
data/README.md CHANGED
@@ -17,11 +17,32 @@ class Comment < ApplicationRecord
17
17
  include PolymorphicBelongsTo
18
18
 
19
19
  polymorphically_belongs_to :commentable, types: [Post, Photo]
20
+
21
+ # Disable the "type inclusion" validator
22
+ polymorphically_belongs_to :commentable, types: [Post, Photo], validate_types: false
23
+
24
+ # Customize the "type inclusion" validator
25
+ polymorphically_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
+ `polymorphically_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.1.0"
6
6
  end
@@ -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
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.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Ahn