Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@KonnorRogers
KonnorRogers / flash_component.rb
Last active March 31, 2023 19:20
A view component for using Shoelace alerts
class FlashComponent < ViewComponent::Base
extend Dry::Initializer
# notice, warning, and error can be set by default in redirect_to.
# For additional flashes, you must do flash[:type] = "message"
ICONS = {
primary: "info-circle",
success: "check2-circle",
info: "gear",
warning: "exclamation-triangle",
@westonganger
westonganger / Simple Rails Form Object
Last active October 11, 2023 18:23
Simple Rails Form Objects
class Forms::BaseForm
include ActiveModel::Validations
def initialize(attrs={})
attrs ||= {}
attrs.each do |k,v|
self.send("#{k}=", v) ### Use send so that it checks that attr_accessor has already defined the method so its a valid attribute
end
end
@fuksito
fuksito / callable_service.rb
Created August 10, 2021 07:34
CallableService Concern for Rails
# Usually service classes have only one purpose and thus should have only one public method that triggers action
# This module simplifies the common approach:
#
# SomeService.new(a, b, c).call
#
# To:
#
# SomeService.call(a, b, c)
#
# Requirements for class:
@AndreaBarghigiani
AndreaBarghigiani / table_component.html.erb
Created July 30, 2021 06:04
Simple Table with ViewComponents
<table class="<%= classes %>">
<thead class='bg-gray-300 font-medium'>
<% head %>
</thead>
<tbody>
<% body %>
</tbody>
</table>
@dalezak
dalezak / cacheable.rb
Last active March 14, 2023 21:39
Rails Cacheable Concern
# Rails concern that tracks and purges cache keys to avoid using Rails.cache.delete_matched since its very costly on Redis.
#
# @countries = Country.query_cached('countries:index', "limit:#{@limit}", "offset:#{@offset}") do
# query.limit(@limit).offset(@offset).all.to_a
# end
module Cacheable
extend ActiveSupport::Concern
included do
before_save :purge_cache
@existentialmutt
existentialmutt / modal_component.html.erb
Last active March 31, 2023 19:22
Modal ViewComponent
<div class="modal fade" id="<%= dom_id %>" role="dialog" tabindex="-1">
<div class="modal-dialog" role="document">
<div class="modal-content">
<% if header %>
<div class="modal-header">
<%= header %>
<%- if header_close_btn %>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<i class="fa fa-times-circle"></i>
</button>
@amirrajan
amirrajan / main.rb
Last active March 5, 2025 22:40
Gauntlet written in DragonRuby Game Toolkit
class Game
attr_gtk
def tick
defaults
render
input
calc
end
@fractaledmind
fractaledmind / jobbable_concern.rb
Created March 1, 2021 09:09
This concern allows model instances to run async jobs that depend on them. Calling #async_method_name will trigger the MethodNameJob
module Jobbable
extend ActiveSupport::Concern
# This concern allows model instances to run async jobs that depend on them.
# Calling #async_method_name will trigger the MethodNameJob
PREFIX = "async_"
included do
def method_missing(method_name)
#! /usr/bin/env ruby
# adapted from https://gist.github.com/gabehollombe/064a90351c204381832d749ca6b569e0
# but it actually edits a file!
# `editor.rb [filename]`
# arrows, letters, backspace... Just the basics.
require 'curses'
include Curses
include Curses::Key
@songjiz
songjiz / _form.html.erb
Last active August 8, 2023 10:05
Toggle password visibility with stimulus controller
<div class="field" data-controller="toggle-password-visibility" data-toggle-password-visibility-visible-icon-class="mdi-eye-off">
<%= form.label :password, class: 'label'%>
<div class="control">
<div class="field has-addons has-addons-right">
<div class="control is-clearfix has-icons-left has-icons-right">
<span class="icon is-left">
<i class="mdi mdi-onepassword mdi-24px"></i>
</span>
<%= form.password_field :password, class: ['input', { "is-danger": sign_in.errors.key?(:password) }], data: { 'toggle-password-visibility-target': 'password' }, required: true %>
<% if sign_in.errors.key?(:password) %>