This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationRecord < ActiveRecord::Base | |
self.abstract_class = true | |
unless instance_methods.include?(:values_at) | |
def values_at(*methods) | |
methods.flatten.map! { |method| public_send(method) } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyConfiguration < BasicObject | |
def method_missing name, *args | |
getter = name.end_with?(?=) ? name[0..-2] : name | |
(class << self; self end).class_eval { attr_accessor getter } | |
__send__ name, *args | |
end | |
end | |
config = MyConfiguration.new | |
config.liverpool = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post | |
has_many :comments | |
has_one :latest_comment, -> { latest_by :post_id }, class_name: 'Comment' | |
class Comment | |
scope :latest_by, -> column { | |
from( | |
order(column, created_at: :desc).arel | |
.distinct_on(arel_table[column]) | |
.as(quoted_table_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scope :search_uid, ->(*terms) { | |
where(arel_attribute(:uid).matches_any(terms.map {|term| "%#{sanitize_sql_like term}%" }, nil, true)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module CallbackRegistrationTracing | |
module Capture | |
def normalize_callback_params(*args) | |
super.tap do |_, _, options| | |
options[:_caller] = caller(3) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# In this example we have an application concerned about the length of a string, | |
# and is pushing the work to the database for efficiency. There is an index | |
# on the length of a Post's title, and the scopes and orderings have been | |
# neatly refactored to minimise the amount of Arel required. | |
# | |
# A bug has now arisen in the behaviour of `last`. It is giving the first. | |
# | |
# Subsequent investigation has shown that customers were also complaining that |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class WorkAllocation | |
# Runtime process allocations. Last reviewed: 24/9/2019 | |
# Half a gig system allocation for agents and monitors and, unfortunately, | |
# a couple of deploy-time asset compiles due to dependencies. It's | |
# okay to push some processes into swap on smaller instances during | |
# deploy. Ideally we wouldn't compile on-instance at all. | |
OPS_MEMORY_ALLOWANCE = 512 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/concerns/exceeding.rb | |
module Exceeding | |
extend ActiveSupport::Concern | |
included do | |
scope :exceeds, ->(association, n) do | |
joins(association).group(primary_key).having(arel_table[primary_key].count.gteq n) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/lib/adderall.rb | |
module Adderall | |
def adderall(column_name, relation = self, filter = :itself) | |
owner = proxy_association.owner | |
records = proxy_association.target | |
if loaded? || owner.new_record? | |
relation = none | |
else | |
records = records.select(&:new_record?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ByMost | |
extend ActiveSupport::Concern | |
included do | |
scope :by_most, ->(assoc) { | |
reflection = reflect_on_association(assoc) | |
left_joins(assoc) | |
.group(primary_key) | |
.order(reflection.klass.arel_table[reflection.foreign_key].count.desc) | |
} |