All Projects → DmitryTsepelev → Store_model

DmitryTsepelev / Store_model

Licence: mit
Work with JSON-backed attributes as ActiveRecord-ish models

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Store model

Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+57.07%)
Mutual labels:  json, activerecord, rails
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (-53.41%)
Mutual labels:  json, activerecord, rails
Clowne
A flexible gem for cloning models
Stars: ✭ 260 (-36.59%)
Mutual labels:  activerecord, rails
Public activity
Easy activity tracking for models - similar to Github's Public Activity
Stars: ✭ 2,822 (+588.29%)
Mutual labels:  activerecord, rails
Ar lazy preload
Lazy loading associations for the ActiveRecord models
Stars: ✭ 281 (-31.46%)
Mutual labels:  activerecord, rails
Datoji
A tiny JSON storage service. Create, Read, Update, Delete and Search JSON data.
Stars: ✭ 222 (-45.85%)
Mutual labels:  json, rails
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: ✭ 242 (-40.98%)
Mutual labels:  json, rails
Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (-32.93%)
Mutual labels:  activerecord, rails
Coloquent
Javascript/Typescript library mapping objects and their interrelations to JSON API, with a clean, fluent ActiveRecord-like (e.g. similar to Laravel's Eloquent) syntax for creating, retrieving, updating and deleting model objects.
Stars: ✭ 149 (-63.66%)
Mutual labels:  json, activerecord
Algoliasearch Rails
AlgoliaSearch integration to your favorite ORM
Stars: ✭ 352 (-14.15%)
Mutual labels:  activerecord, rails
Html5 validators
A gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation
Stars: ✭ 302 (-26.34%)
Mutual labels:  activerecord, rails
Isolator
Detect non-atomic interactions within DB transactions
Stars: ✭ 362 (-11.71%)
Mutual labels:  activerecord, rails
Rabl Rails
Rails 4.2 & 5 templating system with JSON, XML and Plist support.
Stars: ✭ 200 (-51.22%)
Mutual labels:  json, rails
Oj
Optimized JSON
Stars: ✭ 2,824 (+588.78%)
Mutual labels:  json, rails
Flexirest
Flexirest - The really flexible REST API client for Ruby
Stars: ✭ 188 (-54.15%)
Mutual labels:  json, rails
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+606.34%)
Mutual labels:  activerecord, rails
Second level cache
Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4, 5 and 6.
Stars: ✭ 380 (-7.32%)
Mutual labels:  activerecord, rails
Pager Api
Easy API pagination for Rails
Stars: ✭ 86 (-79.02%)
Mutual labels:  json, rails
Sabisu Rails
Simple and powerful engine for exploring your Rails api application
Stars: ✭ 129 (-68.54%)
Mutual labels:  json, rails
Pg party
ActiveRecord PostgreSQL Partitioning
Stars: ✭ 294 (-28.29%)
Mutual labels:  activerecord, rails

StoreModel Gem Version Coverage Status

StoreModel gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes.

  • 💪 Powered with Attributes API. You can use a number of familiar types or write your own
  • 🔧 Works like ActiveModel. Validations, enums and nested attributes work very similar to APIs provided by Rails
  • 1️⃣ Follows single responsibility principle. Keep the logic around the data stored in a JSON column separated from the model
  • 👷‍♂️ Born in production.
class Configuration
  include StoreModel::Model

  attribute :model, :string
  enum :status, %i[active archived], default: :active

  validates :model, :status, presence: true
end

class Product < ApplicationRecord
  attribute :configuration, Configuration.to_type
end

Sponsored by Evil Martians

Why should I wrap my JSON columns?

Imagine that you have a model Product with a jsonb column called configuration. This is how you likely gonna work with this column:

product = Product.find(params[:id])
if product.configuration["model"] == "spaceship"
  product.configuration["color"] = "red"
end
product.save

This approach works fine when you don't have a lot of keys with logic around them and just read the data. However, when you start working with that data more intensively–you may find the code a bit verbose and error-prone.

For instance, try to find a way to validate :model value to be required. Despite of the fact, that you'll have to write this validation by hand, it violates single-repsponsibility principle: why parent model (Product) should know about the logic related to a child (Configuration)?

📖 Read more about the motivation in the Wrapping JSON-based ActiveRecord attributes with classes post

Getting started

Start with creating a class for representing the hash as an object:

class Configuration
  include StoreModel::Model

  attribute :model, :string
  attribute :color, :string
end

Attributes should be defined using Rails Attributes API. There is a number of types available out of the box, and you can always extend the type system.

Register the field in the ActiveRecord model class:

class Product < ApplicationRecord
  attribute :configuration, Configuration.to_type
end

When you're done, the initial snippet could be rewritten in the following way:

product = Product.find(params[:id])
if product.configuration.model == "spaceship"
  product.configuration.color = "red"
end
product.save

Documentation

  1. Installation
  2. StoreModel::Model API:
  1. Array of stored models
  2. One of
  3. Alternatives
  4. Defining custom types

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].