polymorphic_enum_type 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +12 -0
- data/gemfiles/Gemfile.rails-6.0-stable +7 -0
- data/gemfiles/Gemfile.rails-6.1-stable +7 -0
- data/gemfiles/Gemfile.rails-7.0-stable +7 -0
- data/gemfiles/Gemfile.rails-7.1-stable +7 -0
- data/lib/polymorphic_enum_type/config.rb +21 -0
- data/lib/polymorphic_enum_type/version.rb +5 -0
- data/lib/polymorphic_enum_type.rb +28 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2347f9edba997acc5f5e2587942b3d33f8498f8977beb3ba2fd9ca5d1bc1ccdb
|
4
|
+
data.tar.gz: faf15df0ff6910df88afa53af29a6a3acc1ca445183e1614d7d747de5edf9e50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 24ea74179aa5273923fd533b0173e96e8cf156c8a8509a7ebcd442620667ad52b12812e4c1e5b49a359bda6c26b8b1fc63bd44b2eccd15143fce53491acd5401
|
7
|
+
data.tar.gz: 856f966b6a8265519aa31ab0d73b3319be4e38ad588a7d5ec9d7731733e40a7cb70fe3cae64884a5c3d50d58dd60c789697c6ded69cd9446fca1ec9faa72b31a
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at oleg.b.antonyan@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Oleg Antonyan
|
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,131 @@
|
|
1
|
+
# PolymorphicEnumType
|
2
|
+
|
3
|
+
[![CI](https://github.com/olegantonyan/polymorphic_enum_type/actions/workflows/ci.yml/badge.svg)](https://github.com/olegantonyan/polymorphic_enum_type/actions/workflows/ci.yml)
|
4
|
+
|
5
|
+
Storing class name as a string for each record is bad idea performance-wise. This gem enables `ActiveRecord::Enum` as type column in polymorphic associations. Unlike https://github.com/clio/polymorphic_integer_type this gem does not monkey-patch anything. In fact, all it does is adding `enum` to your model with values as `Hash` where key is your class name and value is integer associated with this class. And that's it, literally a single line of code does the trick.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add polymorphic_enum_type
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install polymorphic_enum_type
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Extend `PolymorphicEnumType` module in a model with polymorphic `belongs_to` and use `belongs_to_polymorphic_enum_type` instead of `belongs_to`:
|
20
|
+
```ruby
|
21
|
+
class Comment < ActiveRecord::Base
|
22
|
+
extend PolymorphicEnumType
|
23
|
+
belongs_to :commentable, polymorphic: true, enum_type: true
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
The database table must have `commentable_type` integer field instead of default string:
|
28
|
+
```ruby
|
29
|
+
create_table :comments do |t|
|
30
|
+
t.bigint :commentable_id
|
31
|
+
t.bigint :commentable_type
|
32
|
+
|
33
|
+
...
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Create initializer, for example `config/initializers/polymorphic_enum_type.rb` and set the mapping integer to class name there:
|
38
|
+
```ruby
|
39
|
+
PolymorphicEnumType.configure do |config|
|
40
|
+
config.add :commentable, { 1 => 'Article', 2 => 'Post' }
|
41
|
+
config.add :imageable, { 1 => 'Comment', 2 => 'User' }
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance Avatar, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get to_i called on it and stored in the database. "Avatar".to_i == 0, so if your mapping included 0, this would create a weird bug.
|
46
|
+
|
47
|
+
## Migrating an existing association
|
48
|
+
|
49
|
+
If you want to convert a polymorphic association that is already a string, you'll need to set up a migration. (Assuming SQL for the time being, but this should be pretty straightforward.)
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class CommentsToPolymorphicEnumType < ActiveRecord::Migration
|
53
|
+
|
54
|
+
def up
|
55
|
+
change_table :comments do |t|
|
56
|
+
t.bigint :new_commentable_type
|
57
|
+
end
|
58
|
+
|
59
|
+
execute <<-SQL
|
60
|
+
UPDATE comments
|
61
|
+
SET new_commentable_type = CASE commentable_type
|
62
|
+
WHEN 'Post' THEN 2
|
63
|
+
WHEN 'Article' THEN 1
|
64
|
+
END
|
65
|
+
SQL
|
66
|
+
|
67
|
+
change_table :comments, bulk: true do |t|
|
68
|
+
t.remove :commentable_type
|
69
|
+
t.rename :new_commentable_type, :commentable_type
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def down
|
74
|
+
change_table :comments do |t|
|
75
|
+
t.string :new_commentable_type
|
76
|
+
end
|
77
|
+
|
78
|
+
execute <<-SQL
|
79
|
+
UPDATE comments
|
80
|
+
SET new_commentable_type = CASE commentable_type
|
81
|
+
WHEN 2 THEN 'Post'
|
82
|
+
WHEN 1 THEN 'Article'
|
83
|
+
END
|
84
|
+
SQL
|
85
|
+
|
86
|
+
change_table :comments, bulk: true do |t|
|
87
|
+
t.remove :commentable_type
|
88
|
+
t.rename :new_commentable_type, :commentable_type
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Lastly, you will need to be careful of any place where you are doing raw SQL queries with the string (commentable_type = 'Post'). They should use the integer instead. However, when using ActiveRecord query methods and passing attributes as symbols there you still have to use strings:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
Comment.create(text: 'good', commentable: Post.create(text: '123'))
|
98
|
+
artcile = Article.create(text: '1234')
|
99
|
+
Comment.create(text: 'good1', commentable: artcile)
|
100
|
+
Comment.create(text: 'good2', commentable: artcile)
|
101
|
+
|
102
|
+
# using where with attr: value syntax works with wtrings - ActiveRecord converts strings to enum values prior to query
|
103
|
+
assert_equal 1, Comment.where(commentable_type: 'Post').count
|
104
|
+
assert_equal 2, Comment.where(commentable_type: 'Article').count
|
105
|
+
|
106
|
+
# whenever you need to write raw SQL query you have to use integers, since ActiveRecord no longer converts strings to integers for enum in such cases
|
107
|
+
assert_equal 0, Comment.where("commentable_type = 'Post'").count
|
108
|
+
assert_equal 0, Comment.where("commentable_type = 'Article'").count
|
109
|
+
|
110
|
+
# commentable_types holds hash used for enum
|
111
|
+
assert_equal 1, Comment.where("commentable_type = ?", Comment.commentable_types['Post']).count
|
112
|
+
assert_equal 2, Comment.where("commentable_type = ?", Comment.commentable_types['Article']).count
|
113
|
+
```
|
114
|
+
|
115
|
+
## Development
|
116
|
+
|
117
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
118
|
+
|
119
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
120
|
+
|
121
|
+
## Contributing
|
122
|
+
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/olegantonyan/polymorphic_enum_type. 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/olegantonyan/polymorphic_enum_type/blob/master/CODE_OF_CONDUCT.md).
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
128
|
+
|
129
|
+
## Code of Conduct
|
130
|
+
|
131
|
+
Everyone interacting in the PolymorphicEnumType project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/olegantonyan/polymorphic_enum_type/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PolymorphicEnumType
|
4
|
+
class Config
|
5
|
+
def initialize
|
6
|
+
@h = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(attr, hash)
|
10
|
+
h[attr.to_sym] = hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def enum_hash(attr)
|
14
|
+
h.fetch(attr.to_sym).invert
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :h
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "polymorphic_enum_type/version"
|
4
|
+
require_relative "polymorphic_enum_type/config"
|
5
|
+
|
6
|
+
module PolymorphicEnumType
|
7
|
+
def belongs_to(name, scope = nil, **options)
|
8
|
+
if options.delete(:enum_type)
|
9
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
10
|
+
enum("#{name}_type", PolymorphicEnumType.config.enum_hash(name), scopes: false, instance_methods: false)
|
11
|
+
else
|
12
|
+
enum("#{name}_type".to_sym => PolymorphicEnumType.config.enum_hash(name), _scopes: false)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
super(name, scope, **options)
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def configure
|
21
|
+
yield(config)
|
22
|
+
end
|
23
|
+
|
24
|
+
def config
|
25
|
+
@config ||= PolymorphicEnumType::Config.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polymorphic_enum_type
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oleg Antonyan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-11-22 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: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
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: minitest
|
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: sqlite3
|
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
|
+
description: Does the same as https://github.com/clio/polymorphic_integer_type but
|
70
|
+
without monkey-patching, using ActiveRecord::Enum instead.
|
71
|
+
email:
|
72
|
+
- oleg.b.antonyan@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- CHANGELOG.md
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- gemfiles/Gemfile.rails-6.0-stable
|
83
|
+
- gemfiles/Gemfile.rails-6.1-stable
|
84
|
+
- gemfiles/Gemfile.rails-7.0-stable
|
85
|
+
- gemfiles/Gemfile.rails-7.1-stable
|
86
|
+
- lib/polymorphic_enum_type.rb
|
87
|
+
- lib/polymorphic_enum_type/config.rb
|
88
|
+
- lib/polymorphic_enum_type/version.rb
|
89
|
+
homepage: https://github.com/olegantonyan/polymorphic_enum_type
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata:
|
93
|
+
homepage_uri: https://github.com/olegantonyan/polymorphic_enum_type
|
94
|
+
source_code_uri: https://github.com/olegantonyan/polymorphic_enum_type
|
95
|
+
changelog_uri: https://github.com/olegantonyan/polymorphic_enum_type/CHANGELOG.md
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 2.6.0
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.4.10
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Allows integer database field for polymorphic association type instead of
|
115
|
+
string via enum.
|
116
|
+
test_files: []
|