safe_polymorphic 0.1.0 → 0.1.2
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 +8 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +10 -8
- data/README.md +22 -3
- data/lib/safe_polymorphic/associations.rb +3 -7
- data/{config → lib/safe_polymorphic}/locales/en.yml +0 -0
- data/lib/safe_polymorphic/version.rb +1 -1
- data/lib/safe_polymorphic.rb +4 -2
- data/safe_polymorphic.gemspec +4 -2
- metadata +8 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 164e389f0104c35deef618728e27279f0f1efb563398cec61c4c4c6611972f1b
|
|
4
|
+
data.tar.gz: 9ee365c0242b60164baac2f00f338334f99c73b96e661eea11ffd3f60aa225d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c9f0e945aeabcf77b7cf35b7b491df63dc97e86fa0d30500d31b2424880bb42543b0310f8f1840e8a9dbab57088292670e36097e7e3c2be7f0b32959299e3074
|
|
7
|
+
data.tar.gz: 1c64d72005b5c64007a00286bca4ca9bab89753efff0636c33d5b1e103af5f3d5465ec777d87033eabf8372a38372a2973b7b57c6353f8d252d374eb64bab345
|
data/CHANGELOG.md
CHANGED
|
@@ -4,3 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
- Initial release
|
|
6
6
|
- Moved from [Belongs to Polymorphic](https://rubygems.org/gems/belongs_to_polymorphic)
|
|
7
|
+
|
|
8
|
+
## [0.1.1] - 2022-12-28
|
|
9
|
+
|
|
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
CHANGED
|
@@ -5,9 +5,9 @@ source 'https://rubygems.org'
|
|
|
5
5
|
# Specify your gem's dependencies in safe_polymorphic.gemspec
|
|
6
6
|
gemspec
|
|
7
7
|
|
|
8
|
-
gem 'database_cleaner-active_record', '~> 1.8.0', require: false
|
|
8
|
+
gem 'database_cleaner-active_record', '~> 1.8.0', require: false, group: :test
|
|
9
9
|
gem 'rake', '~> 13.0'
|
|
10
10
|
gem 'rspec', '~> 3.0'
|
|
11
11
|
gem 'rubocop', '~> 1.21'
|
|
12
|
-
gem 'simplecov', '~> 0.17.1'
|
|
13
|
-
gem 'sqlite3', '~> 1.4.2', require: false
|
|
12
|
+
gem 'simplecov', '~> 0.17.1', require: false, group: :test
|
|
13
|
+
gem 'sqlite3', '~> 1.4.2', require: false, group: :test
|
data/Gemfile.lock
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
safe_polymorphic (0.1.
|
|
4
|
+
safe_polymorphic (0.1.2)
|
|
5
5
|
activerecord (>= 5.2, < 7.1)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
activemodel (
|
|
11
|
-
activesupport (=
|
|
12
|
-
activerecord (
|
|
13
|
-
activemodel (=
|
|
14
|
-
activesupport (=
|
|
15
|
-
activesupport (
|
|
10
|
+
activemodel (6.1.4.1)
|
|
11
|
+
activesupport (= 6.1.4.1)
|
|
12
|
+
activerecord (6.1.4.1)
|
|
13
|
+
activemodel (= 6.1.4.1)
|
|
14
|
+
activesupport (= 6.1.4.1)
|
|
15
|
+
activesupport (6.1.4.1)
|
|
16
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
17
17
|
i18n (>= 1.6, < 2)
|
|
18
18
|
minitest (>= 5.1)
|
|
19
19
|
tzinfo (~> 2.0)
|
|
20
|
+
zeitwerk (~> 2.3)
|
|
20
21
|
ast (2.4.2)
|
|
21
22
|
concurrent-ruby (1.1.10)
|
|
22
23
|
database_cleaner (1.8.5)
|
|
@@ -71,6 +72,7 @@ GEM
|
|
|
71
72
|
tzinfo (2.0.5)
|
|
72
73
|
concurrent-ruby (~> 1.0)
|
|
73
74
|
unicode-display_width (2.3.0)
|
|
75
|
+
zeitwerk (2.6.0)
|
|
74
76
|
|
|
75
77
|
PLATFORMS
|
|
76
78
|
arm64-darwin-21
|
|
@@ -86,4 +88,4 @@ DEPENDENCIES
|
|
|
86
88
|
sqlite3 (~> 1.4.2)
|
|
87
89
|
|
|
88
90
|
BUNDLED WITH
|
|
89
|
-
2.
|
|
91
|
+
2.4.1
|
data/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# SafePolymorphic
|
|
2
|
+
[](https://badge.fury.io/rb/safe_polymorphic)
|
|
3
|
+
[](https://github.com/gogrow-dev/safe_polymorphic/actions/workflows/main.yml)
|
|
4
|
+
[](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/maintainability)
|
|
5
|
+
[](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/test_coverage)
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
ActiveRecord extension which allows us to safely use polymorphic associations, by validating which classes are allowed to be related to, while also adding scopes and helper methods.
|
|
4
8
|
|
|
5
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/).
|
|
6
10
|
|
|
@@ -32,6 +36,14 @@ If we try to set an `owner` from a class rather than the aforementioend ones, it
|
|
|
32
36
|
#ActiveRecord::RecordInvalid: Validation failed: Owner type OtherThing class is not an allowed class.
|
|
33
37
|
```
|
|
34
38
|
|
|
39
|
+
We can also use strings and symbols instead of the classes themselves:
|
|
40
|
+
```ruby
|
|
41
|
+
class Book < ActiveRecord::Base
|
|
42
|
+
belongs_to :owner, polymorphic: [:user, 'Publisher']
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
Provided that the strings and symbols translate to existing classes when used with `.classify.constantize`.
|
|
46
|
+
|
|
35
47
|
### Usage with namespaced models
|
|
36
48
|
|
|
37
49
|
```ruby
|
|
@@ -77,8 +89,15 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
77
89
|
|
|
78
90
|
## Contributing
|
|
79
91
|
|
|
80
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
92
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gogrow-dev/safe_polymorphic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/gogrow-dev/safe_polymorphic/blob/main/CODE_OF_CONDUCT.md).
|
|
81
93
|
|
|
82
94
|
## Code of Conduct
|
|
83
95
|
|
|
84
|
-
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/
|
|
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)
|
|
@@ -49,7 +49,7 @@ module SafePolymorphic
|
|
|
49
49
|
const_class = case klass
|
|
50
50
|
when String then klass
|
|
51
51
|
when Symbol then klass.to_s
|
|
52
|
-
else
|
|
52
|
+
else klass.class.name
|
|
53
53
|
end
|
|
54
54
|
const_class.classify.constantize
|
|
55
55
|
end
|
|
@@ -63,12 +63,8 @@ module SafePolymorphic
|
|
|
63
63
|
# Generates a generic finder method
|
|
64
64
|
def define_generic_finder_method(name)
|
|
65
65
|
define_singleton_method("with_#{name}") do |type|
|
|
66
|
-
type =
|
|
67
|
-
|
|
68
|
-
when String then type
|
|
69
|
-
else type.class.name
|
|
70
|
-
end
|
|
71
|
-
where("#{name}_type" => type)
|
|
66
|
+
type = constantize(type) unless type.is_a?(Class)
|
|
67
|
+
where("#{name}_type" => type.name)
|
|
72
68
|
end
|
|
73
69
|
end
|
|
74
70
|
|
|
File without changes
|
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/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.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nicolas Erlichman
|
|
@@ -30,7 +30,9 @@ dependencies:
|
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
32
|
version: '7.1'
|
|
33
|
-
description:
|
|
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,9 +48,9 @@ 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
|
|
53
55
|
- safe_polymorphic.gemspec
|
|
54
56
|
homepage: https://github.com/gogrow-dev/safe_polymorphic
|
|
@@ -58,6 +60,7 @@ metadata:
|
|
|
58
60
|
homepage_uri: https://github.com/gogrow-dev/safe_polymorphic
|
|
59
61
|
source_code_uri: https://github.com/gogrow-dev/safe_polymorphic
|
|
60
62
|
changelog_uri: https://github.com/gogrow-dev/safe_polymorphic/blob/master/CHANGELOG.md
|
|
63
|
+
rubygems_mfa_required: 'true'
|
|
61
64
|
post_install_message:
|
|
62
65
|
rdoc_options: []
|
|
63
66
|
require_paths:
|
|
@@ -76,6 +79,6 @@ requirements: []
|
|
|
76
79
|
rubygems_version: 3.3.7
|
|
77
80
|
signing_key:
|
|
78
81
|
specification_version: 4
|
|
79
|
-
summary:
|
|
80
|
-
|
|
82
|
+
summary: Safely use polymorphic associations by validating which classes are allowed
|
|
83
|
+
to be related to.
|
|
81
84
|
test_files: []
|