Giter Site home page Giter Site logo

feracommerce / lazy_columns Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jorgemanrubia/lazy_columns

0.0 1.0 0.0 306 KB

Rails plugin that adds support for lazy-loading columns in Active Record models

License: MIT License

Ruby 87.08% JavaScript 2.37% CSS 2.01% HTML 8.54%

lazy_columns's Introduction

lazy_columns

lazy_columns is a Rails plugin that lets you specify columns to be loaded lazily in your Active Record models.

By default, Active Records loads all the columns in each model instance. This plugin lets you specify columns to be excluded by default. It is intended for scenarios where you have large attributes and don't want to load them in every operation because of performance.

Notice that a much better approach is moving those columns to new models, since Rails loads related models lazily by default. This plugin is an easy workaround.

Installation

In your Gemfile

gem 'lazy_columns'

Usage

Use lazy_load in your Active Record models to define which column or columns should be loaded lazily:

class Action < ActiveRecord::Base
  lazy_load :comments

  attr_accessible :comments, :title
end

Now, when you fetch some action the comments are not loaded:

Action.create(title: "Some action", comments: "Some comments") # => <Action id: 1...>
action = Action.find(1) # => <Action id: 1, title: "Some action">

And if you try to read the comments attribute it will be loaded into the model:

action.comments # => "Some comments"
action # => <Action id: 1, title: "Some action", comments: "Some comments" 

How the plugin works

This plugin does two things:

  • Modify the default scope of the model so that it fetches all the attributes except the marked as lazy.
  • Define an accessor method per lazy attribute that will reload the corresponding column under demand.

Eager loading of attributes defined as lazy

The first time you access a lazy attribute a new database query will be executed to load it. If you are going to operate on a number of objects and want to have the lazy attributes eagerly loaded use Active Record .select() in the initial query. For example:

Action.select(:comments).all 

Credits

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.