Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@IanVaughan
IanVaughan / unused_routes.rb
Created August 27, 2020 11:15
Find unused rails routes
# frozen_string_literal: true
Rails.application.eager_load!
unused_routes = {}
# Iterating over all non-empty routes from RouteSet
Rails.application.routes.routes.map(&:requirements).reject(&:empty?).each do |route|
name = route[:controller].camelcase
next if name.start_with?('Rails')
@pashagray
pashagray / pubsub.rb
Created July 16, 2020 06:31
Simple example of pubsub pattern
module Publisher
def subscribe(subscribers)
@subscribers ||= [] # if @subscribers is nil, we initialize it as empty array, else we do nothing
@subscribers += subscribers
end
def broadcast(event, *payload)
@subscribers ||= [] # @subscribers is nil, we can't do each on it
@subscribers.each do |subscriber|
# If event is :item_added occured with payload item itself
@tangentus
tangentus / some_view.html.erb
Last active March 31, 2023 19:26
A PoC of a Stimulus ViewComponent
<%= render StimulusComponent.new controller: "test" do |component| %>
<button <%= component.action :click, :test, :test %>>Hello, Stimulus Component!</button>
<% end %>
require "open-uri"
module Imports
class PageCrawler
attr_reader :current_page, :pages, :crawled
attr_accessor :follow_patterns, :ignore_patterns
def initialize(start_url, max_depth: 3, max_pages: 1000)
@max_depth, @max_pages = max_depth, max_pages
@sakkas-zendesk
sakkas-zendesk / README.md
Last active June 28, 2023 20:24
Manage your symlinks in your dotfiles or anywhere [Ruby]

How to

You might need to modify default path for your symlink sources and destinations as per your preferences.

# Where we keep the original files which we track in Git
SRC_BASE = HOME + "/.dotfiles/symlinker/files/"
# Where we place them by default, if there is no override
DST_BASE = HOME + "/"
@muhammadyana
muhammadyana / rails-softdeleteable.rb
Created May 9, 2020 07:25
Ruby on Rails Soft Deleteable
module SoftDeletable
extend ActiveSupport::Concern
included do
scope :deleted, ->{ where.not(deleted_at: nil) }
scope :without_deleted, ->{ where(deleted_at: nil) }
scope :with_deleted, ->{ unscope(where: :deleted_at) }
default_scope { without_deleted }
end
@dhh
dhh / tracker_blocking.rb
Last active June 30, 2024 14:35
Current list of spy pixels named'n'shamed in HEY, as of April 23, 2020
module Entry::TrackerBlocking
extend ActiveSupport::Concern
included do
has_many :blocked_trackers
end
email_service_blockers = {
"ActiveCampaign" => /lt\.php(.*)?l\=open/,
"AWeber" => "openrate.aweber.com",
@icculus
icculus / main.rb
Created March 24, 2020 07:22
Tetris in DragonRuby, code at the end of Part 2
$gtk.reset
class TetrisGame
def initialize args
@args = args
@next_piece = nil
@next_move = 30
@score = 0
@gameover = false
@grid_w = 10
@amirrajan
amirrajan / main.rb
Created February 29, 2020 21:56
Ruby function to convert hex to RGB without using regex.
class String
def color
if self.length != 6 && self.length != 8
raise "Color must be in the format rrggbb or rrggbbaa. This is what you provided #{self}."
end
if self.length == 6
working_number = 0xff000000 + self.to_i(16)
else
working_number = [*self[-2..-1], *self[0..5]].join.to_i(16)
end
@amirrajan
amirrajan / main.rb
Created January 6, 2020 00:52
DragonRuby Mode 7
def defaults args
args.outputs.static_background_color = [0, 0, 0]
args.grid.origin_center!
args.state.texture_size = 64
args.state.texture_path = 'sprites/square-orange.png'
return if args.state.squares
cols = args.grid.w.idiv(args.state.texture_size) + 2
rows = args.grid.h.half.idiv(args.state.texture_size) * 2
args.state.squares = cols.numbers.product(rows.numbers).map do |x, y|
{