Skip to content

Instantly share code, notes, and snippets.

# Mock all calls through .find on an ActiveRecord model
# or its chained scopes
def expect_find(model)
@scope ||= model.scoped
model.stub!(:scoped).and_return @scope
@scope.should_receive(:find).ordered
end
@avit
avit / gist:4974312
Created February 18, 2013 00:11
Ruby Time DST
std = Time.local(2013, 11, 3, 01, 00, 00)
# => 2013-11-03 01:00:00 -0800
dst = std - 3600
# => 2013-11-03 01:00:00 -0700
## Challenge:
# Is it possible to instantiate a time in the last hour of DST (01:00 - 01:59)
# without first having to create a nearby time to subtract from?
@avit
avit / stub_barometer.rb
Created February 9, 2013 23:17
Allows for using barometer gem in client app specs without hitting the network.
require 'bundler'
require 'barometer'
require 'fakeweb'
module TestStubs
module Barometer
def self.stub!
FakeWeb.register_uri(
:get, %r[^http://api\.wunderground\.com/auto/wui/geo/WXCurrentObXML/index\.xml\?query],
body: File.read(current_fixture_path)
## app/models/project.rb
class Project
autoload :Report, 'project_report'
end
## app/models/project_report.rb
class Project::Report
end
@avit
avit / nil_object.rb
Last active December 11, 2015 07:58
Wondering why a direct comparison using "if" passes when the value is falsy.
class NullObject
def true?; false end
def false?; true end
def nil?; true end
def blank?; true end
def present?; false end
def empty?; true end
def to_s(*); "" end
def !; true end
@avit
avit / backtick_attribute_syntax.rb
Last active December 10, 2015 01:38
Stupid ruby tricks for ActiveRecord query syntax. For amusement only.
module BacktickAttributeSyntax
extend self
# WARNING: this is a dangerous hack. Don't use it. It'll bite you if you ever
# accidentally call backticks outside of a model.
# Override backtick syntax to return an Arel attribute for use with
# predications like `.gteq` in the model. To use the original shell
# behaviour, (which should be very uncommon in ActiveRecord models), you can
class Foo < ActiveRecord::Base
serialize :check_in, TimeOfDayAttribute # dump/load for valid, empty, nil values tested independently
# Setter will handle both String & TimeOfDay input
# I want to test the getter before defining (and depending on) this method
def check_in=(param)
# ...
end
end
Oct 16 19:56:48 localhost mysqld_safe[27557]: ERROR: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE user ADD column Show_view_priv enum('N','Y') CHARACTER SET utf8 NOT ' at line 1
Oct 16 19:56:48 localhost mysqld_safe[27557]: 121016 19:56:48 [ERROR] Aborting
@avit
avit / rack_inputs.html
Created October 9, 2012 03:58
Bad inputs
<!-- this combination of inputs will throw Rack -->
<input name="event[dates][]" type="date" />
<input id="event_dates__destroy" name="event[dates][_destroy]" type="hidden" />
class SomethingsController < ActionController::Base
def create
Time.zone = current_user.time_zone
@another_user = current_user.friends.find(params[:friend_id])
FriendMailer.message(@another_user).deliver
# Wouldn't Time.zone be reset by the mailer before rendering the page here?
end
end