Skip to content

Instantly share code, notes, and snippets.

View bublik's full-sized avatar

Voloshyn Ruslan bublik

View GitHub Profile
@bublik
bublik / reset_primary_key.md
Created November 26, 2018 18:15 — forked from paulsturgess/reset_primary_key.md
Reset postgres primary key index using Rails
$ ActiveRecord::Base.connection.reset_pk_sequence!('table_name')

If you need the table names:

$ ActiveRecord::Base.connection.tables

=> ["accounts", "assets", ...]

@bublik
bublik / AllControllersWithActions
Created October 28, 2017 04:36
Some time you need get all controllers and actions and dont touch routing.
$ rails c
Rails.application.eager_load!
ApplicationController.subclasses.each_with_index do |c, num|
puts “#{num} #{c}“; puts ” - ” + c.instance_methods(false).join(‘, ’)
end
#WhiteLabelSettingsController
# - new, index, update, create, edit, show, navbar_logo, login_logo
@bublik
bublik / movie-titles.rb
Created December 4, 2015 09:08 — forked from karmi/movie-titles.rb
Multiple analyzers and query fields in Elasticsearch for auto-completion
require 'tire'
# Tire.configure { logger STDERR, level: 'debug' }
Tire.index('movie-titles') do
delete
create \
settings: {
index: {
analysis: {
@bublik
bublik / pinger.rb
Created September 26, 2015 13:14 — forked from technoweenie/pinger.rb
ZeroMQ pub/sub demo
require 'zmq'
context = ZMQ::Context.new
pub = context.socket ZMQ::PUB
pub.setsockopt ZMQ::IDENTITY, 'ping-pinger'
pub.bind 'tcp://*:5555'
i=0
loop do
pub.send "ping pinger #{i+=1}" ; sleep 1
end
@bublik
bublik / infos_controller.rb
Created September 11, 2015 12:02
join sql selects to one request
def new_messages_count
require 'benchmark/ips'
record = User.connection.select_one("SELECT * FROM
(#{current_user.unread_todos_counter.select('count(*) as unread_todos').to_sql}) unread_todos,
(#{current_user.unread_chats_counter.reorder('').select('count(*) as unread_chats').to_sql}) unread_chats,
(#{current_user.pending_houses.select('count(*) as pending_invitations').to_sql}) pending_invitations,
(#{current_user.received_invited_clients.select('count(*) as received_invited_clients').to_sql}) received_invited_clients
")
logger.debug(record)
@bublik
bublik / inventory_dimension_units.rb
Created August 28, 2015 12:29
FactoryGirl has_and_blongs_to_many associaltion save
FactoryGirl.define do
factory :inventory_dimension_unit, :class => 'Inventory::DimensionUnit' do
inventory_unit_type
name "MyString"
ratio "0.3"
inventory_categories { [create(:inventory_category)] }
factory :inventory_dimension_unit_imperial do
system 1
end
@bublik
bublik / application.rb
Created April 28, 2015 14:53
show user translation keys
module I18n
class << self
alias :__translate :translate # move the current self.translate() to self.__translate()
def translate(key, *options)
puts "Translate: #{key}" unless key.match(/faker/)
return self.__translate(key, options)
end
end
end
@bublik
bublik / snapshot.coffee
Created April 20, 2015 18:48
Snapshot builder for web sites
#Scriptable Headless WebKit http://phantomjs.org
# original source url https://github.com/ariya/phantomjs
page = require('webpage').create()
system = require 'system'
if system.args.length < 3
console.log 'Usage: rasterize.coffee URL filename [paperwidth*paperheight|paperformat]'
phantom.exit 1
else
@bublik
bublik / store.rb
Created April 20, 2015 18:47
How to build snapshots from web sites
def snapshot_update
#Thumbs maker
begin
if system("phantomjs #{Rails.root}/app/assets/snapshots/snapshot.coffee #{url} #{snapshot_original}") &&
system("convert #{snapshot_original} -filter Lanczos -thumbnail 225x225 #{snapshot_thumb}")
logger.info("Snapshot: #{url} OK ")
self.update_attributes!(snaps_updated_at: Time.now, present_snapshot: true)
else
logger.error("Fail: #{url}")
end
#!/usr/bin/env ruby
# s3-delete-bucket.rb
# Fog-based script for deleting large Amazon AWS S3 buckets (~100 files/second)
# Forked from this excellent script: https://github.com/SFEley/s3nuke
require 'rubygems'
require 'thread'
require 'fog'