active_record_associations_module_name 0.1.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: b05545848d718d6257c4cec922cae8d0a527d87f57d4a85d9a016ed1ff3b81f7
4
+ data.tar.gz: bd5192641fc352ae5667b48441cf291c851bdb2468db53a09f6a8781df4ebece
5
+ SHA512:
6
+ metadata.gz: 4bd1f0a9292ace7ab88660ba5a2efeb1841a31fe98a80e1d577a7a6cd192a1d2dad55403021425e560fde0207a44b9f05988c5d83cbf1ca6a66b21cf4847deb2
7
+ data.tar.gz: b4b7bfb0559594efe9f06d14d29224e22b4a13e6675257792a3ab570f6f2e6b6f12d6c5ba4ebb27953ecdbaac44d40dde025e204c2ec8e189a825c3a32a1adc1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Rafe Rosen
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,47 @@
1
+ # ActiveRecordAssociationsModuleName
2
+ When working with ActiveRecord models within modules, retyping the module path within association definitions can become of repetive and noisy. This extension DRYs things up by adding a `module_name` option to ActiveRecord association definitions.
3
+
4
+ ## Usage
5
+ Specify a string `module_name` for any ActiveRecord association (belongs_to, has_many, has_one, or has_and_belongs_to_many)
6
+
7
+ For example:
8
+
9
+ ```ruby
10
+ class Post
11
+ has_many :comments, module_name: "Blog"
12
+
13
+ # this replaces:
14
+ # has_many :comments, class_name: "Blog::Comment"
15
+ end
16
+
17
+ ```
18
+
19
+ Models often reference other models in the same module. For this you can use the shortcut `module_name: true`.
20
+
21
+ ```ruby
22
+ class Blog::Post
23
+ has_many :comments, module_name: true
24
+
25
+ # this replaces:
26
+ # has_many :comments, class_name: "Blog::Comment"
27
+ end
28
+
29
+ ```
30
+
31
+ ## Installation
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'active_record_associations_module_name'
36
+ ```
37
+
38
+ And then execute:
39
+ ```bash
40
+ $ bundle
41
+ ```
42
+
43
+ ## Contributing
44
+ Issues and pull requests are welcome. Please be respectful.
45
+
46
+ ## License
47
+ 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,14 @@
1
+ require "bundler/setup"
2
+ puts ENV["DATABASE_URL"]
3
+
4
+ require "bundler/gem_tasks"
5
+
6
+ require "rake/testtask"
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = false
12
+ end
13
+
14
+ task default: :test
@@ -0,0 +1,73 @@
1
+ require "active_record_associations_module_name/version"
2
+
3
+ # When working with ActiveRecord models within modules, retyping the module
4
+ # path within association definitions becomes repetitive. These extensions add a
5
+ # module_name option to AR-associations. You can specify a string that will
6
+ # be prepended to the definition's class_name option, or you can just say
7
+ # true to use the caller's module.
8
+ #
9
+ # Example:
10
+ #
11
+ # class Blog::Post
12
+ # has_many :comments, module_name: true
13
+ #
14
+ # # this replaces:
15
+ # # has_many :comments, class_name: "Blog::Comment"
16
+ # end
17
+ #
18
+ module ActiveRecordAssociationsModuleName
19
+ module AssociationClassMethodExtension
20
+ def belongs_to(name, scope = nil, **options)
21
+ options = process_module_name_option(name, options)
22
+ super(name, scope, **options)
23
+ end
24
+
25
+ def has_one(name, scope = nil, **options)
26
+ options = process_module_name_option(name, options)
27
+ super(name, scope, **options)
28
+ end
29
+
30
+ def has_many(name, scope = nil, **options)
31
+ options = process_module_name_option(name, options)
32
+ super(name, scope, **options)
33
+ end
34
+
35
+ def has_and_belongs_to_many(name, scope = nil, **options)
36
+ options = process_module_name_option(name, options)
37
+ super(name, scope, **options)
38
+ end
39
+
40
+ private def process_module_name_option name, options
41
+ if options[:class_name] && options[:module_name]
42
+ raise ArgumentError, "Options `module_name` and `class_name` are incompatible. Please specify only one."
43
+ elsif !options[:module_name]
44
+ return options
45
+ end
46
+
47
+ module_name = options.delete(:module_name)
48
+ class_name = name.to_s.singularize.camelize
49
+ case module_name
50
+ when true
51
+ module_prefix = to_s.split("::")[0..-2].join("::")
52
+ when String
53
+ module_prefix = module_name
54
+ else
55
+ raise ArgumentError, "Option module_name must be either a String or `true`."
56
+ end
57
+
58
+ options[:class_name] = [module_prefix, class_name].reject(&:blank?).join("::")
59
+ options
60
+ end
61
+ end
62
+ ActiveRecord::Associations::ClassMethods.prepend(ActiveRecordAssociationsModuleName::AssociationClassMethodExtension)
63
+
64
+ module ActiveRecordAssociationsModuleName::BuilderExtension
65
+ def valid_options(options)
66
+ super.push(:module_name)
67
+ end
68
+ end
69
+ ActiveRecord::Associations::Builder::Association.singleton_class.prepend(ActiveRecordAssociationsModuleName::BuilderExtension)
70
+ end
71
+
72
+
73
+
@@ -0,0 +1,3 @@
1
+ module ActiveRecordAssociationsModuleName
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_associations_module_name
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafe Rosen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-31 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.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '5.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '7'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '5.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '7'
53
+ description:
54
+ email:
55
+ - rafe@existentialmutt.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - MIT-LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - lib/active_record_associations_module_name.rb
64
+ - lib/active_record_associations_module_name/version.rb
65
+ homepage: http://github.com/existentialmutt/active_record_associations_module_name
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: http://github.com/existentialmutt/active_record_associations_module_name
70
+ source_code_uri: http://github.com/existentialmutt/active_record_associations_module_name
71
+ changelog_uri: http://github.com/existentialmutt/active_record_associations_module_name/releases
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.1.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Provides a `module_name` option for ActiveRecord Associations
91
+ test_files: []