Skip to content

Instantly share code, notes, and snippets.

@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

@harssh
harssh / Floating_Labels_with_Tailwind_CSS.html
Created July 22, 2022 16:28
Floating Labels with Tailwind CSS
<div class="selection:bg-rose-500 selection:text-white">
<div class="min-h-screen bg-rose-100 flex justify-center items-center">
<div class="p-8 flex-1">
<div class="w-80 bg-white rounded-3xl mx-auto overflow-hidden shadow-xl">
<div class="relative h-48 bg-rose-500 rounded-bl-4xl">
<svg class="absolute bottom-0" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320">
<path fill="#ffffff" fill-opacity="1" d="M0,64L48,80C96,96,192,128,288,128C384,128,480,96,576,85.3C672,75,768,85,864,122.7C960,160,1056,224,1152,245.3C1248,267,1344,245,1392,234.7L1440,224L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path>
</svg>
</div>
<div class="px-10 pt-4 pb-8 bg-white rounded-tr-4xl">
@harssh
harssh / create_gemset
Created July 14, 2022 23:26 — forked from rab1234/create_gemset
rails_prep.rb - a ruby script to prepare a new rails application
#!/bin/bash
#
# I use this bash script to create an rvm gemset for a new rails app
# and I call this script from my rails_prep.rb ruby script
#
echo "create new gemset for new rails application ..."
#rvm --create 1.9.2@$1 # <--> this instruction does not work, but the following does
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" && rvm --create @$1
# -------------------------------
# Use the gemset we just created