Skip to content

Instantly share code, notes, and snippets.

@IvikGH
IvikGH / ruby_tasks.rb
Created December 13, 2018 00:04
ruby_tasks
# Sort by frequency
def frequency(arr)
arr.each_with_object(Hash.new(0)) { |letter, result| result[letter] += 1 }
.sort_by { |_k, v| -v }.to_h.keys
end
# Flatten a list
def flatten(arr)
arr.flatten!
end
@IvikGH
IvikGH / test_sql_queries.sql
Last active December 12, 2018 23:04
test sql queries
# get all students last_name, alphabetically ordered, not repeating
SELECT DISTINCT last_name
FROM students
ORDER BY last_name
# get the count of all students in each groups, order by groups names descending
SELECT groups.name,
Count(students.id)
FROM groups
LEFT JOIN students
@IvikGH
IvikGH / transactions.markdown
Created September 24, 2018 13:13 — forked from jcasimir/transactions.markdown
Transactions

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

let(:inst_client) { double(user: inst_user) }
let(:inst_user) do
# instance_double(Instagram::Client, id: instagram_id,
double(id: instagram_id,
access_token: access_token,
username: name,
full_name: 'John Doe',
picture: picture)
end
let(:instagram_id) { '1234567890' }
let(:picture) { 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' }
let(:inst_client) { double(user: inst_user) }
let(:inst_user) do
# instance_double(Instagram::Client, id: instagram_id,
double(id: instagram_id,
access_token: access_token,
username: name,
@IvikGH
IvikGH / heroku-remote.sh
Created November 22, 2017 16:25 — forked from randallreedjr/heroku-remote.md
Add a Heroku remote to an existing git repo
$ git remote add staging https://git.heroku.com/staging-app.git
$ git remote add heroku https://git.heroku.com/app.git
@IvikGH
IvikGH / nil_vs_empty_vs_blank_ror.md
Created September 27, 2017 07:13 — forked from Jellyfishboy/nil_vs_empty_vs_blank_ror.md
nil vs empty vs blank in Ruby on Rails

.nil? can be used on any object and is true if the object is nil.

.empty? can be used on strings, arrays and hashes and returns true if:

  • String length == 0
  • Array length == 0
  • Hash length == 0

Running .empty? on something that is nil will throw a NoMethodError.

@IvikGH
IvikGH / new_practice.rb
Created April 25, 2017 16:06
some example to get new practices
Чтобы не быть голословным:
require 'redis'
require 'connection_pool'
module Shared
class RedisPool
DEFAULT_POOL_SIZE = 100.freeze # Per 1 process
# Closes all the connections and clears the pool
#
@IvikGH
IvikGH / pimpochka.html
Created April 25, 2017 11:59
pimpochka.html
<div class="switch">
<input type="checkbox" id="autopayments" class="switch-checkbox" checked="checked">
<label class="switch-label" for="autopayments">
<span class="switch-inner"></span>
<span class="switch-btn"></span>
</label>
</div>
@IvikGH
IvikGH / rails_helper.rb
Last active April 19, 2017 15:04
rails_helper
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require 'paper_trail/frameworks/rspec'
require "pundit/rspec"
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }