Git Product home page Git Product logo

active_denormalize's Introduction

ActiveDenormalize

ActiveDenormalize is a gem that allows you to denormalize data across your ActiveRecord models. It currently supports belongs_to relationships.

Below is a simple example which you can use to get started (and mostly until further documentation is added in this README).

class Product < ApplicationRecord
  has_many :inventory_checks
end

class InventoryCheck < ApplicationRecord
  belongs_to :product, denormalize: true
end

ActiveDenormalize works by adding create, update, and destroy after_*_commit callbacks to the host model, in this case InventoryCheck, which will update the denormalized attributes on the Product model.

To know which attributes to denormalize on to the Product model, ActiveDenormalize will look for inventory_check prefixed columns on the Product model. For example, if you have a inventory_check_id column on the Product model, ActiveDenormalize will update that column with the ID of most recent InventoryCheck record that belong to the Product.

# No InventoryCheck has been created yet
> product = Product.create(name: "Product 1")
> product.inventory_check_id
=> nil
> product.inventory_check_created_at
=> nil
> product.inventory_check_status
=> nil
> product.inventory_check_denormalized_at
=> nil

# Updates the Product record to know about the newest InventoryCheck
> first_inventory_check = InventoryCheck.create(product: product, status: "backordered")
> first_inventory_check.id
=> 1
> product.reload.inventory_check_id
=> 1
> product.inventory_check_created_at
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
> product.inventory_check_status
=> "backordered"
> product.inventory_check_denormalized_at
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00

# Updates the Product record to know about the newest InventoryCheck
> second_inventory_check = InventoryCheck.create(product: product, status: "available")
> second_inventory_check.id
=> 2
> product.reload.inventory_check_id
=> 2
> product.inventory_check_created_at
=> Sun, 17 Sep 2023 17:28:53.903842001 UTC +00:00
> product.inventory_check_status
=> "available"
> product.inventory_check_denormalized_at
=> Sun, 17 Sep 2023 17:28:53.903842001 UTC +00:00
> second_inventory_check.destroy

# Reverts to the previous InventoryCheck record
> product.reload.inventory_check_id
=> 1
> product.inventory_check_created_at
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
> product.inventory_check_status
=> "backordered"
> product.inventory_check_denormalized_at
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00

# Updates the denormalized representation of the InventoryCheck on Product
> first_inventory_check.update!(status: "available")
> product.reload.inventory_check_id
=> 1
> product.inventory_check_created_at
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
> product.inventory_check_status
=> "available"
> product.inventory_check_denormalized_at # This timestamp will be updated
=> Sun, 17 Sep 2023 17:31:16.388224345 UTC +00:00

Installation

Add this line to your application's Gemfile:

gem "active_denormalize"

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_denormalize

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_denormalize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

active_denormalize's People

Contributors

dewski avatar

Watchers

 avatar  avatar

active_denormalize's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.