Skip to content

Instantly share code, notes, and snippets.

@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 / Capybara_attach_file.rb
Created January 12, 2023 20:40
Capybara_attach_file
If someone (like me) finds this later, I was able to achieve this without adding this helper, and instead having capybara attach like so:
attach_file(@basic_file, class: 'dz-hidden-input', make_visible: true)
Found via Stack Overflow, here: https://stackoverflow.com/questions/16722291/how-to-test-dropzone-js-upload-with-rails-cucumber-and-capybara
@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'}
@harssh
harssh / company.rb
Created November 3, 2022 16:02 — 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
@harssh
harssh / deployUser_config.md
Last active September 6, 2022 13:36 — forked from learncodeacademy/deployUser.md
Adding a deploy user in Linux with some less headache goodies (not typing or using password)

(wherever it says url.com, use your server's domain or IP)

Login to new server as root, then add a deploy user

sudo useradd --create-home -s /bin/bash deploy
sudo adduser deploy sudo
sudo passwd deploy

And Update the new password