{"id":13394889,"url":"https://github.com/DmitryTsepelev/store_model","last_synced_at":"2025-03-13T20:31:44.796Z","repository":{"id":40680935,"uuid":"170321215","full_name":"DmitryTsepelev/store_model","owner":"DmitryTsepelev","description":"Work with JSON-backed attributes as ActiveRecord-ish models","archived":false,"fork":false,"pushed_at":"2024-12-10T12:36:56.000Z","size":294,"stargazers_count":1096,"open_issues_count":23,"forks_count":91,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-12-16T16:07:02.888Z","etag":null,"topics":["activerecord","hacktoberfest","json","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DmitryTsepelev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-12T13:17:57.000Z","updated_at":"2024-12-15T10:29:46.000Z","dependencies_parsed_at":"2023-11-16T22:54:12.207Z","dependency_job_id":"3ec55a8b-b8be-4d0a-942a-cc9b57587e18","html_url":"https://github.com/DmitryTsepelev/store_model","commit_stats":{"total_commits":174,"total_committers":51,"mean_commits":3.411764705882353,"dds":0.3390804597701149,"last_synced_commit":"a1e197250a0047106d8f7c8e03f86c85b292c1dc"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fstore_model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fstore_model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fstore_model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DmitryTsepelev%2Fstore_model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DmitryTsepelev","download_url":"https://codeload.github.com/DmitryTsepelev/store_model/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243475363,"owners_count":20296711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","hacktoberfest","json","rails","ruby"],"created_at":"2024-07-30T17:01:35.208Z","updated_at":"2025-03-13T20:31:44.790Z","avatar_url":"https://github.com/DmitryTsepelev.png","language":"Ruby","readme":"# 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)\n\n**StoreModel** gem allows you to wrap JSON-backed DB columns with ActiveModel-like classes.\n\n- 💪 **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\n- 🔧 **Works like ActiveModel**. Validations, enums and nested attributes work very similar to APIs provided by Rails\n- 1️⃣ **Follows single responsibility principle**. Keep the logic around the data stored in a JSON column separated from the model\n- 👷‍♂️ **Born in production**.\n\n```ruby\nclass Configuration\n  include StoreModel::Model\n\n  attribute :model, :string\n  enum :status, %i[active archived], default: :active\n\n  validates :model, :status, presence: true\nend\n\nclass Product \u003c ApplicationRecord\n  attribute :configuration, Configuration.to_type\nend\n```\n\nYou can support my open–source work [here](https://boosty.to/dmitry_tsepelev).\n\n## Why should I wrap my JSON columns?\n\nImagine that you have a model `Product` with a `jsonb` column called `configuration`. This is how you likely gonna work with this column:\n\n```ruby\nproduct = Product.find(params[:id])\nif product.configuration[\"model\"] == \"spaceship\"\n  product.configuration[\"color\"] = \"red\"\nend\nproduct.save\n```\n\nThis 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.\n\nFor 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`)?\n\n\u003e 📖 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\n\n## Getting started\n\nStart with creating a class for representing the hash as an object:\n\n```ruby\nclass Configuration\n  include StoreModel::Model\n\n  attribute :model, :string\n  attribute :color, :string\nend\n```\n\nAttributes 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.\n\nRegister the field in the ActiveRecord model class:\n\n```ruby\nclass Product \u003c ApplicationRecord\n  attribute :configuration, Configuration.to_type\nend\n```\n\nWhen you're done, the initial snippet could be rewritten in the following way:\n\n```ruby\nproduct = Product.find(params[:id])\nif product.configuration.model == \"spaceship\"\n  product.configuration.color = \"red\"\nend\nproduct.save\n```\n\n_Usage note: Rails and assigning Arrays/Hashes to records_\n\n- 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.\n- 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(\u0026:as_json)`) or by `will_change!` (`self.my_stored_models_will_change!`)\n- Mixing `StoreModel::NestedAttributes` into your model will allow you to use `accepts_nested_attributes_for` in the same way as ActiveRecord.\n\n```ruby\nclass Supplier \u003c ActiveRecord::Base\n  include StoreModel::NestedAttributes\n\n  has_many :bicycles, dependent: :destroy\n\n  attribute :products, Product.to_array_type\n\n  accepts_nested_attributes_for :bicycles, :products, allow_destroy: true\nend\n```\n\nThis will allow the form builders to work their magic:\n\n```erb\n\u003c%= form_with model: @supplier do |form| %\u003e\n  \u003c%= form.fields_for :products do |product_fields| %\u003e\n    \u003c%= product_fields.text_field :name %\u003e\n  \u003c% end %\u003e\n\u003c% end %\u003e\n```\n\nResulting in:\n```html\n\u003cinput type=\"text\" name=\"supplier[products_attributes][0][name]\" id=\"supplier_products_attributes_0_name\"\u003e\n```\n\nIn the controller:\n```ruby\ndef create\n  @supplier = Supplier.from_value(supplier_params)\n  @supplier.save\nend\n\nprivate\n\ndef supplier_params\n  params.require(:supplier).permit(products_attributes: [:name])\nend\n```\n\n## Documentation\n\n1. [Installation](./docs/installation.md)\n2. StoreModel::Model API:\n  * [Instantiation](./docs/instantiation.md)\n  * [Validations](./docs/validations.md)\n  * [Enums](./docs/enums.md)\n  * [Nested models](./docs/nested_models.md)\n  * [Unknown attributes](./docs/unknown_attributes.md)\n3. [Array of stored models](./docs/array_of_stored_models.md)\n4. [One of](./docs/one_of.md)\n4. [Alternatives](./docs/alternatives.md)\n5. [Defining custom types](./docs/defining_custom_types.md)\n6. [Disabling Parent Tracking](./docs/enable_parent_assignment.md)\n\n## Credits\n\nInitially sponsored by [Evil Martians](http://evilmartians.com).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Gems","Ruby"],"sub_categories":["Attributes Management"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDmitryTsepelev%2Fstore_model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDmitryTsepelev%2Fstore_model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDmitryTsepelev%2Fstore_model/lists"}