Skip to content

Instantly share code, notes, and snippets.

View stevegeek's full-sized avatar
🐝

Stephen Ierodiaconou stevegeek

🐝
View GitHub Profile
@stevegeek
stevegeek / sync_from_ismartcoding.rake
Last active February 12, 2024 14:38
If you are developing with maybe-finance/maybe and dont want to create a paid account with openexchangerate.org, use could use this
require "bigdecimal"
namespace :exchange_rates do
desc "Fetch exchange rates from https://github.com/ismartcoding/currency-api"
task sync_from_ismartcoding: :environment do
# Exchange rate data is simply stored as JSON files in a repository:
# https://raw.githubusercontent.com/ismartcoding/currency-api/main/{yyyy-mm-dd}/{24h hour}.json
FIRST_DATE = "2023-10-10" # First date that has completed data
DEFAULT_HOUR = "23" # Reference hour of the day to take exchange rates from
PRECISION = 6 # 6 decimal places
require "benchmark/ips"
classes = ["items-center", "justify-center", "px-4", "py-2", "text-sm", "font-medium", "border", "border-slate-300", "shadow-sm", "rounded-md", "focus:outline-none", "focus:ring-offset-2", "text-white", "bg-blue-600", "ring-blue-700", "hover:bg-blue-700", "focus:ring-offset-blue-50", "dark:border-slate-950", "dark:bg-blue-700", "dark:text-blue-50", "dark:ring-blue-950", "dark:hover:bg-blue-800", "pl-1", "pr-4", "dark:focus:ring-offset-blue-700"]
merger = TailwindMerge::Merger.new
Benchmark.ips do |x|
x.report("init new merger") { TailwindMerge::Merger.new.merge(classes.shuffle.join(" ")) }
x.report("reuse with cache") { merger.merge(classes.shuffle.join(" ")) }
x.compare!
end
@stevegeek
stevegeek / action_policy_authorization_client.rb
Last active September 25, 2023 15:27
Avo Authorization client for`action_policy`
# frozen_string_literal: true
module Avo
class ActionPolicyAuthorizationClient
include ActionPolicy::Behaviour
def authorize(user, record, action, policy_class: nil)
authorize!(record, to: action, with: policy_class, context: {user: user})
rescue ActionPolicy::NotFound => error
raise NoPolicyError.new error.message
@stevegeek
stevegeek / Rakefile
Last active June 30, 2022 09:38
Use `npm` instead of `yarn` with `cssbundling-rails` and `jsbundling-rails` - Add the following to your `Rakefile`
require_relative "config/application"
Rails.application.load_tasks
# Overide the default Rake task extensions for assets:precompile from cssbundling-rails and jsbundling-rails.
# This is necessary because the default Rake task will try use yarn, but we want to use npm.
Rake::Task["css:build"].clear
Rake::Task["javascript:build"].clear
namespace :css do
@stevegeek
stevegeek / skewed-box.css
Last active January 31, 2022 10:15
CSS and HTML to create a box with a skewed background
.skewed-box__container {
position: relative;
/* Customise accordingly: */
max-width: 10rem;
}
.skewed-box__text {
/* Customise accordingly: */
color: white;
@stevegeek
stevegeek / ilo-time.js
Last active April 7, 2021 05:24
Paste into your browser JS console on the 'Tweets' page of https://ilo.so
// Metrics normalised by number of tweets over time periods of day
// `bucket` controls the hours over which data is binned, eg 4 = creates 4 hour windows
((bucket = 4) => {
const columns = {'Impressions': 3, 'Engagements': 4, 'Likes': 6, 'Profile clicks': 10, 'EPI': 5}
Object.entries(columns).forEach(([name, idx]) => {
const h = Array(Math.ceil(24 / bucket)).fill(0).map(() => [0, 0]);
[
...document.querySelectorAll('tbody tr')
].map(
(e) => [
# frozen_string_literal: true
# copyright: Stephen Ierodiaconou 2020
# Usage from console:
# require './no_autoload/migrators/change_acl'
# eg: Migrators::ChangeAcl.new.run(UserProfile, :profile_image)
module Migrators
class ChangeAcl
def run(model_klass, association_name)
@stevegeek
stevegeek / spec_helper.rb
Last active December 27, 2019 13:07
Check where FactoryBot records are being created in specs, Inspired by https://thoughtbot.com/blog/debugging-why-your-specs-have-slowed-down . See description in source
RSpec.configure do |config|
# Check where records are being created
# Inspired by https://thoughtbot.com/blog/debugging-why-your-specs-have-slowed-down
# This is best used by modifying Factory bot to return the `caller` call stack
# in factory_bot/factory_runner.rb , edit `instrumentation_payload` hash to include `caller: caller,`
record_counts = {}
config.before(:suite) do
ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |name, start, finish, id, payload|
strategy = payload[:strategy].to_s