Skip to content

Instantly share code, notes, and snippets.

View expect_find.rb
# 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 Feb 18, 2013
Ruby Time DST
View gist:4974312
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 Feb 9, 2013
Allows for using barometer gem in client app specs without hitting the network.
View stub_barometer.rb
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)
View gist:4647283
## 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 Dec 11, 2015
Wondering why a direct comparison using "if" passes when the value is falsy.
View nil_object.rb
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 Dec 10, 2015
Stupid ruby tricks for ActiveRecord query syntax. For amusement only.
View backtick_attribute_syntax.rb
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
View foo.rb
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
View gist:3901771
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
View rack_inputs.html
<!-- this combination of inputs will throw Rack -->
<input name="event[dates][]" type="date" />
<input id="event_dates__destroy" name="event[dates][_destroy]" type="hidden" />
View something_controller.rb
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