Code Monkey home page Code Monkey logo

adjustable_schema's Introduction

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.

adjustable_schema's People

Contributors

alexander-senko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.