Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexander-senko/adjustable_schema
Rails Engine to allow ActiveRecord associations be set up in the DB instead of being hard-coded.
https://github.com/alexander-senko/adjustable_schema
Last synced: 4 months ago
JSON representation
Rails Engine to allow ActiveRecord associations be set up in the DB instead of being hard-coded.
- Host: GitHub
- URL: https://github.com/alexander-senko/adjustable_schema
- Owner: Alexander-Senko
- License: mit
- Created: 2023-12-20T13:24:22.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-28T22:57:01.000Z (8 months ago)
- Last Synced: 2024-09-30T06:39:04.882Z (4 months ago)
- Language: Ruby
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# Adjustable Schema for Rails
Define your model associations in the database without changing the schema or models.
This Rails Engine was renamed and refactored from [Rails Dynamic Associations](https://github.com/Alexander-Senko/rails_dynamic_associations).
## Features
* Creates associations for your models when application starts.
* Provides `Relationship` & `Relationship::Role` models.
* No configuration code needed.
* No code generated or inserted to your app (except migrations).
* Adds some useful methods to `ActiveRecord` models to handle their relationships.## Usage
Add configuration records to the DB:
``` ruby
AdjustableSchema::Relationship.create! source_type: 'Person',
target_type: 'Book'
```Or use a helper method:
``` ruby
AdjustableSchema::Relationship.seed! Person => Book
```Now you have:
``` ruby
person.books
book.people
```### Roles
You can create multiple role-based associations between two models.
``` ruby
AdjustableSchema::Relationship.seed! Person => Book, roles: %w[author editor]
```You will get:
``` ruby
person.books
person.authored_books
person.edited_booksbook.people
book.author_people
book.editor_people
```#### Special cases
##### "Actor-like" models
In case you have set up relationships with `User` model you'll get a slightly different naming:
``` ruby
AdjustableSchema::Relationship.seed! User => Book, roles: %w[author editor]
`````` ruby
book.users
book.authors
book.editors
```The list of models to be handled this way can be set with `actor_model_names` configuration parameter.
It includes `User` by default.##### Self-referencing models
You may want to set up recursive relationships:
``` ruby
AdjustableSchema::Relationship.seed! Person, roles: %w[friend]
```In this case you'll get these associations:
``` ruby
person.parents
person.children # for all the children
person.people # for "roleless" children, not friends
person.friends
person.friended_people
```If you prefer a different naming over `parents` & `children`, you can configure it like this:
```ruby
AdjustableSchema::Engine.configure do
config.names[:associations][:source][:self] = :effect
config.names[:associations][:target][:self] = :cause
end
```Thus, for hierarchical `Event`s, you'll get:
``` ruby
event.causes
event.effects
```## Installation
Add this line to your application's Gemfile:
```ruby
gem "adjustable_schema"
```And then execute:
```bash
bundle
```Or install it yourself as:
```bash
gem install adjustable_schema
```## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).