Skip to content

Instantly share code, notes, and snippets.

View garyharan's full-sized avatar
🎯
Focusing

Gary Haran garyharan

🎯
Focusing
View GitHub Profile
@garyharan
garyharan / _form.html.erb
Last active December 2, 2023 22:16
search select
<%= form_tag home_search_path, method: :get, data: { controller: "search" } do |form| %>
<div class="relative">
<div class="absolute inset-y-0 start-0 flex items-center ps-3 pointer-events-none">
<svg class="w-4 h-4 text-gray-500 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
</svg>
</div>
<%= text_field_tag :q, params[:q], data: { "search-target": "query" }, class: "block w-full p-4 ps-10 text-sm text-gray-900 border border-gray-300 rounded-t-lg rounded-b-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500", placeholder: "Search", required: true %>
@garyharan
garyharan / best_practices.md
Created November 3, 2023 12:45
Ways to improve your coding practices

Best Practices when coding

Bring display logic down the line when you can

If your template has lots of ifs about what kind of models you have you may want to generisize the display portion.

One example is that instead of this:

class Car
@garyharan
garyharan / java_installs.md
Created June 29, 2023 19:53
Figure out what versions of Java are installed on my Mac

Run the following: mdfind -name 'java' | grep '/bin/java$'

@garyharan
garyharan / back_to_the_tab.md
Last active March 1, 2023 18:02
RFC How to reopen existing tab

We need a way back to the tab when resetting passwords or getting a token from email or second factor authentication.

Too often we open a bunch of tabs and forget which one was open.

Imagine if we could create a token that would reopen the given tab that initiated the exit.

window.security.tabIdToken // returns: 35c7b72b-00bd-4734-81f7-318392f52370
@garyharan
garyharan / installation.md
Created February 1, 2023 14:49
How to install turbo-ios

Installing Turbo iOS on a Swift app

Add turbo-ios package

File > Add Packages > Paste in 'https://github.com/hotwired/turbo-ios/' > Add

Enable Cocoapods

First run sudo gem install cocoapods Then touch Podfile

@garyharan
garyharan / test_helper.rb
Created December 27, 2022 16:16
How to test using capybara in latest rails and chrome
# as per https://rubydoc.info/github/teamcapybara/capybara/master#using-capybara-with-minitest
require 'capybara/rails'
require 'capybara/minitest'
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Make `assert_*` methods behave like Minitest assertions
include Capybara::Minitest::Assertions
@garyharan
garyharan / height_difference.md
Created December 19, 2022 17:01
ActiveRecord order by SQL generated value

If you want to create an extra attribute based on SQL query with ActiveRecord you can use the following:

@games = Game.select("*, ABS(#{@profile.level} - level) AS level_difference")

Then in your Ruby code you can refer to things this way:

@games.first.level_difference # =&gt; 16
@garyharan
garyharan / story_time_classy_switch_cases.rb
Last active May 12, 2022 16:05
Using a class comparison in a switch case condition
# Story time: Classy Switch Classes
# Say you have a bunch of messages coming in Slack and you'd like to add an automatic emoji
# of course your team is multicultural and you may want to cater to the diverse crowd you have.
# For the purpose of this exercise we will treat `message#emojis` and `message#replies` as arrays we can push to.
# You could write your code like this:
if message.text.include?("Good morning!")
@garyharan
garyharan / experience.txt
Last active June 7, 2021 14:05
Experience and diversity have value
A friend of mine, approaching 50, works as a programmer in a medium sized consultancy. In a recent conversation he told me how he felt like he was feeling older now that they had hired many 20-somethings in his company. The younger developers, he said, would do things faster than he would and it started to look like he was just a slower developer than the more youthful new hires. He was wondering if his days were numbered.
He was resigning himself to the idea that he was experiencing some kind of decline of his faculties if he compared himself to the more youthful years of his career. However this wasn’t the only conversation I had about the work at his company. The conversation before that one had him put the finger on something crucial.
So I had a conversation with him about it and we connected the dots.
Me: What was the password story they tackled?
Him: A password reset.
Me: And what was wrong with how they implemented it.
Him: Well the user typed their email in and then if they click the “forgot passw
@garyharan
garyharan / fix_mac_audio.sh
Created March 17, 2020 20:02
Restart all core audio when airpods do not work
# Instead of rebooting system
# If audio has half going through speakers and half going through headphones.
ps aux | grep 'coreaudio[d]' | awk '{print $2}' | xargs sudo kill