This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
log = logging.getLogger('django.db.backends') | |
log.setLevel(logging.DEBUG) | |
log.addHandler(logging.StreamHandler()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Utils | |
class Timer | |
def self.call(name) | |
t = Time.zone.now | |
result = yield | |
time = ((Time.zone.now - t).to_d * 1000).to_i | |
p "++++++++++ #{name}: #{time}ms" | |
{ result: result, time: time } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FooPresenter | |
include Draper::Decoratable | |
attr_accessor :user | |
def initialize(user) | |
@user = user | |
end | |
def name | |
user.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let we have Post(title, body) -> Comments(body). | |
# Write the active record query to get all posts, where "query" is into title or body or comment. | |
class Post < ActiveRecord::Base | |
#title, body | |
has_many :comments | |
scope :search, ->(text) { | |
wildcard_search = "%#{text}%" | |
joins(:comments).where("title ILIKE :search OR body ILIKE :search OR comments.body ILIKE :search", search: wildcard_search) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dir["app/assets/images/*"].each do |image| | |
name = File.basename(image) | |
if Dir.glob("**/*").reject {|fn| File.directory?(fn) }. | |
map{|fn| File.read(fn).include?(name) ? fn : nil }.any? | |
p image | |
else | |
p "To delete: #{image}" | |
File.delete image | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AuthenticityTokenCatcher | |
extend ActiveSupport::Concern | |
included do | |
rescue_from ActionController::InvalidAuthenticityToken do |exception| | |
if request.referrer | |
flash[:error] = I18n.t :authenticit_token_error | |
redirect_to request.referrer | |
else | |
raise exception | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a demo task. | |
A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e. | |
A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1]. | |
Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1. | |
For example, consider the following array A consisting of N = 8 elements: | |
A[0] = -1 | |
A[1] = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04 | |
How To Install and Configure Redis on Ubuntu 16.04 | |
PostedMay 11, 2016 51.6k views NOSQL REDIS UBUNTU UBUNTU 16.04 | |
Introduction | |
Redis is an in-memory key-value store known for its flexibility, performance, and wide language support. In this guide, we will demonstrate how to install and configure Redis on an Ubuntu 16.04 server. | |
Prerequisites | |
To complete this guide, you will need access to an Ubuntu 16.04 server. You will need a non-root user with sudo privileges to perform the administrative functions required for this process. You can learn how to set up an account with these privileges by following our Ubuntu 16.04 initial server setup guide. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flattify(array) | |
array.each_with_object([]) do |element, flattened| | |
flattened.push *(element.is_a?(Array) ? flattify(element) : element) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Developer | |
def initialize | |
@chain = [] | |
end | |
def are | |
@chain << "I" | |
self | |
end |
NewerOlder