Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

polymorphic_preloader

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

polymorphic_preloader

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

PolymorphicPreloader

This gem does exactly what it says — it preloads nested polymorphic associations on ActiveRecord objects.

Here's a common example:

class Transaction < ActiveRecord::Base
  belongs_to :user
  belongs_to :purchase, polymorphic: true
end

class Product < ActiveRecord::Base
  has_many :transactions, as: :purchase
  belongs_to :company
end

class Service < ActiveRecord::Base
  has_many :transactions, as: :purchase
  belongs_to :provider
end

Suppose we want to show all the products and services that user has purchased. Apparently we want to eager-load products with companies and services with providers. ActiveRecord won't allow you to do includes(purchase: [ :company, :provider ]). Here's where PolymorphicPreloader comes to help.

Installation

Add this line to your application's Gemfile:

gem 'polymorphic_preloader', github: 'antstorm/polymorphic_preloader', branch: 'initial-version'

And then execute:

$ bundle

Or install it yourself as:

$ gem install polymorphic_preloader

Usage

Considering the above example:

transactions = current_user.transactions.includes(:purchase)

PolymorphicPreloader.new(transactions, :purchase).preload!(product: :company, service: :provider)

TODO

  • Ensure good test coverage
  • Tidy up and release as a gem
  • Hook into ActiveRecord for a delayed preloading and nicer interface (e.g. includes_polymorphic())

FAQs

Package last updated on 05 Jul 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc