Skip to content

Instantly share code, notes, and snippets.

@pietromoro
pietromoro / build.bat
Created October 25, 2023 10:16
Simple build script for a c/cpp msvc project without going deep in msvisual studio
@echo off
setlocal EnableDelayedExpansion
REM TODO: Make this better this one, detect vcvarsall for 2019/2022 versions and run accordingly
if not defined DevEnvDir (
call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
)
set BUILD_DIR=build
set SOURCE_DIR=src
@pietromoro
pietromoro / update_dns.sh
Created August 5, 2023 10:21
Update Porkbun DNS via API (effectiverly creating a DDNS)
#!/bin/bash
# Tempfile that contains the last ip set
tempfile=currentip.tmp
touch $tempfile
last_ip=`cat $tempfile`
echo "Last IP: $last_ip"
curl -X GET "https://v4.ident.me" > $tempfile
current_ip=`cat $tempfile`

Keybase proof

I hereby claim:

  • I am pietromoro on github.
  • I am pietromoro (https://keybase.io/pietromoro) on keybase.
  • I have a public key whose fingerprint is 4E9D F2C8 074A 306B 936E C394 AF50 1491 457B 0B4C

To claim this, I am signing this object:

@pietromoro
pietromoro / error_styler.rb
Last active May 2, 2022 16:48
Rails bad input restyle with bootstrap initializer file
# Be sure to restart your server when you modify this file.
# Restyle how errors are shown
ActionView::Base.field_error_proc = proc do |html_tag, instance|
html = %(<div class="field-with-errors">#{html_tag}</div>).html_safe
form_fields = %w[textarea input select]
elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css 'label, ' + form_fields.join(', ')
elements.each do |element|
next if element.attributes.include? "data-disable-error-styling"
@pietromoro
pietromoro / sluggable.rb
Last active May 10, 2023 10:05
Sluggable rails concern: use slugs instead of ids in the url!
module Sluggable
extend ActiveSupport::Concern
cattr_reader :slugging
included do
before_save :generate_slug
def self.sluggable
all.extending(Sluggable::Finder)
end
@pietromoro
pietromoro / enum.rb
Last active July 30, 2020 13:27
Quick ruby enum
module Kernel
def enum(values)
Module.new do |mod|
values.each_with_index{ |v,i| mod.const_set(v.to_s.capitalize, 2**i) }
def mod.inspect
"#{self.name} {#{self.constants.join(', ')}}"
end
end
end
@pietromoro
pietromoro / before_render.rb
Last active September 9, 2024 13:28
BeforeRender Callback as concern for rails controller(s). Use it with the same syntax used for other callbacks, just include in the controller. Fires after action but before rendering happens.
module BeforeRender
extend ActiveSupport::Concern
included do
alias_method :render_without_before_render_action, :render
alias_method :render, :render_with_before_render_action
define_callbacks :render
end
def render_with_before_render_action(*options, &block)