Skip to content

Instantly share code, notes, and snippets.

@avit
avit / conventions.md
Last active Oct 2, 2015
These are the things I think we need to define for ourselves on top of ActiveRecord
View conventions.md

Things that exist

Model.column_names

[
  "id",
  "other_id",
  "other_type",
  "stuff",
  "things",
@avit
avit / pipes.rb
Created Sep 26, 2014
Simple ruby pipelines
View pipes.rb
class Object
def | other
case other
when Proc
if self.is_a?(Proc)
proc { |input| other.call( self.call(input) ) }
else
other.call(self)
end
else
@avit
avit / rfc822.rb
Created Sep 19, 2014
Regular expression for parsing email addresses
View rfc822.rb
#
# RFC822 Email Address Regex
# --------------------------
#
# Originally written by Cal Henderson
# c.f. http://iamcal.com/publish/articles/php/parsing_email/
#
# Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
View base-Berksfile
site :opscode
metadata
cookbook 'locale',
git: "https://github.com/hw-cookbooks/locale.git",
tag: "v1.0.0"
@avit
avit / lib-gem_ext-arel-predications.rb
Created Sep 21, 2013
Allow Ransack to include NULL values in "does not equal" searches.
View lib-gem_ext-arel-predications.rb
module Arel
module Predications
def not_eq other
if other.is_a? Nodes::Not
Nodes::Equality.new self, other.value
else
Nodes::NotEqual.new self, other
end
end
View active_record_scopes.md

How should ActiveRecord behave with merging conditions from various sources? See https://github.com/rails/rails/issues/9813 for background.

  • clobber: conflicting condition values are replaced with the rightmost value
  • append: conflicting condition values are chained into an AND query

Direct find

View qlobe.rb
v=0000;eval$s=%q~d=%!^Lcf<LK8, _@7gj*LJ=c5nM)Tp1g0%Xv.,S[<>YoP
4ZojjV)O>qIH1/n[|2yE[>:ieC "%.#% :::##" 97N-A&Kj_K_><wS5rtWk@*a+Y5
yH?b[F^e7C/56j|pmRe+:)B "##% ::##########" O98(Zh)'Iof*nm.,$C5Nyt=
PPu01Avw^<IiQ=5$'D-y? "##: ###############" g6`YT+qLw9k^ch|K'),tc
6ygIL8xI#LNz3v}T=4W "# #. .####:#######" lL27FZ0ij)7TQCI)P7u
}RT5-iJbbG5P-DHB<. " ##### # :############" R,YvZ_rnv6ky-G+4U'
$*are@b4U351Q-ug5 " #######################" 00x8RR%`Om7VDp4M5
PFixrPvl&<p[]1IJ " ############:#### %#####" EGgDt8Lm#;bc4zS^
y]0`_PstfUxOC(q " .#############:##% .## ." /,}.YOIFj(k&q_V
zcaAi?]^lCVYp!; " %% .################. #. " ;s="v=%04o;ev"%
@avit
avit / active_support_test.rb
Last active Dec 17, 2015
Problem with different level of precision when adding ActiveSupport::Duration than Fixnum seconds. It looks like Rails 4.0 has a fix for this. The bug (illustrated here with plain ruby) depends on the system environment for the precision error to show up.
View active_support_test.rb
require 'active_support/time'
require 'active_support/version'
t0 = Time.now # => 2013-05-26 01:14:51 +0000
t1 = t0 + 86400
t2 = t0 + 1.day
puts "Ruby %s / ActiveSupport %s" % [RUBY_VERSION, ActiveSupport::VERSION::STRING]
puts "Time + Fixnum nsec: %s" % t1.nsec # => 11111111
puts "Time + AS::Duration nsec: %s" % t2.nsec # => 11111000
@avit
avit / pry.txt
Last active Dec 16, 2015
What is this anonymous class?
View pry.txt
pry> c = ObjectSpace.each_object.detect { |i| i.class.to_s == "#<Class:0xb8f4e33c>" }
pry> c
=> {:perform_caching=>true,
:request_forgery_protection_token=>:authenticity_token,
:allow_forgery_protection=>true,
:relative_url_root=>nil,
:logger=>
#<ActiveSupport::TaggedLogging:0xbc9d972c...
:cache_store=>
@avit
avit / README.md
Last active Dec 15, 2015
IDEA: To add layout options for Rails
View README.md

Override ActionController layout class method to take a view_options hash, whether or not the actual layout template is being changed:

layout "foo", view_options: {sidebar: 'special'}
layout view_options: {sidebar: 'special'}

Define a LayoutOptions module in the controller class and give it to the view helpers. It would define methods: