polymorphic_aliases 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d96cf37a68602af11b72c0de1f86d336cf7e3e1f195b55ae5a21885dbe18989
4
+ data.tar.gz: 60b19a2810a6eaf70022c6acae62849405e5506bdca6581e04ce11f462f2dbbc
5
+ SHA512:
6
+ metadata.gz: 15e7e4eb46cf0b0858ee7be71e0e9c74df973cfa4dbcdb81e7d90046f0846726e6c3d1ab01685d1a2a2ef46551ea9917907db7b37c7f431ec3491ab38ba12fbf
7
+ data.tar.gz: b475361f2f228e2df591ee830479f305b30e8d22742dfe208c5a898778be614b729f07234d581cf456efa70329485a0f1d46b18fe17321244630f6cf20933d3d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 mansakondo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # polymorphic_aliases
2
+ An ActiveRecord extension to use aliases for your polymorphic associations.
3
+
4
+ ## Usage
5
+ Your model code:
6
+ ```ruby
7
+ # app/models/comment.rb
8
+ class Comment < ApplicationRecord
9
+ belongs_to :commentable, polymorphic: true, types: %w( Post Picture )
10
+ end
11
+
12
+ # app/models/post.rb
13
+ class Post < ApplicationRecord
14
+ has_many :comments, as: :commentable
15
+ end
16
+
17
+ # app/models/picture.rb
18
+ class Picture < ApplicationRecord
19
+ has_many :comments, as: :commentable
20
+ end
21
+ ```
22
+ In the console:
23
+ ```ruby
24
+ post = Post.create(title: "Polymorphic Aliases", content: "...")
25
+ picture = Picture.create(legend: "Here's my code.", url: "https://avatars.githubusercontent.com/u/47113995?v=4")
26
+
27
+ comment_from_post = Comment.create(content: "...", commentable: post)
28
+ comment_from_picture = Comment.create(content: "...", commentable: picture)
29
+
30
+ comment_from_post.post.title
31
+ # => "Polymorphic Aliases"
32
+
33
+ comment_from_picture.picture.legend
34
+ # => "Here's my code."
35
+
36
+ comment_from_post.picture
37
+ # => nil
38
+
39
+ comment_from_picture.post
40
+ # => nil
41
+ ```
42
+
43
+ ## Installation
44
+ Add this line to your application's Gemfile:
45
+
46
+ ```ruby
47
+ gem "polymorphic_aliases"
48
+ ```
49
+
50
+ And then execute:
51
+ ```bash
52
+ $ bundle
53
+ ```
54
+
55
+ Or install it yourself as:
56
+ ```bash
57
+ $ gem install polymorphic_aliases
58
+ ```
59
+
60
+ ## Contributing
61
+ Contribution directions go here.
62
+
63
+ ## License
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PolymorphicAttributeAliases
4
+ VALID_PREFIXES = %w( reload_ ).freeze
5
+ VALID_SUFFIXES = %w( = ).freeze
6
+
7
+ private_constant :VALID_PREFIXES, :VALID_SUFFIXES
8
+
9
+ def method_missing(method, *args, &block)
10
+ prefix, attribute_name, suffix = decompose_method_name(method)
11
+
12
+ self.class.reflect_on_all_associations(:belongs_to).each do |reflection|
13
+ begin
14
+ next unless (types = reflection.options[:types])
15
+ next unless (target_type = attributes["#{reflection.name}_type"])
16
+ next unless types.any? { |type| type.to_s.constantize == target_type.constantize }
17
+ next unless target_type.constantize == attribute_name.camelize.constantize
18
+
19
+ association_method_name = prefix + reflection.name.to_s + suffix
20
+ return send(:"#{association_method_name}", *args, &block)
21
+ rescue
22
+ return super
23
+ end
24
+ end
25
+
26
+ super
27
+ end
28
+
29
+ private
30
+
31
+ def decompose_method_name(method_name)
32
+ method_name = method_name.to_s
33
+
34
+ prefix = ""
35
+ affix = method_name
36
+ suffix = ""
37
+
38
+ VALID_PREFIXES.each do |valid_prefix|
39
+ next unless method_name.start_with?(valid_prefix)
40
+ prefix = valid_prefix
41
+ affix = method_name.delete_prefix(valid_prefix)
42
+
43
+ break
44
+ end
45
+
46
+ VALID_SUFFIXES.each do |valid_suffix|
47
+ next unless method_name.ends_with?(valid_suffix)
48
+ suffix = valid_suffix
49
+ affix = affix.delete_suffix(valid_suffix)
50
+
51
+ break
52
+ end
53
+
54
+ return prefix, affix, suffix
55
+ end
56
+ end
57
+
58
+ ActiveModel::AttributeMethods.prepend(PolymorphicAttributeAliases)
@@ -0,0 +1,52 @@
1
+ module BelongsToPolymorphicAssociation
2
+ extend ActiveSupport::Concern
3
+
4
+ class_methods do
5
+ private
6
+
7
+ def valid_options(options)
8
+ valid = super
9
+ valid += [:types] if options[:polymorphic]
10
+ valid
11
+ end
12
+
13
+ def define_accessors(model, reflection)
14
+ super
15
+
16
+ mixin = model.generated_association_methods
17
+
18
+ if reflection.polymorphic? && reflection.options[:types].present?
19
+ define_aliases(mixin, reflection)
20
+ end
21
+ end
22
+
23
+ def define_aliases(mixin, reflection)
24
+ types = reflection.options[:types]
25
+ association_name = reflection.name
26
+
27
+ types.each do |type|
28
+ name = type.tableize.tr("/", "_").singularize
29
+ type = type.inspect
30
+
31
+ mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
32
+ def #{name}
33
+ if #{type} == self["#{association_name}_type"]
34
+ association(:#{association_name}).reader
35
+ end
36
+ end
37
+ def #{name}=(value)
38
+ if #{type} == self["#{association_name}_type"]
39
+ association(:#{association_name}).writer(value)
40
+ end
41
+ end
42
+ def reload_#{name}
43
+ if #{type} == self["#{association_name}_type"]
44
+ association(:#{association_name}).force_reload_reader
45
+ end
46
+ end
47
+ CODE
48
+ end
49
+ end
50
+ end
51
+ end
52
+ ActiveRecord::Associations::Builder::BelongsTo.prepend(BelongsToPolymorphicAssociation)
@@ -0,0 +1,4 @@
1
+ module PolymorphicAliases
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module PolymorphicAliases
2
+ VERSION = '0.2.0'
3
+ end
@@ -0,0 +1,6 @@
1
+ require "polymorphic_aliases/version"
2
+ require "polymorphic_aliases/railtie"
3
+
4
+ module PolymorphicAliases
5
+ require "polymorphic_aliases/active_record/associations/builder/belongs_to_polymorphic_association.rb"
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :polymorphic_aliases do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polymorphic_aliases
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - mansakondo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.3
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.3.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.3
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.3.1
33
+ description:
34
+ email:
35
+ - mansakondo22@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/polymorphic_aliases.rb
44
+ - lib/polymorphic_aliases/active_model/attribute_methods/polymorphic_attribute_aliases.rb
45
+ - lib/polymorphic_aliases/active_record/associations/builder/belongs_to_polymorphic_association.rb
46
+ - lib/polymorphic_aliases/railtie.rb
47
+ - lib/polymorphic_aliases/version.rb
48
+ - lib/tasks/polymorphic_aliases_tasks.rake
49
+ homepage: https://github.com/mansakondo/polymorphic_aliases
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/mansakondo/polymorphic_aliases
54
+ source_code_uri: https://github.com/mansakondo/polymorphic_aliases
55
+ changelog_uri: https://github.com/mansakondo/polymorphic_aliases/blob/main/CHANGELOG.md
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.2.15
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: An ActiveRecord extension to use aliases for your polymorphic associations.
75
+ test_files: []