Skip to content

Instantly share code, notes, and snippets.

View RichOrElse's full-sized avatar

Ritchie Paul Buitre RichOrElse

View GitHub Profile
@RichOrElse
RichOrElse / application_record.rb
Last active January 9, 2024 11:36
Faster SQL search with Union operation in Ruby on Rails
class ApplicationRecord
# ...
scope :union, ->(*others) { from_unions(self, *others) }
def self.from_unions(*relations)
subquery = relations.map(&:arel).inject(:union)
unscoped.from subquery.as(table_name)
end
# ...
end
@RichOrElse
RichOrElse / application_record.rb
Last active August 24, 2023 03:47
ActiveRecord missing methods
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
scope :subquery_as, ->(name) { unscoped.from(arel.as(name)) }
scope :select_with, ->(*fields) { dup.select_with!(*fields) }
scope :select_with!, ->(*fields) { self.select_values |= fields.flatten | [table[Arel.star]]; self }
end
@RichOrElse
RichOrElse / .railsrc
Last active January 18, 2023 12:13
Rails 7 app template
-a propshaft
--skip-jbuilder
--skip-helper
--skip-test
--template=https://gist.githubusercontent.com/RichOrElse/9fada3065b1e8d1a47b5e16aab482f09/raw/dfb8b7801fdee97037b4ff426e47f3e4af52ef4a/app_template.rb
module Runner
def initialize(**data)
end
def run(klass)
klass.new.process
end
def call
process
@RichOrElse
RichOrElse / delegate_to.rb
Last active September 20, 2020 15:17
DelegateTo
class DelegateTo < BasicObject
include ::Kernel
def initialize(object)
@delegate_to = object
end
def inspect
"#{self.class.inspect}[#{@delegate_to}]"
end
@RichOrElse
RichOrElse / application.css
Last active September 8, 2020 02:46
CSS & HTML examples
img {
max-width: 100%
height: auto;
}
@RichOrElse
RichOrElse / Gemfile
Created September 6, 2020 09:46
Webtest configuration
gem 'vcr', '~> 6.0'
gem 'webmock', '~> 3.8', '>= 3.8.3'
gem 'webdrivers', '~> 4.0'
@RichOrElse
RichOrElse / application_record.rb
Last active July 4, 2020 04:31
Active Record extensions
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.inherited(child_class)
super
child_class.const_get('ActiveRecord_Relation').include(RelationMethods)
end
end
@RichOrElse
RichOrElse / function_for.rb
Last active March 15, 2020 16:18
Functional Programming in Rails
# turns methods into functions
class FunctionFor < Module
def initialize(*method_names)
@method_names = method_names.each { |named| override named }
end
def override(named)
define_method(named) do |*input, &block|
return function_for[named] if input.none? && block.nil?
@RichOrElse
RichOrElse / migration_reversible.rb
Created February 27, 2020 02:02
DB migration helper
module MigrationReversible
def remove_existing_index(table_name, column_names, **options)
index_name = options[:name]
exists = suppress_messages { index_exists?(table_name, column_names, name: index_name) }
reversible do |dir|
dir.up { remove_index(table_name, name: index_name) if exists }
dir.down do
if exists
say "existing index #{index_name.inspect} on table #{table_name.inspect}"