safe_polymorphic 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +10 -8
- data/README.md +19 -0
- data/lib/safe_polymorphic/associations.rb +3 -7
- data/lib/safe_polymorphic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0aaf728e17cf09be9b04b2a3465b43e089b3daeb0c61c69530f7a40cde085037
|
4
|
+
data.tar.gz: dcadfc2a11171a2aad8d269b672297606a05175de1200562b003219d2f1bb39e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21ead7903d65fd2cccf30325cd47069702ce44f385d949e45fc0f0d598f313db35d68026127e36433fb270b4d934703289294ba0118767e519151a28620bc473
|
7
|
+
data.tar.gz: 4b9cc16deef36d7b430c936a74f79562c0983a32ae500f4c65d6075f289b9d454305e2217fa49fe8b8ba2712cb393e4e128f342dc471cf0b18aa03000908166e
|
data/CHANGELOG.md
CHANGED
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.1)
|
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,4 +1,8 @@
|
|
1
1
|
# SafePolymorphic
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/safe_polymorphic.svg)](https://badge.fury.io/rb/safe_polymorphic)
|
3
|
+
[![Ruby](https://github.com/gogrow-dev/safe_polymorphic/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/gogrow-dev/safe_polymorphic/actions/workflows/main.yml)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/1b0a4b92bbee2be50dc1/maintainability)](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/maintainability)
|
5
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/1b0a4b92bbee2be50dc1/test_coverage)](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/test_coverage)
|
2
6
|
|
3
7
|
An ActiveRecord extension which allows us to safely use polymorphic associations, by validating which classes are allowed to be related to, while also adding some helper methods.
|
4
8
|
|
@@ -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
|
@@ -82,3 +94,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
82
94
|
## Code of Conduct
|
83
95
|
|
84
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)
|
@@ -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
|
|