No commit activity in last 3 years
No release in over 3 years
This is a gem for easy build of nested relations through options of initialize
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0

Runtime

 Project Readme

RelationBuilder

This is a gem for easy build of nested relations through options of initialize

Gem Version

Installation

Add this line to your application's Gemfile:

gem 'relation_builder'

And then execute:

$ bundle

Or install it yourself as:

$ gem install relation_builder

Usage

Examples

Old style of initialize several nested objects:

building = Building.new(params)
flat = Flat.new
room = Room.new
bathroom = Bathroom.new
flat.rooms << room
flat.bathroom = bathroom
building.flats << flat

Initializing with relation_builder:

building = Building.new(params, auto_build_relations: {flats: [:rooms, :bathroom]})

How to install?

In additions to gem install you need to

include RelationBuilder::InitializeOptions

into all models involved in nested build.

Strategy of relations build

There is two strategy: "nested" and "build". They can be specified by passing build_strategy: :build/:nested into options of initialize. Bydefault using :nested strategy.

'Nested' strategy

It use nested_attributes for build nested relations. If you have half-filled params. For example, in params you have attributes for building, flat and room. And suppose that in any case you want to have a bathroom. So you can say same thing as in previous example:

building = Building.new(params, auto_build_relations: {flat: [:room, :bathroom]})

Here you keep all attributes of nested models extracted from params. And additionally in any case for each flat you get a bathroom.

'Build' strategy

It based on simple build association. It can be useful if you doesn't want to have a deal with nested_attributes. This strategy has same syntax with auto_build_relations key, but have several restrictions.

Positive moments:

Negative moments:

  • this strategy can't build rest of nesting relation, only full chain, i.e. this strategy is incompatible with nested_attributes.

Purpose

One of aim of this gem is easy way to deal with any kind of form_builder (for example, formtastic ). If you want to make a form with several nested object you must initialize all objects before send them into the form. Otherwise you can't see any fields of nested object on the form. For many nested object it can be annoying.

Contributing

  1. Fork it ( https://github.com/Loriowar/relation_builder/fork )
  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 a new Pull Request