Skip to content

Instantly share code, notes, and snippets.

View kivanio's full-sized avatar
🏠
Working from home

Kivanio Barbosa kivanio

🏠
Working from home
View GitHub Profile
require 'active_support/core_ext/time'
class BusinessHoursFeature
# This feature is only available between the hours of 10am and 4pm
def enabled?
Time.use_zone('Pacific Time (US & Canada)') do
now = Time.zone.now
am, pm = Time.zone.parse('10:00'), Time.zone.parse('16:00')
weekday = !(now.saturday? || now.sunday?)
@kivanio
kivanio / Utf8Sanitizer.rb
Created March 29, 2014 21:28
rack Utf8Sanitizer
#http://dev.mensfeld.pl/2014/03/rack-argument-error-invalid-byte-sequence-in-utf-8/
class Utf8Sanitizer
SANITIZE_ENV_KEYS = %w(
HTTP_REFERER
PATH_INFO
REQUEST_URI
REQUEST_PATH
QUERY_STRING
)
module Reportable
extend ActiveSupport::Concern
module ClassMethods
# Chain on a scope and specify the fields to extract.
# Example:
# User.enabled.report %{
# :email_opt_in,
# 'created_at as sign_up_date'
# }

Micro Tooltip

Things it does:

  • a tooltip on hover
  • hide on mouseout
  • hide if you hover the tip itself
  • unobtruscive javascurpt

Configuration:

# app/models/post.rb
class Post
searchable :auto_index => false, :auto_remove => false do
text :title
text :body
end
after_commit :sidekiq_solr_update, :if => :persisted?
class DailyWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
# Daily at midnight
recurrence { daily }
def perform
User.includes(:profile).where("profiles.daily_email" => true) do |u|
ReminderMailer.remind_email(u).deliver
module SidekiqIndexing
module SearchableOverride
extend ActiveSupport::Concern
module InstanceOverrides
def solr_index
SunspotIndexer.perform_async(self.class.to_s, self.id)
end
end
# A Sidekiq background job worker tha knows how to render Rails views
# Don't worry, I *think* I know what I'm doing ;)
class RenderWorker < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Logger # dependency from actionview calling logging
include Rails.application.routes.url_helpers
include Sidekiq::Worker
append_view_path "#{Rails.root}/app/views" # from AbstractController::ViewPaths
class SalesReportWorker < ApplicationController
include Sidekiq::Worker
sidekiq_options backtrace: true, retry: false
self.view_paths = "app/views" # Where to look for view templates
def perform(request_ident, params)
@_response = ActionDispatch::Response.new # Needed to avoid "ActionController::RackDelegation#content_type
# delegated to @_response.content_type, but @_response is nil" error
...
/* How to calculate postgreSQL database size in disk ? */
SELECT pg_size_pretty(pg_database_size('thedbname'));
/* Calculate size of a table including or excluding the index */
SELECT pg_size_pretty(pg_total_relation_size('big_table'));
SELECT pg_size_pretty(pg_relation_size('big_table')); /* without index */
/* See indexes on a table with `\d tablename` */