Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@nikhgupta
nikhgupta / application_presenter.rb
Last active March 14, 2023 21:17
Rails PORO Decorator - Supports ActiveAdmin
class ApplicationPresenter
attr_reader :model
delegate_missing_to :@model
delegate :helpers, to: 'ActionController::Base'
delegate :url_helpers, to: 'Rails.application.routes'
delegate :to_param, :to_json, :to_query, :to_yaml, :to_enum, to: :@model
delegate :model_name, :model_name_const, :model_name_underscore, to: :class
def initialize(model)
@BrianSigafoos
BrianSigafoos / README.md
Last active February 23, 2023 19:58
Rails module to manage Stripe subscriptions from Stripe Checkout

Stripe CLI

  • Install the Stripe CLI with: brew install stripe/stripe-cli/stripe
  • Login to our Stripe account: stripe login
  • Listen for Stripe webhooks using Latest API version and forward to:
    • yarn stripe:listen, which does:
    • stripe listen --latest --forward-to http://localhost:3000/webhook_events/stripe
  • Replay events locally with stripe trigger <event type>:
  • stripe trigger checkout.session.completed
@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",
@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|
{