Skip to content

Instantly share code, notes, and snippets.

🙂
🌴

Max Chernyak maxim

🙂
🌴
Block or report user

Report or block maxim

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@maxim
maxim / rails_load_path_tips.md
Last active Nov 19, 2019
How to use rails load paths, app, and lib directories.
View rails_load_path_tips.md

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 / gh-dl-release
Last active Nov 9, 2019
Download assets from private Github releases
View gh-dl-release
#!/usr/bin/env bash
#
# gh-dl-release! It works!
#
# This script downloads an asset from latest or specific Github release of a
# private repo. Feel free to extract more of the variables into command line
# parameters.
#
# PREREQUISITES
#
View barewords.rb
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 Sep 12, 2009
Rake tasks for managing git plugins with submodules.
View git.rake
# 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:
#
@maxim
maxim / ecto_batch_stream.ex
Last active Oct 1, 2019
Similar to Rails `find_each`, but for Elixir's Ecto, using Stream
View ecto_batch_stream.ex
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 # => […]
@maxim
maxim / task.yml
Created Jun 12, 2014
Adding github to known_hosts with ansible
View task.yml
- 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 / react.rb
Created Jun 28, 2019
Extending react-rails with styled components support
View react.rb
# 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 / tm2iterm.rb
Created Jun 10, 2012
Convert TextMate themes into iTerm 2 color schemes.
View tm2iterm.rb
#!/usr/bin/env ruby
#
# This script is an astonishing feat of top notch
# rockstar craftsmanship. It totally uses artificial
# intelligence to extract colors out of tmTheme and
# build an itermcolors scheme file for iTerm2.
#
# I know this sounds crazy, but it actually knows
# approximately what colors should be used in the
# ANSI list, and tries to find nearest colors from
View key_authorization.rb
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',
View stream_ext.ex
defmodule StreamExt do
@doc """
Stream records from Ecto in batches.
Supported options are
* batch_size - how many rows to fetch at once
* strategy - either :offset or :id
* :offset - uses SQL offset to fetch pages of results
This is slower, but works for any query.
You can’t perform that action at this time.