Skip to content

Instantly share code, notes, and snippets.

View sergii's full-sized avatar

Serhii Ponomarov sergii

View GitHub Profile
@sergii
sergii / friendly_urls.markdown
Created August 20, 2021 08:52 — forked from jcasimir/friendly_urls.markdown
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@sergii
sergii / alias_matchers.md
Created August 18, 2021 15:59 — forked from JunichiIto/alias_matchers.md
List of alias matchers in RSpec 3

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value
@sergii
sergii / 1. nginx.conf
Created August 13, 2021 11:37 — forked from mezbahalam/1. nginx.conf
ruby on rails multiple custom domains over Https using Openresty and lua (https://www.babar.im/blog/sysadmin/serve-your-saas-customer-domains-over-https-for-free.html)
## /etc/openresty/nginx.conf
user www-data;
events {
worker_connections 1024;
}
http {
lua_shared_dict auto_ssl 1m;
@sergii
sergii / device.rb
Last active August 9, 2021 12:47
How To Handle Special Actions In Ruby on Rails Controllers?
class Device < ApplicationRecord
# Define Validation Errors (TODO: Move it to a Validator)
class DeviceHasBeenAlreadySetUpError < StandartError
def message
"Device [#{@device.id}] has been already set up."
end
end
# => raise DeviceHasBeenAlreadySetUpError if @device.setup_at.present?
require "rails_helper"
feature "User signs up" do
scenario "and is added to the roster" do
visit root_path
fill_in "Name", with: "Mickey Mouse"
fill_in "Experience", with: "Junior"
click_button "Sign Me Up!"
expect(page).to have_text("Mickey Mouse")
@sergii
sergii / pg_dump_pg_restore.sh
Created July 27, 2021 16:19 — forked from orendon/pg_dump_pg_restore.sh
pg_dump pg_restore postgresql backup postgres
# Create and restore backups on PostgreSQL (tested on 9.2 and 9.3)
# create database backup
PGPASSWORD="XXX" pg_dump --no-owner --no-acl -U USER DATABASE -h HOST -F t > output_file.tar
# create database backup (heroku)
heroku pgbackups:capture
heroku pgbackups # show stored backups (assuming a117 is the desired one)
heroku pgbackups:url a117 # => it retrieves dump url on AWS S3
@sergii
sergii / sort_by.md
Created July 15, 2021 05:57 — forked from Kotauror/sort_by.md
Sort_by ruby method - sorting using multiple attributes.

Sort_by ruby method - sorting using multiple attributes.

I guess everyone knows that feeling when you struggle with a codewars kata, create (or even not!) a looong solution, then you check the solutions of of others and go whaaaaaaaat -- they did it in one line?

This is how I felt today and this is how I refreshed my memory of Ruby's sort_by method.

Task:

@sergii
sergii / heroku_pg_db_reset.md
Created July 7, 2021 08:46 — forked from zulhfreelancer/heroku_pg_db_reset.md
How to reset PG Database on Heroku?

How to reset PG Database on Heroku?

  • Step 1: heroku restart
  • Step 2: heroku pg:reset DATABASE (no need to change the DATABASE)
  • Step 3: heroku run rake db:migrate
  • Step 4: heroku run rake db:seed (if you have seed)

One liner

heroku restart; heroku pg:reset DATABASE --confirm APP-NAME; heroku run rake db:migrate

@sergii
sergii / devise_invitable.ru.yml
Created June 19, 2021 17:40 — forked from volonterx/devise_invitable.ru.yml
Russian translations for devise_invitable. Completely adheres to the format of the generated devise_invitable.en.yml as of 1.4.0.
# Additional translations at https://github.com/scambra/devise_invitable/wiki/I18n
ru:
devise:
failure:
invited: 'У вас есть непринятое приглашение, подтвердите его чтобы продолжить создание аккаунта.'
invitations:
send_instructions: 'Письмо с приглашением было отправлено на %{email}.'
invitation_token_invalid: 'Неверный токен приглашения.'
updated: 'Ваш пароль успешно сохранен. Вы вошли в систему.'