Skip to content

Instantly share code, notes, and snippets.

@joshmcarthur
joshmcarthur / any_gem_version.sh
Created July 17, 2013 19:10
Run any version of a binary from Rubygems
rails _3.2.11_ new rails32_app
@joshmcarthur
joshmcarthur / gist:6107454
Created July 29, 2013 20:21
Run all specs matching a particular resource with full details of what is being tested
rspec --pattern "spec/**/*[RESOURCE]*_spec.rb,spec/**/[RESOURCE]/**/*_spec.rb" --format "documentation"
@joshmcarthur
joshmcarthur / gitcmd.sh
Created August 1, 2013 22:54
Undo a git merge
git reset --hard origin/BRANCH
@joshmcarthur
joshmcarthur / .travis.yml
Created August 5, 2013 23:43
CI XML Schemas on Travis CI
script:
- "xmllint --noout --schema db/schemas/blog_import.xsd db/schemas/sample/*"
@joshmcarthur
joshmcarthur / creatable_model.rb
Created August 6, 2013 22:47
A Rails model that can be created, but not destroyed, deleted, or updated
class CreatableModel < ActiveRecord::Base
after_initialize :readonly!, unless: :new_record?
end
@joshmcarthur
joshmcarthur / uninstall_gems.sh
Created August 12, 2013 02:15
Remove all gems from Ruby 2.0.0 EXCEPT for system gems
gem list | grep -v 'test-unit' | egrep -v 'rdoc|test-unit|psych|io-console|bigdecimal|json|rake|minitest' | xargs gem uninstall
@joshmcarthur
joshmcarthur / swedish_rounding.rb
Created August 15, 2013 05:40
Swedish rounding (.45 up to .5) for Floats
class Float
# Public: Round a decimal number using the Swedish rounding system.
#
# This method complements Float#round but is intended for dealing with
# currencies, etc. where a more 'real-life' rounding technique is needed.
#
# Basically, the vanilla Float#round method only looks at the first number,
# which can lead to bad rounds.
#
# Example
@joshmcarthur
joshmcarthur / model_spec.rb
Created September 12, 2013 22:19
A nicer way of speccing validations
describe Model do
it "validates presence of name" do
build(:model, name: "").should have(1).errors_on(:name)
end
end
@joshmcarthur
joshmcarthur / asset.rb
Last active December 23, 2015 03:49
One-to-one Asset model with Carrierwave
class Asset < ActiveRecord::Base
validates :attachment_content_type,
presence: true
validates :attachment_file_size,
numericality: {only_integer: true}
mount_uploader :attachment, AssetUploader
before_validation :update_attachment_attributes
@joshmcarthur
joshmcarthur / uploader.rb
Created September 16, 2013 04:02
Process carrierwave images to produce non-Retina and Retina versions of an image
version :avatar_large do
process resize_to_fill: [940, 309]
end
version :avatar_large_2x do
process resize_to_fill: [940, 309].map { |d| d * 2 }
def full_filename(for_file = model.attachment.file)
"avatar_large@2x.#{model.attachment.file.extension}"
end