Skip to content

Instantly share code, notes, and snippets.

View Envek's full-sized avatar
☮️
Stop the war | Нет войне!

Andrey Novikov Envek

☮️
Stop the war | Нет войне!
View GitHub Profile
@Envek
Envek / rescue-from-git-push-force.md
Last active April 16, 2024 10:12
Откат ошибочной команды git push --force

Откат ошибочной команды git push --force

Иногда при работе с несколькими удалёнными репозиториями в git, может произойти страшное: git push --force в не тот remote и/или не в ту ветку.

Такое может случиться, например, если вы используете [Deis], в котором деплой запускается при git push нужного коммита в сборщик, когда при отладке деплоя после очередного git commit --amend по запарке вместо git push deis master --force делается просто git push --force. Упс.

Как результат, последние коммиты коллег безвозвратно потеряны, и вы чувствуете неотвратимость их ярости…

Но это git, а значит всё можно починить!

@Envek
Envek / extension.js
Created November 22, 2021 17:50
GNOME Shell extension to switch between two most recently used keyboard layouts
// Installation:
// 1. mkdir -p ~/.local/share/gnome-shell/extensions/switch-to-last-keyboard-layout@envek
// 2. cp ./extension.js ./metadata.json ~/.local/share/gnome-shell/extensions/switch-to-last-keyboard-layout@envek
// 3. Restart GNOME Shell (e.g. log out and log in)
// 4. Enable it in the GNOME Extensions App
// Usage:
// gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/SwitchToLastKeyboardLayout --method org.gnome.Shell.Extensions.SwitchToLastKeyboardLayout.Call
const { Gio } = imports.gi;
const { getInputSourceManager } = imports.ui.status.keyboard;
@Envek
Envek / pg_interval_support_4_1.rb
Last active December 18, 2023 14:41
Enables PostgreSQL interval datatype support (as ActiveSupport::Duration) in Ruby on Rails from 4.1 to 6.0
# Enables PostgreSQL interval datatype support (as ActiveSupport::Duration) in Ruby on Rails 4.1.
# Based on https://gist.github.com/clarkdave/6529610
require 'active_support/duration'
# add a native DB type of :interval
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:interval] = { name: 'interval' }
# add the interval type to the simplified_type list. because this method is a case statement
# we can't inject anything into it, so we create an alias around it so calls to it will call
@Envek
Envek / login_helpers.rb
Created October 11, 2021 06:42
Signing-in user for integration tests via cookie-only session with Rails, Devise, Capybara, and Cuprite
# spec/system/support/login_helpers.rb
# See this blog post for setup guide: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing
module LoginHelpers
def login_as(user)
# Craft session cookie to make request authenticated (to pass even routing constraints)
# Compilation of these:
# - https://dev.to/nejremeslnici/migrating-selenium-system-tests-to-cuprite-42ah#faster-signin-in-tests
# - https://turriate.com/articles/2011/feb/how-to-generate-signed-rails-session-cookie
# - https://github.com/rails/rails/blob/43e29f0f5d54294ed61c31ddecdf76c2e1a474f7/actionpack/test/dispatch/cookies_test.rb#L350
@Envek
Envek / preload_first_n_records_of_association.rb
Created May 17, 2022 15:51
Preload first N records of an association in ActiveRecord (Ruby on Rails)
# Collection has and belongs to many Projects through CollectionProject
# Usage
@collections = current_user.collections.page(1)
ActiveRecord::Associations::Preloader.new.preload(@collections, :projects, limited_projects(3))
# Now @collections.first.projects will have only first 3 projects accessible
# Constructs SQL like this to preload limited number of recent projects along with collections:
# SELECT * FROM (
# SELECT "projects".*, row_number() OVER (PARTITION BY collection_projects.collection_id ORDER BY collection_projects.created_at) AS collection_position
@Envek
Envek / timezones.ru.rb
Last active August 19, 2023 19:40
Full list of russian time zones for Ruby on Rails
# Full list of Russian Federation time zones with helper to get this list.
# Place this file in config/initializers/timezones.ru.rb
class ActiveSupport::TimeZone
@country_zones = ThreadSafe::Cache.new
def self.country_zones(country_code)
code = country_code.to_s.upcase
@country_zones[code] ||=
TZInfo::Country.get(code).zone_identifiers.select do |tz_id|
@Envek
Envek / install-apks-from-phone-to-phone.sh
Created December 28, 2022 09:54
If you have an application installed on one device, but for some reason can't install it on another via Play Market (e.g. dumb country restrictions!), then this script will allow you to grab APKs from one phone and install them on another!
#!/bin/sh
usage() {
echo "Android application copy from one device to another via ADB APK files copy (requires USB debugging to be enabled)"
echo "Usage: $0 <application_id> [application_id…]"
echo "Example: $0 jp.naver.line.android"
}
if [ $# -eq 0 ]; then usage; exit 1; fi
if [ "$1" = '--help' ]; then usage; exit 0; fi
@Envek
Envek / yabeda-prometheus-metrics-exporter.rb
Created November 7, 2022 13:03
Example of separate metrics exporter process for Yabeda with Prometheus (Ruby)
#!/usr/bin/env ruby
# Example of how to launch separate process to export all metrics from Ruby processes on the same host/container.
# IMPORTANT:
# You MUST configure Direct File store in official Prometheus Ruby client for this to work.
# See https://github.com/yabeda-rb/yabeda-prometheus#multi-process-server-support
# Example of configuration that should be made for additional process to export all metrics
# Here: export Sidekiq metrics from not worker process. See https://github.com/yabeda-rb/yabeda-sidekiq#configuration
@Envek
Envek / true_money.rb
Last active June 23, 2022 13:43
Example of “true money” PostgreSQL composite type including both amoiunt and currency and how to work with it in Ruby on Rails
# Example of custom PostgreSQL composite type support
# Works with Rails 6.0, also should work with Rails 5.2
# Place this file to config/initializers/
gem "money"
require "money"
# Subclass Money class just to get pretty output in rails console
class TrueMoney < Money
def inspect
@Envek
Envek / broadcast.sh
Created June 30, 2011 23:12
Script for launching multicast videostreaming (with logging, restarting and notifications)
#!/bin/bash
#
# Script for managing videostreaming over LAN with logging and GUI notifications.
#
# Dependencies: tstools libnotify-bin
#
# 2012, Novikov «Envek» Andrey. Use it freely under the MIT license.
# Configuration
MULTICASTADDR=224.0.0.1 # Target IP for streaming (from multicast range)