
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
activerecord-deepstore
Advanced tools
ActiveRecord-Deepstore is a Ruby gem that extends ActiveRecord models with additional functionality for handling deeply nested data structures within a database column. It simplifies storing, accessing, and managing complex nested data in your Rails applications.
Add this line to your application's Gemfile:
gem 'activerecord-deepstore', require: 'active_record/deepstore'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install activerecord-deepstore
To use ActiveRecord-Deepstore in your Rails application, include it in your ActiveRecord models:
class MyModel < ActiveRecord::Base
extend ActiveRecord::Deepstore
end
Once included, your models gain access to methods for storing and accessing deeply nested data within database columns.
class User < ActiveRecord::Base
extend ActiveRecord::Deepstore
deep_store :settings, {
notifications: {
posts: { push: true, email: true },
comments: { push: true, email: false }
}
}
end
This implementation provides with an accessor for every level of the provider hash:
user.notifications_settings # => { posts: { push: true, email: true }, comments: { push: true, email: false } }
user.posts_notifications_settings # => { push: true, email: true }
user.push_posts_notifications_settings # => true
user.email_posts_notifications_settings # => true
user.comments_notifications_settings # => { push: true, email: false }
user.push_comments_notifications_settings # => true
user.email_comments_notifications_settings # => false
You can list all the generated accessors by calling deep_stored_accessors
on the model User
:
User.deep_stored_accessors
# => ["notifications_settings", "posts_notifications_settings", "push_posts_notifications_settings", [...], "email_comments_notifications_settings"]
Writer methods automatically cast the value to the type the default values belong to. For example:
user.push_comments_notifications_settings = "1" # => "1"
user.push_comments_notifications_settings # => true
user.push_comments_notifications_settings = "0" # => "0"
user.push_comments_notifications_settings # => false
Dirty attributes are implemented for every accessor. For example:
user.push_comments_notifications_settings # => false
user.push_comments_notifications_settings = true # => true
user.push_comments_notifications_settings_was # => false
user.push_comments_notifications_settings_changes # => { false => true }
user.push_comments_notifications_settings_changed? # => true
You can access the default value of every accessor at anytime by calling the associated default_#{accessor_name}
method:
user.push_comments_notifications_settings # => false
user.update! push_comments_notifications_settings: true
user.push_comments_notifications_settings # => true
user.default_push_comments_notifications_settings #=> false
You can reset every accessor to its default value at anytime by calling the associated reset_#{accessor_name}
method:
# When the changes are not persisted
user.push_comments_notifications_settings # => false
user.push_comments_notifications_settings = true
user.reset_push_comments_notifications_settings
user.push_comments_notifications_settings # => false
# When the changes are persisted
user.update! push_comments_notifications_settings: true
user.push_comments_notifications_settings # => true
user.reset_push_comments_notifications_settings!
user.reload.push_comments_notifications_settings # => false
Bug reports and pull requests are welcome on GitHub at https://github.com/EmCousin/activerecord-deepstore.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that activerecord-deepstore demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.