Skip to content

Instantly share code, notes, and snippets.

View dapi's full-sized avatar
🌴
Open for offers

Danil Pismenny dapi

🌴
Open for offers
View GitHub Profile
@dapi
dapi / 20101219150921_add_typus_attributes_to_users.rb
Created December 19, 2010 18:20
Use Devise authentication for Typus 3.0
class AddTypusAttributesToUsers < ActiveRecord::Migration
def self.up
add_column :users, :role, :string
add_column :users, :status, :boolean, :null=>false, :default=>true
User.find(1).update_attribute(:role,'admin')
end
def self.down
remove_column :users, :role
remove_column :users, :status
@dapi
dapi / mysql2_downcase_adapter.rb
Created July 6, 2011 05:50
mysql2 adapter with downcasing column names
# -*- coding: utf-8 -*-
# Just use 'mysql2_downcase' adapter in database.yml
#
require 'active_record/connection_adapters/mysql2_adapter'
module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
def self.mysql2_downcase_connection(config)
@dapi
dapi / bitrix_authorization.rb
Created January 4, 2012 20:38
1С-Битрикс авторизация на рельсах (ruby on rails)
# Расширение контроллера
#
module Bitrix::Authorization
def current_user
@current_user ||= find_from_cookie
end
def login_user(user)
logout_user if current_user
@current_user = user
@dapi
dapi / reset_counters.rake
Last active December 10, 2019 07:04
Update all model`s counters
# -*- coding: utf-8 -*-
#
namespace :db do
desc 'Update all model`s counters'
task :reset_counters => :environment do
Dir.glob(Rails.root + '/app/models/*.rb').each { |file| require file }
models = Rails::VERSION::MAJOR >= 3 ? ActiveRecord::Base.connection.tables.map {|t| t.classify.constantize rescue nil}.compact : Object.subclasses_of(ActiveRecord::Base)
models.each do |model|
@dapi
dapi / ruby_test.md
Last active December 10, 2019 07:07
Test task for Ruby

The issue

Write a program in Ruby that takes one command line argument (referred to going forward as n). If n is not a perfect square print an appropriate error message and exit. If n is a perfect square then create a sequence from 1 to n and build a matrix from the sequence by walking in counter-clockwise spiral order. Examples below illustrate this for n=1,4,9,16 but your solution should work for any n that is a perfect square. Once you have built the matrix print it, ensuring even column widths as in examples below and then exit.

  Input:  1
@dapi
dapi / find_ordered.rb
Last active December 10, 2019 07:20
Ordered find record by intarray in Postgresql with Rails
# Find ordered record for postgresql in Rails
# enable_extension "intarray"
module FindOrdered
extend ActiveSupport::Concern
included do
scope :find_ordered, ->(ids) { where(id: ids).order("idx(ARRAY#{ids}::integer[], id)") }
end
end
@dapi
dapi / gist:6a9d61e95019b4f5666a
Created December 4, 2015 05:24
Виджет для upload
<script type="text/javascript">
window.art=window.art||[],art.load=function(t,r){art.appId=t,art.config=r=r||{};var e=r.forceSSL||"https:"===document.location.protocol,a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src="http://cdn.1clickanalytics.ru/widget.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n);for(var o=function(t){return function(){art.push([t].concat(Array.prototype.slice.call(arguments,0)))}},c=["clearEventProperties","identify","setEventProperties","track","unsetEventProperty"],s=0;s<c.length;s++)art[c[s]]=o(c[s])},art.load("100003");
</script>
@dapi
dapi / SSL_OCSP_Stapling.conf
Last active October 17, 2019 21:50
nginx ssl conf
# Проверка HTTPS на Comodo:
# https://sslanalyzer.comodoca.com/?url=https%3A%2F%2Fkiiiosk.ru
# Проверка на Trusted
# https://www.digicert.com/help/
# source:
# https://support.comodo.com/index.php?/Default/Knowledgebase/Article/View/1091/1/certificate-installation--nginx
ssl_stapling on;
class SpfChecker
Response = Struct.new(:correct, :spf_value, :message)
def initialize(value)
@value
end
def check(domain)
result = Net::DNS::Resolver.start(domain, Net::DNS::TXT)
values = result.each_mx.map(&:txt)
@dapi
dapi / list_queries.sql
Created June 7, 2017 11:45
Finding and killing long running queries on PostgreSQL
SELECT pid, now() - query_start as duration, query, state FROM pg_stat_activity WHERE now() - query_start > interval '5 minutes';
;; cancel
SELECT pg_cancel_backend(__pid__);
;; kill stucked
SELECT pg_terminate_backend(__pid__);