Skip to content

Instantly share code, notes, and snippets.

Avatar

Pedro Carmona pedrocarmona

  • Lisbon, Portugal
View GitHub Profile
@pedrocarmona
pedrocarmona / something_lookup.rb
Created Mar 16, 2021
Polymorphic lookup - collection
View something_lookup.rb
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 Jun 19, 2020
Displays tests added in the new PR branch
View custom_json_formatter.rb
# 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
Created May 23, 2020
Initials avatar - create an image with letters for using as the user avatar.
View initials_avatar.rb
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:)
View arel_benchmark.rb
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 Mar 8, 2018
Time Zone Enum Type graphql-ruby
View time_zone_enum_type.rb
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
View game.rb
require 'drawille'
class Ceil < Struct.new(:x, :y, :state)
def alive?
state == 1
end
end