Skip to content

Instantly share code, notes, and snippets.

View soulcutter's full-sized avatar

Bradley Schaefer soulcutter

View GitHub Profile
@soulcutter
soulcutter / auto_annotate_models.rake
Created February 13, 2014 20:52
My default lib/tasks/auto_annotate_models.rake
# NOTE: only doing this in development as some production environments (Heroku)
# NOTE: are sensitive to local FS writes, and besides -- it's just not proper
# NOTE: to have a dev-mode tool do its thing in production.
if(Rails.env.development?)
task :set_annotation_options do
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults({
'position_in_routes' => "after",
'position_in_class' => "after",
@soulcutter
soulcutter / datapoint.rb
Last active August 29, 2015 13:57
Conversion methods
# encoding: utf-8
require 'datapoint_value'
class Datapoint < ActiveRecord::Base
include DatapointValue::Conversions
before_save :normalize_value
def self.upsert(params)
dp = where(token: params[:token], at: params[:at]).first
@soulcutter
soulcutter / foo_spec.rb
Created April 4, 2014 18:56
I guess you have to understand the implementation to understand this error…
describe ActiveSupport::TimeZone do
it "has an invalid fraction, clearly" do
rub_a_dub_double = double.as_null_object
ymd = [rub_a_dub_double.year, rub_a_dub_double.month, rub_a_dub_double.day]
ActiveSupport::TimeZone['UTC'].local(*ymd) # ArgumentError: invalid fraction
Date.new(*ymd) # => ArgumentError: invalid date -- now that makes sense
end
end
@soulcutter
soulcutter / filter_manager.rb
Created April 24, 2014 18:56
Proposal for ExclusionFilters
class ExclusionRules < FilterRules
CONDITIONAL_FILTERS = {
:if => lambda { |value| !value },
:unless => lambda { |value| value }
}.freeze
def include_example?(example)
example.any_apply?(@rules) || example.any_apply?(CONDITIONAL_FILTERS)
end
end
@soulcutter
soulcutter / twilio_country_report.rb
Created April 28, 2014 21:19
Twilio Country Report
class TwilioCountryReport < TwilioReport
def generate_stats
@country_stats = HashWithIndifferentAccess.new { |h, country| h[country] = TwilioCountryStat.new(country) }
build_country_stats
country_stats
end
def country_stats
@country_stats.values
end
@soulcutter
soulcutter / spec_helper.rb
Created May 7, 2014 17:34
sample spec_helper in rspec-rails 2.14.2
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
@soulcutter
soulcutter / have_xml.rb
Created June 10, 2014 21:52
General purpose RSpec 2.14 XML matcher
require 'nokogiri'
RSpec::Matchers.define :have_xml do |xpath, matcher|
match do |body|
doc = Nokogiri::XML::Document.parse(body)
nodes = Array(doc.xpath(xpath))
nodes.map! { |node| node.respond_to?(:content) ? node.content : node }
if nodes.empty?
false
@soulcutter
soulcutter / SassMeister-input-HTML.html
Created July 16, 2014 02:59
stacked sass mixin experiment
<div class="stack">
lorem ipsum
</div>
@soulcutter
soulcutter / zones.rb
Created September 9, 2014 16:32
Date#in_time_zone monkeypatch for Rails 3
# this comes included in ActiveSupport 4+
unless ActiveSupport::VERSION::MAJOR > 3
require 'date'
module DateAndTime
module Zones
# Returns the simultaneous time in <tt>Time.zone</tt> if a zone is given or
# if Time.zone_default is set. Otherwise, it returns the current time.
#
# Time.zone = 'Hawaii' # => 'Hawaii'
@soulcutter
soulcutter / delegating_form_object.rb
Last active August 29, 2015 14:08
Basic rails delegating form object factory
class PartialReportForm
include DelegatingFormObject.new(Report)
delegate :name, :name=,
to: :report
validates :name,
presence: true
end