Skip to content

Instantly share code, notes, and snippets.

@fny
fny / nfl-teams.json
Created July 11, 2016 17:58
NFL Teams JSON
[
{
"code": "ARI",
"name": "Cardinals",
"full_name": "Arizona Cardinals"
},
{
"code": "ATL",
"name": "Falcons",
"full_name": "Atlanta Falcons"
@fny
fny / dump_relation.rb
Last active November 28, 2016 15:15
Dump Active Record Relation to CSV
require 'csv'
def dump_relation(file_name, relation)
File.open(file_name, 'w') do |file|
# Load the data once
records = relation.to_a
# Write headers
file.puts records.first.attributes.keys.to_csv
# Write records
records.each { |record| file.puts record.attributes.values.to_csv }
@fny
fny / assets.rb
Created July 8, 2015 01:09
Dry Up Your Initializers with #tap
# Before
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.paths << Emoji.images_path
Rails.application.config.assets.precompile += %w( search.js )
# After
Rails.application.config.assets.tap do |assets|
assets.version = '1.0'
assets.paths << Emoji.images_path
assets.precompile += %w( search.js )
@fny
fny / rails.sublime-project
Created July 7, 2015 16:25
Sublime Project File for Rails Projects
{
"folders":
[
{
"file_exclude_patterns":
[
"*.sqlite3"
],
"folder_exclude_patterns":
[
@fny
fny / date_range_params.rb
Created June 16, 2015 17:29
Date Range Params Concern WIP
module DateRangeParams
extend ActiveSupport::Concern
included do
helper_method :start_date
helper_method :end_date
helper_method :date_range
def self.set_default_end_date(&block)
@default_end_date = block
@fny
fny / hash_map_values.rb
Created June 1, 2015 16:53
Benchmarks for different ways of remapping the values of a hash
require 'benchmark/ips'
MONDO_HASH = (1..50).each_with_object({}) { |num, hash|
hash[num.to_s] = num
}
# Monkey-patching a map_values to test; this should probably be a refinement
class Hash
def map_values
merge({}) { |key, value| yield(value) }
@fny
fny / config.yml.erb
Last active February 16, 2016 22:21
Elastic Beanstalk Deploy with Environment-specific Extensions
# This file lives in `app/.elasticbeanstalk/config.yml.erb`
branch-defaults:
master:
environment: <%= eb_environment %>
# environment: contigo-worker-env # Enable for worker
# Put global configs below!
class WholeNumberValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A\d+\.0*\z/
record.errors[attribute] << (options[:message] || "must be a whole number")
end
end
end
# Processes CSV compiled from http://names.mongabay.com/most_common_surnames.htm
# into a surnames text file and a CSV
require 'csv'
require 'active_support/core_ext/string'
surnames_data = {}
def process_name(name)
# Format names like McCallister
@fny
fny / rails_inclusion_validation_trickery.rb
Last active August 29, 2015 14:11
Sneaking invalid values past Rails' inclusion validation
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end