Skip to content

Instantly share code, notes, and snippets.

@rocLv
rocLv / redis.rb
Created April 19, 2017 09:08 — forked from pubis/redis.rb
Redis config and initialization for rails
#config/initializers/redis.rb
require 'redis'
require 'redis/objects'
REDIS_CONFIG = YAML.load( File.open( Rails.root.join("config/redis.yml") ) ).symbolize_keys
dflt = REDIS_CONFIG[:default].symbolize_keys
cnfg = dflt.merge(REDIS_CONFIG[Rails.env.to_sym].symbolize_keys) if REDIS_CONFIG[Rails.env.to_sym]
$redis = Redis.new(cnfg)
Redis::Objects.redis = $redis
@rocLv
rocLv / ai.rb
Last active April 18, 2017 14:59
require 'curb'
require "json"
require "base64"
require "net/http"
require "uri"
CUID = "8132533";
CLIENT_ID = "your_client_id";
CLIENT_SECRET = "your_client_secret";
@rocLv
rocLv / company.rb
Created April 18, 2017 03:54 — forked from davinmsu/company.rb
remove duplicates from activerecord
def self.dedupe
# find all models and group them on keys which should be common
grouped = all.group_by{|model| [model.title,model.info,model.address,model.phone] }
grouped.values.each do |duplicates|
# the first one we want to keep right?
first_one = duplicates.shift # or pop for last one
# if there are any more left, they are duplicates
# so delete all of them
duplicates.each{|double| double.destroy} # duplicates can now be destroyed
end
@rocLv
rocLv / custom_logger.rb
Created April 7, 2017 10:34 — forked from kinopyo/custom_logger.rb
Custom logger file in Rails
# lib/custom_logger.rb
class CustomLogger < Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
end
end
logfile = File.open("#{Rails.root}/log/custom.log", 'a') # create log file
logfile.sync = true # automatically flushes data to file
CUSTOM_LOGGER = CustomLogger.new(logfile) # constant accessible anywhere
@rocLv
rocLv / 0_reuse_code.js
Created March 31, 2017 08:19
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
Rails 4 - Respond only to JSON and not HTML
I believe there are 2 parts here:
1) json only requests in rails
2) json only responses in rails
1) Configure your application controller to ensure json requests only
# app/controller/application_controller.rb
before_action :ensure_json_request
require_relative 'boot'
require 'csv'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Giant
class Application < Rails::Application
@rocLv
rocLv / upgrade_pg.sh
Last active December 12, 2018 02:44 — forked from edib/upgrade_pg.sh
Upgrade PostgreSQL 9.3 to 9.6 on Ubuntu 16.04
Create the file /etc/apt/sources.list.d/pgdg.list, and add a line for the repository
deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6 postgresql-server-dev-9.6 postgresql-contrib-9.6 -y
sudo su - postgres -c "psql template1 -p 5433 -c 'CREATE EXTENSION IF NOT EXISTS hstore;'"
You can specify an explicit logger in your config;
my_logger = ActiveSupport::BufferedLogger.new(path)
config.logger = my_logger
This will replace Rails.logger
@rocLv
rocLv / model.rb
Last active March 14, 2017 08:58
# lib/templates/active_record/model/model.rb
<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
# keep the default scope first (if any)
# constants come up next
# afterwards we put attr related macros
# followed by association macros