Skip to content

Instantly share code, notes, and snippets.

View Bajena's full-sized avatar
🎩
Working on stuff

Jan Bajena Bajena

🎩
Working on stuff
View GitHub Profile
# 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 }
@Bajena
Bajena / gist.rb
Created January 27, 2019 21:23
Rails joins
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
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 }
@Bajena
Bajena / gist:e80d3b6293490d02fe68c9a28c67fa04
Last active January 27, 2019 21:21
N+1 zapytań - przykład
posts = Post.where(id: [1, 2, 3])
# SELECT * FROM posts WHERE id IN (1, 2, 3)
users = posts.map { |post| post.user }
# SELECT * FROM users WHERE id = 1
# SELECT * FROM users WHERE id = 2
# SELECT * FROM users WHERE id = 3
@Bajena
Bajena / deploy.sh
Created July 21, 2018 14:07
Deploy Google Data Studio connector using CLASP library
#!/bin/sh
echo "Running deploy script..." &&
cd src &&
version_description=`git log --oneline -1 | cat` && # e.g. ec2ab6f My commit message
echo "Pushing code..." &&
clasp push &&
echo "Creating new version: $version_description" &&
version_number=`clasp version "$version_description" | grep -o '[0-9][0-9]*' | tail -1` &&
echo "Created version $version_number" &&
@Bajena
Bajena / config.yml
Created July 21, 2018 13:06
Simple CircleCI config for running GDS connector's tests
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:7.10
@Bajena
Bajena / .eslintrc.js
Created July 16, 2018 18:05
eslint config for GDS connector + jest tests
module.exports = {
"env": {
"browser": true,
"es6": true,
"jest/globals": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017
@Bajena
Bajena / DateUtils.test.js
Created July 10, 2018 20:25
Simple jest test
const DateUtils = require('../src/DateUtils.js');
test('getDatePart', () => {
const date = new Date('2018-07-09');
expect(DateUtils.getDatePart(date)).toBe('2018-07-09');
});
@Bajena
Bajena / DateUtils.js
Created July 10, 2018 20:25
Date utils for GDS
var DateUtils = {
/*
* Converts Date object to a String containing the date part (with dashes).
*
* @return {String} Date part. E.g. '2018-07-10'.
*/
getDatePart: function(dateObject) {
return dateObject.toISOString().slice(0, 10);
}
}
@Bajena
Bajena / Code.gs
Last active June 24, 2018 18:04
Code.gs file with caching
//...
function getData(request) {
// ...
var startDate = request.dateRange.startDate;
var endDate = request.dateRange.endDate;
var cache = new DataCache(CacheService.getUserCache(), startDate, endDate);
var plays = null;