safe_polymorphic 0.1.1 → 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/CHANGELOG.md +5 -1
- data/Gemfile.lock +3 -2
- data/README.md +35 -33
- data/lib/safe_polymorphic/version.rb +1 -1
- data/lib/safe_polymorphic.rb +4 -2
- data/logo.png +0 -0
- data/safe_polymorphic.gemspec +5 -3
- metadata +12 -8
- /data/{config → lib/safe_polymorphic}/locales/en.yml +0 -0
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/CHANGELOG.md
CHANGED
|
@@ -7,4 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
## [0.1.1] - 2022-12-28
|
|
9
9
|
|
|
10
|
-
- Fix finder method bug when searching using an instance of a class
|
|
10
|
+
- Fix finder method bug when searching using an instance of a class
|
|
11
|
+
|
|
12
|
+
## [0.1.2] - 2022-12-28
|
|
13
|
+
|
|
14
|
+
- Fix issue that forced to declare the I18n locale (`class_not_allowed`) in each project using this gem instead of using the default locale included in the gem
|
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/[USERNAME]/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/lib/safe_polymorphic.rb
CHANGED
|
@@ -4,11 +4,13 @@ require_relative 'safe_polymorphic/version'
|
|
|
4
4
|
require 'active_record'
|
|
5
5
|
require_relative 'safe_polymorphic/associations'
|
|
6
6
|
|
|
7
|
-
I18n.load_path << File.expand_path('config/locales/en.yml')
|
|
8
|
-
|
|
9
7
|
module SafePolymorphic
|
|
10
8
|
class Error < StandardError; end
|
|
11
9
|
end
|
|
12
10
|
|
|
11
|
+
ActiveSupport.on_load(:i18n) do
|
|
12
|
+
I18n.load_path << File.expand_path('safe_polymorphic/locales/en.yml', __dir__)
|
|
13
|
+
end
|
|
14
|
+
|
|
13
15
|
# Extend ActiveRecord::Base with belongs to polymorphic associations
|
|
14
16
|
ActiveRecord::Base.include SafePolymorphic::Associations
|
data/logo.png
ADDED
|
Binary file
|
data/safe_polymorphic.gemspec
CHANGED
|
@@ -9,14 +9,16 @@ Gem::Specification.new do |spec|
|
|
|
9
9
|
spec.authors = ['Nicolas Erlichman']
|
|
10
10
|
spec.email = ['nicolas@gogrow.dev']
|
|
11
11
|
|
|
12
|
-
spec.summary = '
|
|
13
|
-
'
|
|
12
|
+
spec.summary = 'Safely use polymorphic associations by validating which classes are allowed to be related to.'
|
|
13
|
+
spec.description = 'ActiveRecord extension which allows us to safely use polymorphic associations, by validating' \
|
|
14
|
+
'which classes are allowed to be related to, while also adding scopes and helper methods.'
|
|
14
15
|
spec.homepage = 'https://github.com/gogrow-dev/safe_polymorphic'
|
|
15
16
|
spec.required_ruby_version = '>= 2.6.0'
|
|
16
17
|
|
|
17
18
|
spec.metadata['homepage_uri'] = spec.homepage
|
|
18
19
|
spec.metadata['source_code_uri'] = spec.homepage
|
|
19
20
|
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
|
|
21
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
20
22
|
|
|
21
23
|
# Specify which files should be added to the gem when it is released.
|
|
22
24
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
@@ -29,5 +31,5 @@ Gem::Specification.new do |spec|
|
|
|
29
31
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
30
32
|
spec.require_paths = ['lib']
|
|
31
33
|
|
|
32
|
-
spec.add_dependency 'activerecord', '>= 5.2', '<
|
|
34
|
+
spec.add_dependency 'activerecord', '>= 5.2', '< 8.0'
|
|
33
35
|
end
|
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,8 +29,10 @@ dependencies:
|
|
|
29
29
|
version: '5.2'
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
33
|
-
description:
|
|
32
|
+
version: '8.0'
|
|
33
|
+
description: ActiveRecord extension which allows us to safely use polymorphic associations,
|
|
34
|
+
by validatingwhich classes are allowed to be related to, while also adding scopes
|
|
35
|
+
and helper methods.
|
|
34
36
|
email:
|
|
35
37
|
- nicolas@gogrow.dev
|
|
36
38
|
executables: []
|
|
@@ -46,10 +48,11 @@ files:
|
|
|
46
48
|
- LICENSE.md
|
|
47
49
|
- README.md
|
|
48
50
|
- Rakefile
|
|
49
|
-
- config/locales/en.yml
|
|
50
51
|
- lib/safe_polymorphic.rb
|
|
51
52
|
- lib/safe_polymorphic/associations.rb
|
|
53
|
+
- lib/safe_polymorphic/locales/en.yml
|
|
52
54
|
- lib/safe_polymorphic/version.rb
|
|
55
|
+
- logo.png
|
|
53
56
|
- safe_polymorphic.gemspec
|
|
54
57
|
homepage: https://github.com/gogrow-dev/safe_polymorphic
|
|
55
58
|
licenses:
|
|
@@ -58,6 +61,7 @@ metadata:
|
|
|
58
61
|
homepage_uri: https://github.com/gogrow-dev/safe_polymorphic
|
|
59
62
|
source_code_uri: https://github.com/gogrow-dev/safe_polymorphic
|
|
60
63
|
changelog_uri: https://github.com/gogrow-dev/safe_polymorphic/blob/master/CHANGELOG.md
|
|
64
|
+
rubygems_mfa_required: 'true'
|
|
61
65
|
post_install_message:
|
|
62
66
|
rdoc_options: []
|
|
63
67
|
require_paths:
|
|
@@ -76,6 +80,6 @@ requirements: []
|
|
|
76
80
|
rubygems_version: 3.3.7
|
|
77
81
|
signing_key:
|
|
78
82
|
specification_version: 4
|
|
79
|
-
summary:
|
|
80
|
-
|
|
83
|
+
summary: Safely use polymorphic associations by validating which classes are allowed
|
|
84
|
+
to be related to.
|
|
81
85
|
test_files: []
|
|
File without changes
|