Skip to content

Instantly share code, notes, and snippets.

View leandro's full-sized avatar
🏠
Working from home

Leandro Camargo leandro

🏠
Working from home
View GitHub Profile
@leandro
leandro / versatile_methods.rb
Created April 13, 2023 00:23
A different approach, compared to using `Module#module_function`
# The idea here is to make all the methods below accessible through the module
# itself and through the classes including the module, but having the methods
# publicly available both as instance and as class methods.
module A
def self.abc = 123
def self.xyz = 456
def self.append_features(klass)
methods_being_added = singleton_methods - [:append_features]
delegation_setter = ->(mod, methods) { delegate *methods, to: mod }
@leandro
leandro / lib__graphql__utils.rb
Created April 5, 2023 13:28
Retrieving the queried fields from a GraphQL operation request (when using `graphql-ruby` gem)
module Graphql
class Utils
CONTEXT_CLASS = GraphQL::Query::Context
FIELD_CLASS = GraphQL::Language::Nodes::Field
FRAGMENT_CLASS = GraphQL::Language::Nodes::FragmentSpread
# Given a GraphQL context (literally, the +context+ method from inside any GraphQL field
# method), this method will return all the fields requested in the operation request.
# So, considering the following query example:
#
@leandro
leandro / app__lib__importer__preloadable.rb
Created March 4, 2023 21:16
Dirty graceful method overriding approach
# frozen_string_literal: true
module Importer
module Preloadable
extend ActiveSupport::Concern
included do
delegate :preloader_settings, to: :class
delegate :preloaded_records_manager, to: :import_service
@leandro
leandro / app__lib__string_utils.rb
Last active February 15, 2023 17:11
HTML based string into human friendly based plain text string
module StringUtils
CONTAINS_HTML_TAG_REGEX = /<[a-z][a-z0-9-]*?[^>]*?>/im
SELF_CLOSING_TAGS = %w[br hr img wbr].freeze
SPACE_REGEX = /^(?:\R|\s)$/m
module_function
# This method aims to convert HTML based strings into human friendly plain
# text strings, where paragraphs represented by <p> tag wrapped text chunks
# are replaced by their contents succeeded by two line break characters
@leandro
leandro / application_controller.rb
Created January 25, 2023 14:25
Injecting controller helper methods inside the helper context in helper methods tests (RSpec)
# frozen_string_literal: true
class ApplicationController < ActionController::Base
helper_method :is_happy?
private
def is_happy? = true
end
@leandro
leandro / db__rakeable_migrations__base.rb
Last active January 10, 2023 17:30
Rakeable migrations experiment
module RakeableMigrations
class Base < ActiveRecord::Migration[6.1]
class << self
def migration_version = @migration_version
def set_migration_version(version) = @migration_version = version.to_s
end
end
end
@leandro
leandro / rails-postgresql-development-on-macos.rb
Created November 17, 2022 14:43
Findings about collation on PostgreSQL + Rails and its varying behaviors between OSs
# ==============================================================================
# On development's rails console (BSD based OS - MacOS):
# ------------------------------------------------------------------------------
a = %Q|SELECT * FROM json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","b":"bar"},{"a":"3","b":"FOO"},{"a":"4","b":"BOring"}]') AS x(a INT, b TEXT) ORDER BY b;|
ActiveRecord::Base.connection.execute(a).to_a
# [
# [0] {
# "a" => 4,
# "b" => "BOring"
# },
@leandro
leandro / file-example.txt
Last active October 17, 2022 21:46
How to iterate a file content by chunks
app/models/branch_action_node.rb:class BranchActionNode < ActionNode
app/models/built_custom_document.rb:class BuiltCustomDocument < CustomDocument
app/models/call_rail_call.rb:class CallRailCall < Call
app/models/custom_booking.rb:class CustomBooking < CustomForm
app/models/custom_booking_action_node.rb:class CustomBookingActionNode < CustomFormActionNode
app/models/custom_document_separator_block.rb:class CustomDocumentSeparatorBlock < CustomDocumentBlock
app/models/custom_field/client.rb:class CustomField::Client < CustomField
app/models/email.rb:class Email < ContactInformation
app/models/event_automation.rb:class EventAutomation < Automation
app/models/event_automation_target.rb:class EventAutomationTarget < AutomationList
@leandro
leandro / config__initializers__sentry.rb
Created September 27, 2022 17:23
Sentry toggling extensiion
require 'sentry_extensions/toggler'
require 'sentry_extensions/configuration'
Sentry.init do |config|
config.dsn = ENV['SENTRY_API_KEY']
config.excluded_exceptions = config.excluded_exceptions - ['ActiveRecord::RecordNotFound']
# This allows us to skip an error to be sent to Sentry, which is useful for
# when we're already manually sending the error to Sentry and then re-raising
# the error.
@leandro
leandro / config__initializers__kernel.rb
Created April 21, 2022 23:45
An improved version of `p` method for Rails.
module Kernel
def log2(*args)
$log_counter ||= 1
options = args.size > 1 && Hash === args.last ? args.pop : {}
stack_lines = options[:stack_lines] || 1
call_lines = caller(1, stack_lines)
stack_line_stripper = lambda do |line|
stack_line = line[/^.+\/gems\/(.+)$/, 1]
stack_line || line.delete_prefix("#{Rails.root}/")