Skip to content

Rails Engine to allow ActiveRecord associations be set up in the DB instead of being hard-coded.

License

Notifications You must be signed in to change notification settings

Alexander-Senko/adjustable_schema

Repository files navigation

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.

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:

AdjustableSchema::Relationship.create! source_type: 'Person',
                                       target_type: 'Book'

Or use a helper method:

AdjustableSchema::Relationship.seed! Person => Book

Now you have:

person.books
book.people

Roles

You can create multiple role-based associations between two models.

AdjustableSchema::Relationship.seed! Person => Book, roles: %w[author editor]

You will get:

person.books
person.authored_books
person.edited_books

book.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:

AdjustableSchema::Relationship.seed! User => Book, roles: %w[author editor]
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:

AdjustableSchema::Relationship.seed! Person, roles: %w[friend]

In this case you'll get these associations:

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:

AdjustableSchema::Engine.configure do
  config.names[:associations][:source][:self] = :effect
  config.names[:associations][:target][:self] = :cause
end

Thus, for hierarchical Events, you'll get:

event.causes
event.effects

Installation

Add this line to your application's Gemfile:

gem "adjustable_schema"

And then execute:

bundle

Or install it yourself as:

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.

About

Rails Engine to allow ActiveRecord associations be set up in the DB instead of being hard-coded.

Resources

License

Stars

Watchers

Forks

Packages

No packages published