activerecord-polymorph 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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +10 -0
- data/activerecord-polymorph.gemspec +26 -0
- data/lib/polymorph.rb +5 -0
- data/lib/polymorph/methods.rb +47 -0
- data/lib/polymorph/relation.rb +33 -0
- data/lib/polymorph/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 52c2f6317a218c26936a2cc6b637b992cfddd239
|
4
|
+
data.tar.gz: dd2c70fc9f51e69f4bdf8813a910a6879913c5a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1d004e0abd6224692cb789738c6c8fcb5552ab901bbc4c9423453c1db5aa8d559d775aac75945073dcdfae38be4a4e6637a53ae241e3917f7fde5822fd83ba57
|
7
|
+
data.tar.gz: 9a0a30ca6bcfd69db7bac4eecb2ca3970b6e7f9e48c65dff9edc9bb1017c0efc68e8652de7d51689b2f97a445c208e266d8a3ac81896f79c84eca214b27a01e0
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at james.kiesel@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 James Kiesel
|
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,123 @@
|
|
1
|
+
# Polymorph
|
2
|
+
|
3
|
+
Polymorph provides a dead simple interface for providing some limited functionality for polymorphic has_many :through relations. Why would we use this?
|
4
|
+
|
5
|
+
Say we have a schema like this:
|
6
|
+
```ruby
|
7
|
+
class Discussion
|
8
|
+
has_many :comments
|
9
|
+
end
|
10
|
+
|
11
|
+
class Comment
|
12
|
+
belongs_to :discussion
|
13
|
+
belongs_to :participant, polymorphic: true
|
14
|
+
end
|
15
|
+
|
16
|
+
class User
|
17
|
+
belongs_to :comment, as: :participant
|
18
|
+
end
|
19
|
+
|
20
|
+
class Robot
|
21
|
+
belongs_to :comment, as: :participant
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
Sure would be cool here to call `discussion.participants`, and get back an `ActiveRecord::Relation` that we could play with, which had both Users and Robots in it. But, if we try this in rails:
|
26
|
+
|
27
|
+
```
|
28
|
+
ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association
|
29
|
+
```
|
30
|
+
(ಠ_ಠ)
|
31
|
+
|
32
|
+
But with Polymorph, we can write a line of code like this:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
polymorph :participants, through: :comments, source_types: [:users, :robots], fields: [:id, :name]
|
36
|
+
```
|
37
|
+
|
38
|
+
And get a polymorphic `ActiveRecord::Relation` back:
|
39
|
+
|
40
|
+
```
|
41
|
+
#<ActiveRecord::AssociationRelation [
|
42
|
+
#<User id: 1, name: "Fry">,
|
43
|
+
#<User id: 2, name: "Leela">,
|
44
|
+
#<Robot id: 1, name: "Bender">
|
45
|
+
]>
|
46
|
+
```
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'activerecord-polymorph'
|
54
|
+
```
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install polymorph
|
63
|
+
|
64
|
+
## Usage
|
65
|
+
|
66
|
+
Polymorph does one thing and one thing only, and that is define the `polymorph` method on `ActiveRecord::Base`.
|
67
|
+
|
68
|
+
Available options:
|
69
|
+
|
70
|
+
- **through**: (required) The name of the relation you'd like to construct
|
71
|
+
- **source_types**: (required) A list of classes that could be returned by this polymorphic relationship
|
72
|
+
- **fields**: (optional, default: [:id]) A list of fields the polymorphic classes have in common (these are also the fields which you can do 'where' queries on)
|
73
|
+
- **through_class**: (optional) The name of the class the relationship reaches through. Is inferred by `through` by default.
|
74
|
+
- **source_column**: (optional) The name of the polymorphic association columns (ie, this is the 'participant' in `participant_type` and `participant_id`). Inferred from the relation name by default.
|
75
|
+
|
76
|
+
So, as a default, we could write
|
77
|
+
```ruby
|
78
|
+
polymorph :participants, through: :comments, source_types: [:users, :robots]
|
79
|
+
```
|
80
|
+
|
81
|
+
Which would work perfectly if we're reaching for Users and Robots through Comments, and Users and Robots don't share any field names.
|
82
|
+
|
83
|
+
If Users and Robots share a 'name' field, we write:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
polymorph :participants, through: :comments, source_types: [:users, :robots], fields: [:id, :name]
|
87
|
+
```
|
88
|
+
|
89
|
+
If we want to call these 'participants', but our database columns are already set to 'commenter_id' and 'commenter_type', we can invoke the `source_column` method:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
polymorph :commenters, through: :comments, source_types: [:users, :robots], source_column: :commenter
|
93
|
+
```
|
94
|
+
|
95
|
+
If the ruby class cannot be inferred by the 'through' option, we can point it to the right place with `through_class`:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
polymorph :commenters, through: :comments, source_types: [:users, :robots], through_class: Comments::Base
|
99
|
+
```
|
100
|
+
|
101
|
+
NB that this relation has somewhat limited support for further querying! Currently, we support count, pluck, and simple where clauses on common keys:
|
102
|
+
|
103
|
+
```
|
104
|
+
discussion.participants.count # => 3
|
105
|
+
discussion.participants.pluck(:name) # => ["Fry, Leela", "Bender"]
|
106
|
+
discussion.participants.where(name: "Bender") # => #<ActiveRecord::Relation [ #<Robot id: 1, name: "Bender" ]>
|
107
|
+
```
|
108
|
+
|
109
|
+
Using additional things like joins or where clauses for columns that aren't shared by the source tables will probably end in pain. I am open to extending this in the future, but it works for what I need it for for now. :D
|
110
|
+
|
111
|
+
I built this so that I could use it in [Loomio](https://github.com/loomio/loomio). Please use it if you think it's helpful; and I'm happy to hear about any troubles you come across.
|
112
|
+
|
113
|
+
## Development
|
114
|
+
|
115
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gdpelican/activerecord-polymorph. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'polymorph/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-polymorph"
|
8
|
+
spec.version = Polymorph::VERSION
|
9
|
+
spec.authors = ["James Kiesel"]
|
10
|
+
spec.email = ["james.kiesel@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Allows polymorphic loading of has_many through objects"
|
13
|
+
spec.homepage = "https://www.github.com/gdpelican/activerecord-polymorph"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activerecord", "~> 4.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
end
|
data/lib/polymorph.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "polymorph/relation"
|
2
|
+
require "byebug"
|
3
|
+
|
4
|
+
module Polymorph
|
5
|
+
module Methods
|
6
|
+
def polymorph(method_name,
|
7
|
+
through:,
|
8
|
+
source_types:,
|
9
|
+
fields: [:id],
|
10
|
+
through_class: through.to_s.singularize.camelize.constantize,
|
11
|
+
source_column: method_name.to_s.singularize,
|
12
|
+
source_type: "#{source_column}_type")
|
13
|
+
|
14
|
+
define_method method_name, -> {
|
15
|
+
|
16
|
+
query = send(through).select([
|
17
|
+
source_types.map { |t| "#{t}.*" },
|
18
|
+
source_types.product(fields).map { |a| "#{a[0]}.#{a[1]} AS #{a[0].to_s.singularize}_#{a[1]}" },
|
19
|
+
"#{through}.#{source_column}_type",
|
20
|
+
"'true'::boolean as polymorph_query"
|
21
|
+
].flatten.join(', '))
|
22
|
+
|
23
|
+
source_types.each do |type|
|
24
|
+
query = query.joins(%{
|
25
|
+
LEFT OUTER JOIN #{type}
|
26
|
+
ON #{type}.id = #{through}.#{source_column}_id
|
27
|
+
AND '#{type.to_s.singularize.camelize}' = #{through}.#{source_type}
|
28
|
+
})
|
29
|
+
end
|
30
|
+
|
31
|
+
Polymorph::Relation.new(query, fields: fields, source_types: source_types)
|
32
|
+
}
|
33
|
+
|
34
|
+
through_class.define_singleton_method :instantiate, ->(attrs, column_types) {
|
35
|
+
super(attrs, column_types).tap do |record|
|
36
|
+
transfer_fields = fields.map { |field| [field, attrs["#{attrs[source_type].downcase}_#{field}"]] }.to_h
|
37
|
+
record.assign_attributes(transfer_fields)
|
38
|
+
end
|
39
|
+
}
|
40
|
+
|
41
|
+
through_class.define_singleton_method :discriminate_class_for_record, ->(attributes) {
|
42
|
+
return super(attributes) unless attributes['polymorph_query'].present?
|
43
|
+
attributes["#{source_column}_type"].camelize.constantize
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Polymorph
|
2
|
+
class Relation < SimpleDelegator
|
3
|
+
|
4
|
+
def initialize(query, source_types:, fields:)
|
5
|
+
@source_types, @fields = source_types, fields
|
6
|
+
super(query)
|
7
|
+
end
|
8
|
+
|
9
|
+
def count
|
10
|
+
self.to_a.length
|
11
|
+
end
|
12
|
+
|
13
|
+
def pluck(field)
|
14
|
+
self.to_a.map(&field)
|
15
|
+
end
|
16
|
+
|
17
|
+
def where(hash = {})
|
18
|
+
fields = hash.slice(*@fields)
|
19
|
+
fields.map do |field|
|
20
|
+
%{(
|
21
|
+
#{@source_types.map { |type| "#{type}.#{field[0]} = :#{field[0]}" }.join(" OR ")}
|
22
|
+
)}
|
23
|
+
end.join(" AND ")
|
24
|
+
rebuild(super(clause, fields))
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def rebuild(query)
|
30
|
+
self.class.new(query, source_types: @source_types, fields: @fields)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-polymorph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Kiesel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-08 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: '4.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- james.kiesel@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- activerecord-polymorph.gemspec
|
84
|
+
- lib/polymorph.rb
|
85
|
+
- lib/polymorph/methods.rb
|
86
|
+
- lib/polymorph/relation.rb
|
87
|
+
- lib/polymorph/version.rb
|
88
|
+
homepage: https://www.github.com/gdpelican/activerecord-polymorph
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Allows polymorphic loading of has_many through objects
|
112
|
+
test_files: []
|