https://github.com/existentialmutt/active_record_associations_module_name
Adds a `module_name` option to ActiveRecord associations
https://github.com/existentialmutt/active_record_associations_module_name
Last synced: over 1 year ago
JSON representation
Adds a `module_name` option to ActiveRecord associations
- Host: GitHub
- URL: https://github.com/existentialmutt/active_record_associations_module_name
- Owner: existentialmutt
- License: mit
- Created: 2021-05-31T15:17:03.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-06-02T03:23:14.000Z (about 5 years ago)
- Last Synced: 2025-04-14T09:22:06.995Z (over 1 year ago)
- Language: Ruby
- Size: 20.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ActiveRecordAssociationsModuleName
When working with ActiveRecord models within modules, retyping the module path within association definitions can become repetive and noisy. This extension makes things a little more DRY by adding a `module_name` option.
## Usage
Specify a string `module_name` for any ActiveRecord association (belongs_to, has_many, has_one, or has_and_belongs_to_many)
For example:
```ruby
class Post
has_many :comments, module_name: "Blog"
# this replaces:
# has_many :comments, class_name: "Blog::Comment"
end
```
Models often reference other models in the same module. For this you can use the shortcut `module_name: true`.
```ruby
class Blog::Post
has_many :comments, module_name: true
# this replaces:
# has_many :comments, class_name: "Blog::Comment"
end
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'active_record_associations_module_name'
```
And then execute:
```bash
$ bundle
```
## Contributing
Issues and pull requests are welcome. Please be respectful.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).