Skip to content

Instantly share code, notes, and snippets.

View sergio1990's full-sized avatar
🇺🇦

Serhii Herniak sergio1990

🇺🇦
View GitHub Profile
@sergio1990
sergio1990 / routes.rb
Created October 9, 2016 12:46
routes with top level scope
Rails.application.routes.draw do
scope 'YOUR_PREFIX_HERE' do
# Your application routes
end
end
@sergio1990
sergio1990 / config.ru
Last active October 9, 2016 12:50
Set SCRIPT_NAME inside Rack middleware
require ::File.expand_path('../config/environment', __FILE__)
class ScriptName
def initialize(app)
@app = app
end
def call(env)
env['SCRIPT_NAME'] += env['HTTP_FRONTEND_URI'].to_s if !Rails.env.development?
@app.call(env)
@sergio1990
sergio1990 / my_gen_server.ex
Created July 6, 2016 14:35
I tried to implement the basic functionality of GenServer by myself :-)
defmodule MyGenServer do
defmacro __using__(_options \\ :empty) do
quote do
def start_link(default_state) do
Task.start_link(fn -> loop(default_state) end)
end
def loop(state) do
receive do
{:call, options, caller} ->
@sergio1990
sergio1990 / ecto_helper.ex
Last active February 27, 2020 15:20
Prettify Ecto errors
defmodule EctoHelper do
@moduledoc """
Provides helper functions
"""
@doc """
Prettifies changeset error messages.
By default `changeset.errors` returns errors as keyword list, where key is name of the field
and value is part of message. For example, `[body: "is required"]`.
This method transforms errors in list which is ready to pass it, for example, in response of
@sergio1990
sergio1990 / setup_exunit_ecto_sandbox.ex
Created June 14, 2016 18:57
Setup ExUnit Ecto sandbox
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(RemarkApi.Repo)
Ecto.Adapters.SQL.Sandbox.mode(RemarkApi.Repo, {:shared, self()})
:ok
end
@sergio1990
sergio1990 / Dockerfile
Created May 29, 2016 14:00
Elixir in Docker
FROM elixir:1.2.3
MAINTAINER Sergey Gernyak <sergeg1990@gmail.com>
ENV MIX_ENV=prod
RUN apt-get update && apt-get install -y build-essential git-core
RUN mkdir -p /app
WORKDIR /app
@sergio1990
sergio1990 / cursor_shape_in_dif_mode
Last active January 18, 2016 10:19
Change cursor shape in different modes
if exists('$TMUX')
let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
else
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
endif
#!/bin/bash
PROJECT_NAME="<YOUR-PROJECT-NAME>"
PROJECT_PATH=" ~/Work/railsapps/${PROJECT_NAME}"
CD_INTO_PROJECT="cd ${PROJECT_PATH}"
tmux has-session -t $PROJECT_NAME
if [ $? != 0 ]
then
@sergio1990
sergio1990 / pipable.rb
Created October 27, 2015 14:29 — forked from pcreux/pipable.rb
*nix has pipes, Elixir has pipes, Ruby deserves pipes.
# Elixir has pipes `|>`. Let's try to implement those in Ruby.
#
# I want to write this:
#
# email.body | RemoveSignature | HighlightMentions | :html_safe
#
# instead of:
#
# HighlightMentions.call(RemoveSignature.call(email.body)).html_safe
#
@sergio1990
sergio1990 / capybara cheat sheet
Created September 30, 2015 08:40 — forked from zhengjia/capybara cheat sheet
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')