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 MongoidEmbeddedObjectModification | |
# Custom field behaves more like an immutable value. | |
# Your changes to the object obtained with getter method will not take an effect unless you assign it back. | |
# https://github.com/mongoid/mongoid/issues/3341 | |
def modify(mongoid_doc, field_name) | |
obj = mongoid_doc.public_send(field_name) | |
yield obj | |
mongoid_doc.public_send("#{field_name}=", obj) | |
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
module ActiveRecordRelationExists | |
# DANGER this switches ActiveRecord::Relation#exists from delegation to arel to our implementation that returns a String with a twist. | |
# This achieves the most common usage of #exists before 4.2. Proper fix will be: https://github.com/rails/rails/issues/16959 | |
def exists | |
SqlStringWithNot.new("EXISTS (#{self.to_sql})") | |
end | |
class SqlStringWithNot < String |
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 FooBar | |
# extend DelegatedModuleFunction | |
# | |
# define_delegated_module_functions do | |
# | |
# def foo_bar | |
# 'Foo' + bar | |
# end | |
# | |
# private |
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 UseSingleForwardableWithActiveSupport | |
extend ActiveSupport::Concern | |
included do | |
original_delegate_method = method(:delegate).unbind if self.respond_to?(:delegate) | |
extend SingleForwardable | |
if original_delegate_method.present? | |
# restore the delegate of ActiveSupport that has been overshadowed by SingleForwardable | |
define_singleton_method :delegate, original_delegate_method |
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 EnumeratorWithPositionAndSize | |
# @yields [original_params, position_and_size] | |
# @yieldparam position_and_size [(Integer, Integer)] 2-element array of position (first being 1) and size; size may be nil (see Enumerator#size) | |
# @example | |
# %w[a b c d e].each.with_position_and_size { |char, (position, size)| puts "#{char} (#{position}/#{size})" } | |
def with_position_and_size | |
return to_enum(__method__) unless block_given? | |
my_size = self.size | |
self.with_index(1) do |original_params, position| |
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 IOStreamingResponseBody | |
def self.render(options = {}, &block) | |
instance = new(options) | |
instance.define_singleton_method :render do |dummy_io| | |
block.call(dummy_io) | |
end | |
instance | |
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
module MongoidEmbeddedObjectModification | |
# Custom field behaves more like an immutable value. | |
# Your changes to the object obtained with getter method will not take an effect unless you assign it back. | |
# https://github.com/mongoid/mongoid/issues/3341 | |
def modify(mongoid_doc, field_name) | |
obj = mongoid_doc.public_send(field_name) | |
yield obj | |
mongoid_doc.public_send("#{field_name}=", obj) | |
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
require 'sequel/model' | |
module Sequel | |
class << self | |
# abstract model needs to avoid inherited callback | |
# (see https://groups.google.com/forum/#!msg/sequel-talk/OG5ti9JAJIE/p1iqO57cwqwJ) | |
# MyAbstractModel = Sequel.new_abstract_model do | |
# # ... your common stuff ... |
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 TwoPlusTwoIsFive | |
def self.included(base) | |
base.class_exec do | |
alias_method :plus_without_two_plus_two_is_five, :+ | |
private :plus_without_two_plus_two_is_five | |
alias_method :+, :plus_with_two_plus_two_is_five | |
public :+ | |
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
# for Rails 3.2.12 | |
# loads fixtures a lot faster | |
require 'active_record/fixtures' | |
class ActiveRecord::Fixtures | |
# based on activerecord-3.2.12/lib/active_record/fixtures.rb l.462 | |
# modified lines are marked CHANGED | |
def self.create_fixtures(fixtures_directory, table_names, class_names = {}) | |
table_names = [table_names].flatten.map { |n| n.to_s } |
OlderNewer