Skip to content

Instantly share code, notes, and snippets.

View maxim's full-sized avatar
🛠️

Max Chernyak maxim

🛠️
View GitHub Profile
@maxim
maxim / task.yml
Created June 12, 2014 11:09
Adding github to known_hosts with ansible
- name: ensure github.com is a known host
lineinfile:
dest: /root/.ssh/known_hosts
create: yes
state: present
line: "{{ lookup('pipe', 'ssh-keyscan -t rsa github.com') }}"
regexp: "^github\\.com"
@maxim
maxim / rails_load_path_tips.md
Last active April 13, 2023 13:28
How to use rails load paths, app, and lib directories.

In Rails 3

NOTE: This post now lives (and kept up to date) on my blog: http://hakunin.com/rails3-load-paths

If you add a dir directly under app/

Do nothing. All files in this dir are eager loaded in production and lazy loaded in development by default.

If you add a dir under app/something/

@maxim
maxim / Excellent Karaoke Setup Guide.md
Last active December 24, 2022 03:12
An excellent high quality karaoke setup for under $1000

Excellent high quality karaoke setup for under $1000

For my Vietnamese wife a good karaoke setup is serious business. Problem is, I noticed that some people spend thousands of dollars on unnecessary stuff like gigantic receivers and speakers, only to end up with hiss, clipping, and clunky operation. Smart/consistent home setup should definitely fit under $1000, and that's with speakers and expensive Nvidia Shield box. You could definitely cut it to under $500 and still have a great setup. I did some research and put together a convenient setup for amazing quality at-home karaoke that isn't worth thousands, isn't a chore to operate, and passes Asian quality standards with flying colors.

How to use this setup once it's ready:

  1. You find any song on your phone's Youtube and cast it to your TV
  2. You pick up the microphone, start singing, and it all just sounds surprisingly beautiful

This guide assumes that you already have a TV with HDMI ARC or Optical/Coaxial audio output, and that you have WiFi at home.

def has_role?(role, object)
if object.has_association?(role)
if has_many?
object.roles.find(self.id)
else
object.role.id == self.id
end
else
self.roles.include?(role)
end
@maxim
maxim / ecto_batch_stream.ex
Last active September 9, 2022 18:15
Similar to Rails `find_each`, but for Elixir's Ecto, using Stream
defmodule EctoBatchStream do
import Ecto.Query, only: [from: 1, from: 2]
@batch_size 1000
# Example:
#
# query = from u in MyApp.User, select: u.email
# stream = EctoBatchStream.stream(MyApp.Repo, query)
# stream |> Stream.take(3) |> Enum.to_list # => […]
def authorize_key_for_root(config, *key_paths)
[*key_paths, nil].each do |key_path|
if key_path.nil?
fail "Public key not found at following paths: #{key_paths.join(', ')}"
end
full_key_path = File.expand_path(key_path)
if File.exists?(full_key_path)
config.vm.provision 'file',
@maxim
maxim / react.rb
Created June 28, 2019 06:12
Extending react-rails with styled components support
# config/initializers/react.rb
Rails.configuration.react.server_renderer =
Class.new(React::ServerRendering::BundleRenderer) do
private
def render_from_parts(before, main, after)
js_code = compose_js(before, main, after)
@context.eval(js_code)
end
@maxim
maxim / task.yml
Created June 10, 2014 18:11
ansible task for ensuring postgresql extension exists without skipping or perpetual "changed"
- name: ensure postgresql hstore extension is created
sudo: yes
sudo_user: postgres
shell: "psql {{ postgresql_database }} -c 'CREATE EXTENSION hstore;'"
register: psql_result
failed_when: >
psql_result.rc != 0 and ("already exists" not in psql_result.stderr)
changed_when: "psql_result.rc == 0"
def plain_text_receipt # => Runtime: 5 sec, Network Timeout
<<-TEXT
Thank you for your order!
Product: #{name} - #{price} # => 2 DB queries
Tax: #{tax_amount} # => 1 DB query, 1 API call
Total: #{total_amount} # => 2 DB queries, 1 API call
TEXT
end
private
@maxim
maxim / git.rake
Created September 12, 2009 07:25
Rake tasks for managing git plugins with submodules.
# Rake tasks for managing git plugins with submodules.
#
# These tasks aim to make life simpler by automating all the boring work.
# What you get:
# - complete git integration (all you need to know is install, uninstall and update)
# - complete github integration (only use author name + plugin name)
# - rails plugin hooks (install.rb/uninstall.rb) are taken care of
#
# Available commands:
#