Skip to content

Instantly share code, notes, and snippets.

View blocknotes's full-sized avatar
🎲
Why choose when I can launch a D6?

Mattia Roccoberton blocknotes

🎲
Why choose when I can launch a D6?
View GitHub Profile
@blocknotes
blocknotes / elixir_if.rb
Last active December 18, 2022 21:41
Elixir like if
def if(check, do:, else: nil)
result = check ? :do : :else
value = binding.local_variable_get(result)
value.is_a?(Proc) ? value.call : value
end
## Without block execution:
# def if(check, do:, else: nil)
# binding.local_variable_get(check ? :do : :else)
# end
@blocknotes
blocknotes / test1.rb
Created October 6, 2022 06:52
Some random experiments
class Player
def initialize(name)
puts "-1- self.name object_id => #{self.name.object_id}\n\n"
puts '[initialize] self.name = name'
self.name = name
puts "-2- @name object_id => #{@name.object_id}, self.name object_id => #{self.name.object_id}\n\n"
puts '[initialize] @name = name'
@name = name
@blocknotes
blocknotes / 20220909010101_create_price_lists.rb
Last active September 12, 2022 18:55
Rails simple versioning
# db/migrate/20220909010101_create_price_lists.rb
class CreatePriceLists < ActiveRecord::Migration[7.0]
def change
create_table :price_lists do |t|
t.string :name, null: false
t.integer :version, null: false, default: 1
t.references :source_price_list
t.jsonb :price_items_changes
@blocknotes
blocknotes / load_samples.rb
Created August 11, 2021 16:16
For development: method to load some records for debugging
# Creates sample methods to fetch single records - it can be useful for debugging, to skip some typing :)
#
# @param mode [Symbol] Load the first|last|random records
# @param models [Array] Models to fetch from
#
# @return [Array] the available sample methods
def load_samples(mode: :first, models: [Model1, Model2, Model3])
puts "> load_samples options => mode: :#{mode} (first|last|random)"
log_level = Rails.logger.level
Rails.logger.level = :warn
@blocknotes
blocknotes / setup.md
Last active February 27, 2024 07:11
ActiveAdmin Trix editor setup (tested with Rails 6.0.3.3)
  • Setup the Rails project with ActiveAdmin using Webpacker: rails g active_admin:install --use_webpacker
  • Setup Trix editor for ActiveAdmin:
    • Execute: bin/rails action_text:install
    • Add Javascript library to app/javascript/packs/active_admin.js:
require("trix")
require("@rails/actiontext")
  • Add style library to app/javascript/stylesheets/active_admin.scss:
@blocknotes
blocknotes / flatten_obj.rb
Created July 2, 2020 09:42
Flatten recursively an hash / array of nested hashes / arrays
def flatten_obj(obj, keys = [], values = {})
if obj.is_a?(Array)
obj.each_with_index do |value, index|
temp_keys = keys + [index]
if value.is_a?(Array) || value.is_a?(Hash)
flatten_obj(value, temp_keys, values)
else
values[temp_keys.join('.')] = value
end
end
# Search SEARCH_WORD in all ruby files of a project (gems included) - using ag - The Silver Searcher
ag SEARCH_WORD --ruby `bundle list --paths | grep '^/'`
# Search SEARCH_WORD in all javascript files of a project (gems included)
ag SEARCH_WORD --js `bundle list --paths | grep '^/'`
# ---
# Old version
ag SEARCH_WORD --ruby `bundle show --paths | grep '^/'`
@blocknotes
blocknotes / specs.sh
Last active July 27, 2021 12:15
Find specs in a commits range - Execute them
# All specs in the diff commits from develop (useful to check a specific branch):
git diff-tree --no-commit-id --name-only -r develop..HEAD | grep '_spec\.rb' | sort | uniq > /tmp/specs
# All specs in a specific range of commits:
git diff-tree --no-commit-id --name-only -r HEAD~3..HEAD | grep '_spec\.rb' | sort | uniq > /tmp/specs
# Execute them:
while read p; do; if [ -f "$p" ]; then; be rspec "$p" || break; fi; done </tmp/specs
@blocknotes
blocknotes / aa_random_stuff.md
Created August 23, 2017 06:39
ActiveAdmin random stuff
  • To ignore ActiveAdmin exceptions while migrating: update routes.rb: ActiveAdmin.routes(self) rescue ActiveAdmin::DatabaseHitDuringLoad
@blocknotes
blocknotes / git_grep_files.rb
Last active August 14, 2017 16:10
Ruby - Access to Git repositories
# Look for a specific string (optionally filtering files with a pattern) of my projects
require 'git'
require 'pathname'
BASE_PATH='/projects'
search='class_eval'
filter_files='*.rb' # Empty for every file
Pathname.new( BASE_PATH ).children.each do |path|