Skip to content

Instantly share code, notes, and snippets.

View vittorius's full-sized avatar

Victor Zagorodny vittorius

View GitHub Profile
class Time
TZ_ABBREVIATION_MAP = ActiveSupport::TimeZone.all.each_with_object({}) do |tz, hash|
[tz.tzinfo.current_period.start_transition, tz.tzinfo.current_period.end_transition].each do |transition|
hash.merge!(transition.offset.abbreviation.to_s => tz.tzinfo.name) if transition.present?
end
end.merge('UTC' => 'Etc/UTC').freeze
class << self
def tz_name_for(time_like)
TZ_ABBREVIATION_MAP[time_like.zone]
@vittorius
vittorius / rspec_helper.rb
Created May 15, 2018 07:42
Wrap rspec-rails request specs methods for testing a JSON API
RSpec.configure do |config|
# ...
config.prepend(
Module.new do
%i[get post patch put delete head].each do |method|
params_snippet = method.in?(%i[post patch put]) ? "args[:params] = args[:params].to_json" : ""
module_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{method}(path, **args)
if args.present? && args.values.none? { |v| v.is_a?(Rack::Test::UploadedFile) }
@vittorius
vittorius / .rspec
Last active January 23, 2022 16:33
VS Code tasks.json for Rubocop and RSpec Flat Error Formatter, w/ config for RSpec
--format RspecFlatErrorFormatter
--color
--require spec_helper
@vittorius
vittorius / truncate_all_tables.rb
Last active January 27, 2021 13:56
Truncate all tables for Rails "test" environment
ActiveRecord::Base.connection.tables.reject { |t| %w[schema_migrations ar_internal_metadata].include?(t) }.each { |t| ActiveRecord::Base.connection.execute("TRUNCATE #{t} CASCADE;") }
@vittorius
vittorius / custom_tagged_rails_logger.rb
Last active September 20, 2018 12:58
Custom tagged Rails logger
class MyFormatter
include ActiveSupport::TaggedLogging::Formatter # to get modified call that collects and prints your tags
# define any custom logic in your formatter
def call(...)
end
end

Keybase proof

I hereby claim:

  • I am vittorius on github.
  • I am vittoriuz (https://keybase.io/vittoriuz) on keybase.
  • I have a public key ASACfV2YrUfvU5LBh9ZX-VGs1_ASxH6C2tdRHJEsZVC7RQo

To claim this, I am signing this object:

# frozen_string_literal: true
require 'rails_helper'
# Haskell: result = operation_1 >>= operation_2 >>= operation_3
# Ruby: result = a&.b&.c
# JS: result = a?.b?.c
describe 'monads' do
describe 'Result' do
@vittorius
vittorius / ruumba.rake
Created April 2, 2021 18:30
Ruumba Rake task
# frozen_string_literal: true
# The default Ruumba::RakeTask is pretty fragile, so we're going a simpler way
namespace :dev do
desc 'Run ruumba linter on app/views'
# rubocop:disable Rails/RakeEnvironment
task :ruumba, :path do |_t, args|
sh "bundle exec ruumba -c .ruumba.yml -e --force-exclusion '#{args.path || 'app/views'}'" do |ok|
puts 'Ruumba has detected some offenses' unless ok
end
@vittorius
vittorius / total_db_rows_breakdown_by_table.rb
Last active September 14, 2021 15:16
See approx. DB rows total count (Rails + Postgres)
puts ActiveRecord::Base.connection.exec_query("SELECT nspname AS schemaname,relname,reltuples FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND relkind='r' ORDER BY reltuples DESC;").rows.map { |row| "#{row.first}\t#{row.second}\t#{row.third}" }
@vittorius
vittorius / ctags alias
Last active May 15, 2023 07:35
VS Code Ruby CTags script and additional setup
alias ctags="`brew --prefix`/bin/ctags" # on macOS, to avoid conflict with XCode ctags