base on Work with JSON-backed attributes as ActiveRecord-ish models # StoreModel [![Gem Version](https://badge.fury.io/rb/store_model.svg)](https://rubygems.org/gems/store_model) ![](https://ruby-gem-downloads-badge.herokuapp.com/store_model?type=total) **StoreModel** gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes. - 💪 **Powered with [Attributes API](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html)**. 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**. ```ruby 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 ``` You can support my open–source work [here](https://boosty.to/dmitry_tsepelev). ## 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: ```ruby 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 the single-responsibility 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](https://evilmartians.com/chronicles/wrapping-json-based-active-record-attributes-with-classes) post ## Getting started Start with creating a class for representing the hash as an object: ```ruby class Configuration include StoreModel::Model attribute :model, :string attribute :color, :string end ``` Attributes should be defined using [Rails Attributes API](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html). 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: ```ruby class Product < ApplicationRecord attribute :configuration, Configuration.to_type end ``` When you're done, the initial snippet could be rewritten in the following way: ```ruby product = Product.find(params[:id]) if product.configuration.model == "spaceship" product.configuration.color = "red" end product.save ``` _Usage note: Rails and assigning Arrays/Hashes to records_ - Assigned attributes must be a String, Hash, Array of Hashes, or StoreModel. For example, if the attributes are coming from a controller, be sure to convert any ActionController::Parameters as needed. - Any changes made to a StoreModel instance requires the attribute be flagged as dirty, either by reassignment (`self.my_stored_models = my_stored_models.map(&:as_json)`) or by `will_change!` (`self.my_stored_models_will_change!`) - Mixing `StoreModel::NestedAttributes` into your model will allow you to use `accepts_nested_attributes_for` in the same way as ActiveRecord. ```ruby class Supplier < ActiveRecord::Base include StoreModel::NestedAttributes has_many :bicycles, dependent: :destroy attribute :products, Product.to_array_type accepts_nested_attributes_for :bicycles, :products, allow_destroy: true end ``` This will allow the form builders to work their magic: ```erb <%= form_with model: @supplier do |form| %> <%= form.fields_for :products do |product_fields| %> <%= product_fields.text_field :name %> <% end %> <% end %> ``` Resulting in: ```html <input type="text" name="supplier[products_attributes][0][name]" id="supplier_products_attributes_0_name"> ``` In the controller: ```ruby def create @supplier = Supplier.from_value(supplier_params) @supplier.save end private def supplier_params params.require(:supplier).permit(products_attributes: [:name]) end ``` ## Documentation 1. [Installation](./docs/installation.md) 2. StoreModel::Model API: * [Instantiation](./docs/instantiation.md) * [Validations](./docs/validations.md) * [Enums](./docs/enums.md) * [Nested models](./docs/nested_models.md) * [Unknown attributes](./docs/unknown_attributes.md) 3. [Array of stored models](./docs/array_of_stored_models.md) 4. [One of](./docs/one_of.md) 4. [Alternatives](./docs/alternatives.md) 5. [Defining custom types](./docs/defining_custom_types.md) 6. [Disabling Parent Tracking](./docs/enable_parent_assignment.md) ## Credits Initially sponsored by [Evil Martians](http://evilmartians.com). ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ", Assign "at most 3 tags" to the expected json: {"id":"10815","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"