New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

adjustable_schema

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adjustable_schema

  • 0.8.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 08 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc