Skip to content

Instantly share code, notes, and snippets.

@harssh
harssh / hash_except.md
Created August 2, 2023 17:31 — forked from O-I/hash_except.md
[TIx 4] Remove key-value pairs from a hash and return the hash

Note: As of Ruby 3.0.0, Hash#except is now [part][1] of the language. However, Ruby does not implement Hash#except!.

Sometimes I want to remove a specific key-value pair from a Ruby hash and get the resulting hash back. If you're using Rails or ActiveSupport you can accomplish this using Hash#except:

hash = { a: 1, b: 2, c: 3 }
hash.except(:a)     # => { b: 2, c: 3 }

# note, the original hash is not modified
hash # => { a: 1, b: 2, c: 3 }
@harssh
harssh / ruby_ocr.rb
Created April 27, 2023 02:28 — forked from dshorthouse/ruby_ocr.rb
OCR Image-based PDF in ruby
require 'parallel'
require 'rtesseract'
require 'mini_magick'
source = "/MyDirectory/my.pdf"
doc = {}
pdf = MiniMagick::Image.open(source)
Parallel.map(pdf.pages.each_with_index, in_threads: 8) do |page, idx|
tmpfile = Tempfile.new(['', '.tif'])
MiniMagick::Tool::Convert.new do |convert|
@harssh
harssh / user_observer.rb
Created March 27, 2023 17:45 — forked from jhjguxin/user_observer.rb
cache devise's current_user method, speed up page's load, used for guanxi_cms, Suitable for devise 2.x - 3.x, Rails 3.x
class UserObserver < Mongoid::Observer
def after_save(record)
expire_cache(record)
end
private
def expire_cache(record)
Rails.cache.delete("user:#{record.id}")
end
@harssh
harssh / clean_code.rake
Created February 23, 2023 03:42 — forked from thorstenspringhart/clean_code.rake
extended rake tasks to run rubocop only on uncommitted files / diff with master or entire project
namespace :clean_code do
# rubcop only available in dev
if Rails.env.development?
require 'rubocop/rake_task'
def diff_files
cmd = %q( git diff --name-only --diff-filter=ACMRTUXB \
$(git merge-base HEAD origin/master) \
| egrep '\.rake$|\.rb$' )
diff = `#{cmd}`
diff.split("\n")
@harssh
harssh / git diff comma separated files.sh
Created February 23, 2023 01:17 — forked from marcelblijleven/git diff comma separated files.sh
Comma separated files with git diff
git --no-pager diff --name-only develop | tr '\n' ',' | sed 's/\(.*\),/\1 /'
@harssh
harssh / query.rb
Created February 15, 2023 14:35 — forked from mamantoha/query.rb
PostgreSQL: query to select records from last week on weekdays between 9:00 and 18:00
Model
.where(
"EXTRACT(dow FROM log_in) IN (1,2,3,4,5)"
)
.where(
"log_in::time BETWEEN '9:00' AND '18:00'"
)
.where(
"log_in BETWEEN now()::timestamp - (interval '1 week' AND now()::timestamp)"
)
@harssh
harssh / experience.rb
Created February 15, 2023 03:56 — forked from mamantoha/experience.rb
Rails API Filtering and Sorting
# app/models/experience.rb
#
# == Schema Information
#
# Table name: experiences
#
# id :integer not null, primary key
# title :string
# description :text
# created_at :datetime not null
@harssh
harssh / pr-checklist.md
Created February 13, 2023 15:40 — forked from katyhuff/pr-checklist.md
Pull Request Review Checklist
  • Read the PR description
  • Read through all the changes, considering the following questions.
    • Is there anything you can praise about this PR? Start with praise.
    • Are variable names brief but descriptive?
    • Are new/changed functions no longer than a paragraph?
    • Do all function parameters have default values where appropriate?
    • Is the code clear and clean? (see Robert C. Martin's Clean Code)
    • Is there enough documentation?
  • Does the programming style meet the requirements of the repository (PEP8 for python, google for c++, etc.)
@harssh
harssh / gist:3d70c2bcc944e6423d4a790c70316719
Created January 17, 2023 15:54 — forked from cdale77/gist:d8502ef11373862f8eaa
Recursively copy and rename files with Ruby
#!/usr/bin/env ruby
require 'fileutils'
files = Dir["*/*.PDF"].collect{|f| File.expand_path(f)}
files.each_with_index do |file, index|
puts "copying file #{index}"
FileUtils.cp file, "pdf/#{index}.pdf"
end
@harssh
harssh / gist:22cd4ec60e6c28541a18e4d1490b6682
Created January 12, 2023 20:38 — forked from kyleroberthammond/gist:a544e0a4ee5f6380b6a22698fb585ba6
Test Dropzone.js File Uploads With Capybara
Test Dropzone.js File Uploads With Capybara
Dec 29, 2014
Integration tests involving complex JS interactions are a pain, especially with something like drag and drop file uploads (you can’t exactly drag something into a headless browser). My recent use case was an AngularJS application using Dropzone.js with Ruby on Rails on the backend, so I needed a way to make Dropzone play nicely with Capybara Webkit. Here is the spec helper I ended up with:
def drop_in_dropzone(file_path)
# Generate a fake input selector
page.execute_script <<-JS
fakeFileInput = window.$('<input/>').attr(
{id: 'fakeFileInput', type:'file'}