Skip to content

Latest commit

 

History

History
120 lines (85 loc) · 2.77 KB

README.md

File metadata and controls

120 lines (85 loc) · 2.77 KB

NestedRecord

This gem is for mapping of json fields on ActiveModel objects!

Installation

Add this line to your application's Gemfile:

gem 'nested_record'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nested_record

Usage

Use nested_record to define nested associations on ActiveRecord models via JSON attributes.

First add json column into your database:

change_table :users do |t|
  t.json :profile
end

Then define association using has_one_nested macro:

class User < ActiveRecord::Base
  include NestedRecord::Macro

  has_one_nested :profile
end

Or you can include the Macro globally:

class ApplicationRecord < ActiveRecord::Base
  include NestedRecord::Macro
end

class User < ApplicationRecord
  has_one_nested :profile
end

Define nested record attributes using ActiveModel::Attributes API (since Rails 5.2):

class Profile < NestedRecord::Base
  attribute :age,    :integer
  attribute :active, :boolean
  has_one_nested :contacts
end

You can go deeper and define models on the next nesting level:

class Profile::Contacts < NestedRecord::Base
  attribute :email, :string
  attribute :phone, :string
end

You can store a collection of objects with has_many_nested:

class Profile::Contacts < NestedRecord::Base
  attribute :email, :string
  attribute :phone, :string
  has_many_nested :socials
end

class Profile::Social < NestedRecord::Base
  attribute :name
  attribute :url
end

user.profile.age = 39
user.profile.contacts.email = '[email protected]'
user.profile.contacts.socials[0].name # => 'facebook'

You can assign attributes in the way like accepts_nested_attributes_for macros provides for AR models:

user.profile_attributes = {
  age: 39,
  contacts_attributes: {
    email: '[email protected]',
    socials_attributes: [
      { name: 'facebook', url: 'facebook.example.com/johndoe' },
      { name: 'twitter', url: 'twitter.example.com/johndoe' }
    ]
  }
}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marshall-lee/nested_record.

License

The gem is available as open source under the terms of the MIT License.