This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Loader | |
def load | |
Enumerator.new { |main_enum| stream(main_enum) } | |
end | |
private | |
def stream(main_enum) | |
reader = nil | |
file_uri.open do |file| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "net/ftp" | |
class Loader | |
def load | |
Enumerator.new { |main_enum| stream(main_enum) } | |
end | |
private |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseApiController | |
def render_json_api(options) | |
render( | |
options.merge(included).merge(fields: fields).merge(adapter: :json_api) | |
) | |
end | |
# Allows limiting fields in given resource type's payloads. | |
# `fields` param is sent as a hash in which: | |
# - keys are singular or plural resource names (e.g. `accounts` or `account`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calls provided HTTP requests batch and retries in case of errors | |
* | |
* @param {object} fetchService - service for performing HTTP requests. Defaults to UrlFetchApp provided by Google. | |
* @param {Array<object>} requests - Array of request param objects | |
* (https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params) | |
* | |
* @return {object} RetriableRequestsBatch. | |
*/ | |
function RetriableRequestsBatch(fetchService, requests) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseSerializer < ActiveModel::Serializer | |
include AmsLazyRelationships::Core | |
end | |
class PostSerializer < BaseSerializer | |
attributes :id, :title | |
lazy_belongs_to :user | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSerializer < ActiveModel::Serializer | |
attributes :id, :title | |
belongs_to :user | |
def self.lazy_user(post) | |
BatchLoader.for(post.user_id).batch do |user_ids| | |
User.where(id: user_ids) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users = posts.map { |post| post.user_lazy } | |
users.each { |user| puts "#{user.name}" } # SELECT * FROM users WHERE id IN (1, 2, 3) | |
users.each { |user| puts "#{user.address}" } # Pobierze użytkowników z cache'u |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/post.rb | |
def user_lazy | |
BatchLoader.for(user_id).batch do |user_ids| | |
User.where(id: user_ids) | |
end | |
end | |
posts = Post.where(id: [1, 2, 3]) # SELECT * FROM posts WHERE id IN (1, 2, 3) | |
users = posts.map { |post| post.user_lazy } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Post.joins(:users).where(users: { country_code: 'PL' }).map { |post| post.title } | |
# SELECT "posts".* FROM "posts" INNER JOIN "users" ON "posts"."user_id" = "users"."id" WHERE "users"."country_code" = $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
posts = Post.where(id: [1, 2, 3]).includes(:user) | |
# SELECT * FROM posts WHERE id IN (1, 2, 3) | |
# SELECT * FROM users WHERE id IN (1, 2, 3) | |
users = posts.map { |post| post.user } |
NewerOlder