belongs_to_polymorphic 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 12a886ce1619050030e07557b2fa3c005b9a26ce8b0d9738b21cfa62c7a505fe
4
+ data.tar.gz: 7714ae16a60d00ddb42e22233f7bcbf8853902f7a6e0aab7f920d8a1f898e898
5
+ SHA512:
6
+ metadata.gz: 2394b5c7083898163b35f2fa5850c7fa1a5b7ecd80567a1e9855aee1ff72f660aebe83836a0e270cbac169a437953e96621d4cb8852e212a7590e6e8454df453
7
+ data.tar.gz: 6c9f165512366489fc10dd436c277c48e8fe971cac8ae3befa87c6479f647bdab0074775ab2d738251b97913d2c11c81e2fa024ea8d6008cb2c6c5404559018d
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 GoGrow
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Belongs to Polymorphic
2
+
3
+ An ActiveRecord extension defining the concept *belongs to polymorphic*, which allows us to use polymorphic associations (belongs_to) and restrict which classes are allowed to be related to.
4
+
5
+
6
+ The base idea and code 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/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'belongs_to_polymorphic'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install belongs_to_polymorphic
23
+
24
+ ## Usage
25
+
26
+ In your model you can do the following:
27
+
28
+ ```ruby
29
+ class Book < ActiveRecord::Base
30
+ belongs_to_polymorphic :owner, allowed_classes: [Person, Publisher]
31
+ end
32
+ ```
33
+
34
+ You can also add any options that a regular `belongs_to` with `polymorphic: true` could use.
35
+
36
+ By using this you create a polymorphic relationship in `Book` called `contents` which can be a `Person` or a `Publisher`.
37
+ If you try to set an `owner` from a class rather than the aforementioend ones, it will return the following error:
38
+ ```ruby
39
+ #<ActiveModel::Error attribute=profile_type, type=inclusion, options={:value=>"IncorrectModel"}>
40
+ ```
41
+
42
+ It also automatically adds some helpers
43
+
44
+ Class:
45
+ - `Book.owner_types`: returns the allowed classes
46
+ - `Book.with_owner(#{type})`: generic finder method
47
+ - `Book.with_owner_#{allowed_class_name}`: scope for each allowed class
48
+
49
+ Instance:
50
+ - `book.owner_type_#{allowed_class_name}?`: check if it is from that specific class
51
+
52
+ ### Usage with namespaced models
53
+
54
+ ```ruby
55
+ class Book < ActiveRecord::Base
56
+ belongs_to_polymorphic :owner, allowed_classes: [Publisher::Person, Publisher]
57
+ end
58
+ ```
59
+
60
+ It will allow you to use:
61
+ - `Book.with_owner(Publisher::Person)`
62
+ - `Book.with_owner_publisher_person`
63
+ - `book.owner_type_publisher_person?`
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ module BelongsToPolymorphic
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'belongs_to_polymorphic/version'
2
+ require 'active_support/concern'
3
+
4
+ module BelongsToPolymorphic
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ # rubocop:disable Metrics/AbcSize
9
+ def belongs_to_polymorphic(name, allowed_classes:, **options)
10
+ allowed_classes = Array.new(allowed_classes)
11
+ belongs_to name, polymorphic: true, **options
12
+ classes = allowed_classes.map(&:name)
13
+ classes << nil if options[:optional]
14
+ validates "#{name}_type", inclusion: { in: classes }
15
+ define_singleton_method("#{name}_types") { allowed_classes }
16
+
17
+ # generates a generic finder method
18
+ define_singleton_method("with_#{name}") do |type|
19
+ type = case type
20
+ when Class then type.name
21
+ when String then type
22
+ else type.class.name
23
+ end
24
+ where("#{name}_type" => type)
25
+ end
26
+
27
+ allowed_classes.each do |model|
28
+ model_name = model.name
29
+ model_name_sanitized = model_name.underscore.tr('/', '_')
30
+ # generates scope for each allowed class
31
+ scope "with_#{name}_#{model_name_sanitized}",
32
+ -> { where("#{name}_type" => model_name) }
33
+
34
+ # generates instance method for each allowed class to check if type is that class
35
+ define_method("#{name}_type_#{model_name_sanitized}?") do
36
+ public_send("#{name}_type") == model_name
37
+ end
38
+ end
39
+ end
40
+ # rubocop:enable Metrics/AbcSize
41
+ end
42
+ end
43
+
44
+ ActiveRecord::Base.include BelongsToPolymorphic
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: belongs_to_polymorphic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Erlichman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: reek
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rootstrap
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.17.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.17.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '5.2'
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '7.1'
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '5.2'
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: '7.1'
117
+ description: ActiveRecord extension - Polymorphic associations with restricted allowed
118
+ classes.
119
+ email:
120
+ - hello@gogrow.dev
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - LICENSE.txt
126
+ - README.md
127
+ - lib/belongs_to_polymorphic.rb
128
+ - lib/belongs_to_polymorphic/version.rb
129
+ homepage: https://github.com/gogrow-dev/belongs_to_polymorphic
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ source_code_uri: https://github.com/gogrow-dev/belongs_to_polymorphic
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubygems_version: 3.1.4
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: ActiveRecord extension - Polymorphic associations with restricted allowed
153
+ classes.
154
+ test_files: []