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
# 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 / 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
@blocknotes
blocknotes / setup.md
Last active February 24, 2025 15:40
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 / 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 / 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 / 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 / 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