Skip to content

Instantly share code, notes, and snippets.

View pedrocarmona's full-sized avatar

Pedro Carmona pedrocarmona

  • Lisbon, Portugal
View GitHub Profile
@pedrocarmona
pedrocarmona / something_lookup.rb
Created March 16, 2021 19:45
Polymorphic lookup - collection
class SomethingLookup < ApplicationRecord
self.table_name = 'something_lookups'
belongs_to :something, polymorphic: true
def self.load_schema!
# rubocop:disable Naming/MemoizedInstanceVariableName
@columns_hash ||= {}
# rubocop:enable Naming/MemoizedInstanceVariableName
end
@pedrocarmona
pedrocarmona / custom_json_formatter.rb
Created June 19, 2020 15:56
Displays tests added in the new PR branch
# frozen_string_literal: true
require "rspec/core/formatters/base_text_formatter"
class CustomJsonFormatter
# This registers the notifications this formatter supports, and tells
# us that this was written against the RSpec 3.x formatter API.
RSpec::Core::Formatters.register self, :example_started
def initialize(output)
@pedrocarmona
pedrocarmona / initials_avatar.rb
Last active December 24, 2022 16:50
Initials avatar - create an image with letters for using as the user avatar.
require "mini_magick"
# require "initials_avatar"
# InitialsAvatar.new(initials: "PC").avatar_path
class InitialsAvatar
BACKGROUND_COLORS = [
"#C53030", "#9C4221", "#975A16", "#2F855A", "#2C7A7B", "#2B6CB0", "#434190", "#553C9A", "#97266D"
]
def initialize(initials:)
@pedrocarmona
pedrocarmona / arel_benchmark.rb
Last active September 24, 2019 14:37
arel_benchmark
require 'benchmark/ips'
ActiveRecord::Base.logger = nil
def pagy_count(collection)
(c = collection.count(:all)).is_a?(Hash) ? c.size : c
end
def pagy_count2(collection)
result = collection.count(:all)
@pedrocarmona
pedrocarmona / time_zone_enum_type.rb
Last active March 8, 2018 11:40
Time Zone Enum Type graphql-ruby
Types::TimeZoneEnumType = GraphQL::EnumType.define do
name "TimeZoneEnum"
ActiveSupport::TimeZone.all.uniq! {|tz| tz.tzinfo.identifier }.map { |tz|
symbol = tz.tzinfo.identifier.gsub(/[^_a-zA-Z0-9]/, '_').squeeze('_').upcase!
value("TZ_#{symbol}", tz.tzinfo.identifier, value: tz.name)
}
end
@pedrocarmona
pedrocarmona / game.rb
Last active January 22, 2016 17:00
game
require 'drawille'
class Ceil < Struct.new(:x, :y, :state)
def alive?
state == 1
end
end