Skip to content

Instantly share code, notes, and snippets.

View jphenow's full-sized avatar
💻
👋🏻 :octocat:

Jon Phenow jphenow

💻
👋🏻 :octocat:
View GitHub Profile
@jphenow
jphenow / method-defaults-from-previous-params.rb
Created April 15, 2014 16:08
Interesting, useful method signature behavior on Ruby
def foo(one, two = one)
[one, two]
end
foo("1")
# => ["1", "1"]
foo("1", "2")
# => ["1", "2"]
@jphenow
jphenow / chain-queries.rb
Created May 14, 2014 15:31
Chainable Queries
class Post < ActiveRecord::Base
belongs_to :author
has_many :comments
scope :author, ->(name) { joins(:author).where(author: { name: name }) }
scope :comment, ->(text) { joins(:comments).merge(Comment.text_query(text)) }
scope :text, ->(text) { where("text ILIKE ?", "%#{text}%") }
end
{ author: "Jon", comment: "awesome", text: "Ruby" }.reduce(Post) { |query, (scope, args)|
@jphenow
jphenow / merge_hash_arrays.rb
Created May 30, 2014 18:52
Merge Hash Arrays by attribute
# Let's say we have
# original = [
# {"id"=>3, "address"=>"something@something.com", "alerts"=>false},
# {"id"=>4, "address"=>"something2@something2.com", "alerts"=>false}
# ]
# new = [{"id"=>3, "address"=>"CHANGED@something.com", "alerts"=>false}]
original.reduce([]) { |array, el|
array << new.find { |new_element| new_element["id"] == el["id"] } || el
}
@jphenow
jphenow / index.rb
Created July 30, 2014 15:09
Get index and stuff
array = [{ foo: :bar}, 1]
array.insert(array.index { |el| el.is_a?(Hash) }, 2)
array # => [2, { foo: :bar}, 1]
@jphenow
jphenow / field_error.rb
Created July 30, 2014 15:17
Spec the field error proc
# Say you have
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
class_attr_index = html_tag.index 'class="'
if class_attr_index
html_tag.insert class_attr_index+7, 'error '
else
html_tag.insert html_tag.index('>'), ' class="error"'
end
end
@jphenow
jphenow / multiparam_attribute.rb
Created August 19, 2014 15:22
extracted multiparam attributes from Rails (for dates and times) for personal use
class MultiattributeParam #:nodoc:
attr_reader :type, :values
def initialize(type, name, params)
@type = type
@values = {}
params.each do |key, value|
regex = /#{name}\((\d+)i\)/.match(key.to_s)
next unless regex && regex.captures.present?
@values[regex.captures.first.to_i] = value
@jphenow
jphenow / build_variations.sh
Last active August 29, 2015 14:05
seed-writer
#!/usr/bin/env bash -e
clare_dir=~/workspace/clare_accounting
disperation_dir=~/workspace/disperation
[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"
function move_to_clare() {
builtin cd $clare_dir
rvm use ruby-2.1.1@clare_accounting &> /dev/null
@jphenow
jphenow / paper-trail.rb
Created September 10, 2014 18:33
wrap paper trail records
class PaperTrailClassWrapper < SimpleDelegator
def initialize
__set_obj__(PaperTrail::Version)
end
def some_fancy_scope
where(item_type: "FancyClass")
end
end
@jphenow
jphenow / fork-rspec.rb
Created October 6, 2014 22:42
forked Rspec
require 'rspec/core/rake_task'
@parallel_limit = 4
files = Dir.glob("spec/**/*_spec.rb")
pids = []
@parallel_limit.times do |i|
pids << fork do
# Run the tests
@jphenow
jphenow / uuid.rb
Created October 9, 2014 19:37
uuid
# make IdExtractor partition the list sent in so you can get ids and uuids
scope :id_or_uuid, ->(id_or_uuid) {
extracted = IdExtractor.extract(id_or_uuid)
where(["#{table_name}.id IN (?) OR #{table_name}.uuid IN (?)", extracted.ids, extracted.uuids])
}