safe_polymorphic 0.1.2 → 0.1.3
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 +4 -4
- data/Gemfile.lock +3 -2
- data/README.md +35 -33
- data/lib/safe_polymorphic/version.rb +1 -1
- data/logo.png +0 -0
- data/safe_polymorphic.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2343d183e1afc06a3571606a2631753284c943eb1079fdd09498e8153b4e254
|
4
|
+
data.tar.gz: a6f105af6c4ac6ac1484f63339fc5bb71429a3f947bfc9a96b10ea7fce92893f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91a5dc3f1d3195a6ebf3fdc1a6c34a6c021a7685a46ac631859b9fcb9971864e374e50c9b6ba61941891346b47171ffa2d9d42d5ac5a0cfa39a6ec50cf4f6dce
|
7
|
+
data.tar.gz: ea87992a43a22e74d264112b5db178ccebd353d5ba18cd59ac8c21796904af6f69df23c90700656812ac1e389d0364af29bd46d56c9f8701b868a5c77616bea6
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
safe_polymorphic (0.1.
|
5
|
-
activerecord (>= 5.2, <
|
4
|
+
safe_polymorphic (0.1.3)
|
5
|
+
activerecord (>= 5.2, < 8.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -76,6 +76,7 @@ GEM
|
|
76
76
|
|
77
77
|
PLATFORMS
|
78
78
|
arm64-darwin-21
|
79
|
+
arm64-darwin-23
|
79
80
|
x86_64-linux
|
80
81
|
|
81
82
|
DEPENDENCIES
|
data/README.md
CHANGED
@@ -1,14 +1,27 @@
|
|
1
|
-
|
2
|
-
[](https://badge.fury.io/rb/safe_polymorphic)
|
1
|
+
[](https://badge.fury.io/rb/safe_polymorphic.svg)
|
3
2
|
[](https://github.com/gogrow-dev/safe_polymorphic/actions/workflows/main.yml)
|
4
3
|
[](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/maintainability)
|
5
4
|
[](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/test_coverage)
|
6
5
|
|
7
|
-
|
6
|
+
# SafePolymorphic
|
7
|
+
|
8
|
+
> The best way to keep your polymorphic relationships safe.
|
9
|
+
|
10
|
+
<img align="right" width="150" title="SafePolymorphic logo" src="./logo.png">
|
11
|
+
|
12
|
+
An ActiveRecord extension for polymorphic associations.
|
13
|
+
|
14
|
+
* **Simple.** It is as easy to use as it is to update.
|
15
|
+
* **Safe.** It helps you avoid unintended usage of polymorphic classes.
|
16
|
+
* **Powerful.** It packages some helpful scopes and helper methods.
|
17
|
+
|
18
|
+
<a href="https://gogrow.dev/?utm_source=github&utm_content=safe_polymorphic">
|
19
|
+
<img src="https://www.gogrow.dev/_next/static/media/gogrow-logo.96254aba.svg" alt="Built by GoGrow" width="230"></a>
|
8
20
|
|
9
|
-
The base idea was taken from this blogpost: [Improve ActiveRecord Polymorphic Associations - Head of engineering at Product Hunt](https://blog.rstankov.com/allowed-class-names-in-activerecord-polymorphic-associations/).
|
10
21
|
|
11
|
-
|
22
|
+
> Credits: [Improve ActiveRecord Polymorphic Associations](https://blog.rstankov.com/allowed-class-names-in-activerecord-polymorphic-associations/).
|
23
|
+
|
24
|
+
## Install
|
12
25
|
|
13
26
|
Install the gem and add to the application's Gemfile by executing:
|
14
27
|
|
@@ -23,23 +36,23 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
23
36
|
In your model you can do the following:
|
24
37
|
|
25
38
|
```ruby
|
26
|
-
class
|
27
|
-
belongs_to :
|
39
|
+
class Address < ActiveRecord::Base
|
40
|
+
belongs_to :addressabel, polymorphic: [User, Company]
|
28
41
|
end
|
29
42
|
```
|
30
43
|
|
31
44
|
Define a `belongs_to` relatinoship and instead of just adding the option `polymorphic: true`, we are able to specify the allowed polymorphic classes.
|
32
45
|
|
33
|
-
By using this, we will create a polymorphic relationship in `
|
34
|
-
If we try to set an `
|
46
|
+
By using this, we will create a polymorphic relationship in `Address` called `addressable` which can only be a `User` or a `Company`.
|
47
|
+
If we try to set an `addressable` from a class rather than the aforementioend ones, it will return the following error:
|
35
48
|
```ruby
|
36
|
-
#ActiveRecord::RecordInvalid: Validation failed:
|
49
|
+
#ActiveRecord::RecordInvalid: Validation failed: Addressable type OtherThing class is not an allowed class.
|
37
50
|
```
|
38
51
|
|
39
52
|
We can also use strings and symbols instead of the classes themselves:
|
40
53
|
```ruby
|
41
|
-
class
|
42
|
-
belongs_to :
|
54
|
+
class Address < ActiveRecord::Base
|
55
|
+
belongs_to :addressable, polymorphic: [:user, 'Company']
|
43
56
|
end
|
44
57
|
```
|
45
58
|
Provided that the strings and symbols translate to existing classes when used with `.classify.constantize`.
|
@@ -47,27 +60,27 @@ Provided that the strings and symbols translate to existing classes when used wi
|
|
47
60
|
### Usage with namespaced models
|
48
61
|
|
49
62
|
```ruby
|
50
|
-
class
|
51
|
-
belongs_to :
|
63
|
+
class Address < ActiveRecord::Base
|
64
|
+
belongs_to :addressable, polymorphic: [Company::User, Company]
|
52
65
|
end
|
53
66
|
```
|
54
67
|
|
55
68
|
### Helper methods
|
56
69
|
|
57
70
|
Class methods:
|
58
|
-
- `
|
59
|
-
- `
|
60
|
-
- `
|
71
|
+
- `Address.addressable_types`: returns the allowed classes
|
72
|
+
- `Address.with_addressable(#{type})`: generic finder method
|
73
|
+
- `Address.with_addressable_#{allowed_class_name}`: scope for each allowed class
|
61
74
|
|
62
75
|
Instance methods:
|
63
|
-
- `
|
76
|
+
- `Address.addressable_type_#{allowed_class_name}?`: check if it is from that specific class
|
64
77
|
|
65
78
|
### Helper methods with namespaced models
|
66
79
|
|
67
80
|
Class methods:
|
68
|
-
- `
|
69
|
-
- `
|
70
|
-
- `
|
81
|
+
- `Address.with_addressable(Company::User)`
|
82
|
+
- `Address.with_addressable_company_user`
|
83
|
+
- `Address.addressable_type_company_user?`
|
71
84
|
|
72
85
|
## I18n
|
73
86
|
|
@@ -89,15 +102,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
89
102
|
|
90
103
|
## Contributing
|
91
104
|
|
92
|
-
Bug reports and pull requests are welcome on GitHub
|
93
|
-
|
94
|
-
## Code of Conduct
|
95
|
-
|
96
|
-
Everyone interacting in the SafePolymorphic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gogrow-dev/safe_polymorphic/blob/main/CODE_OF_CONDUCT.md).
|
97
|
-
|
98
|
-
## Credits
|
99
|
-
|
100
|
-
belongs_to_polymorphic is maintained by [GoGrow](https://gogrow.dev) with the help of our
|
101
|
-
[contributors](https://github.com/gogrow-dev/belongs_to_polymorphic/contributors).
|
102
|
-
|
103
|
-
[<img src="https://user-images.githubusercontent.com/9309458/180014465-00477428-fd76-48f6-b984-5b401b8ce241.svg" height="50"/>](https://gogrow.dev)
|
105
|
+
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](./CODE_OF_CONDUCT.md).
|
data/logo.png
ADDED
Binary file
|
data/safe_polymorphic.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_polymorphic
|
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
|
- Nicolas Erlichman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '5.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '8.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '5.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '8.0'
|
33
33
|
description: ActiveRecord extension which allows us to safely use polymorphic associations,
|
34
34
|
by validatingwhich classes are allowed to be related to, while also adding scopes
|
35
35
|
and helper methods.
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/safe_polymorphic/associations.rb
|
53
53
|
- lib/safe_polymorphic/locales/en.yml
|
54
54
|
- lib/safe_polymorphic/version.rb
|
55
|
+
- logo.png
|
55
56
|
- safe_polymorphic.gemspec
|
56
57
|
homepage: https://github.com/gogrow-dev/safe_polymorphic
|
57
58
|
licenses:
|