Skip to content

Instantly share code, notes, and snippets.


Robert Rawlins SirRawlins

View GitHub Profile
sshaw / rspec-checksum-matchers.rb
Last active Jan 12, 2020
RSpec::Checksum::Matchers: Check if a String looks like a digest produced a the given hash algorithm. Also see
View rspec-checksum-matchers.rb
# RSpec::Checksum::Matchers Check if a String looks like a checksum produced by the given algorithm.
# Copyright (c) 2016 Skye Shaw. Released under the MIT License (
require "rspec/expectations"
RSpec::Matchers.define :be_a_checksum do
regex = /\A[0-9a-f]{32,128}\z/i
chars = {
skanev / rubocop.rb
Last active Jun 4, 2020
A Rubocop wrapper that checks only added/modified code
View rubocop.rb
#!/usr/bin/env ruby
# A sneaky wrapper around Rubocop that allows you to run it only against
# the recent changes, as opposed to the whole project. It lets you
# enforce the style guide for new/modified code only, as opposed to
# having to restyle everything or adding cops incrementally. It relies
# on git to figure out which files to check.
# Here are some options you can pass in addition to the ones in rubocop:
stereoscott / copy_attachments.rb
Created Apr 6, 2014
Paperclip Copy Attachments
View copy_attachments.rb
module Paperclip
module CopyAttachments
def copy_attachments_from(source_obj, source_bucket = nil, destination_bucket = nil)
self.class.attachment_definitions.keys.each do |attachment_name|
source_attachment = source_obj.send(attachment_name)
next if source_attachment.blank?
destination_attachment = self.send(attachment_name)
connection = destination_attachment.send(:connection)
jasny / bootstrap-em.less
Last active Jan 5, 2020
Use em or rem font-size in Bootstrap 3
View bootstrap-em.less
* Use em or rem font-size in Bootstrap 3
@font-size-root: 14px;
@font-unit: 0rem; // Pick em or rem here
// Convert all variables to em
gaganawhad / i18n
Last active Sep 22, 2017
Some conventions / guidelines / best practices that helped me while working on internationalization of a rails app
View i18n

Internationalization of a Rails App : conventions / guidelines / best practices

I have worked, and am currently working on internationalization of a Rails app. The apps require translating the ActiveRecord models. I have done some googling, but haven't really found any best practices / conventions / guidelines on how to think through this and what guidelines to follow.

I decided to create this document, to give myself a start and have it possibly help others. I am not an expert at any of this, and there may be things I am thinking incorrectly, but having it documented makes it easier to see the flaws and improve upon. If you have some ideas on additions, or think that somethings should change, get in touch.

Note: I use Globalize for model translations

  • I18n.locale controls the app wide locale. It refers to the particular localization version of the app. It defines how the app is localized to the user. It's scope is somewhat larger than the language/tran
View favicons.html
<!-- For third-generation iPad with high-resolution Retina display: -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144-precomposed.png">
<!-- For iPhone with high-resolution Retina display: -->
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114-precomposed.png">
<!-- For first- and second-generation iPad: -->
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72-precomposed.png">
<!-- For non-Retina iPhone, iPod Touch, and Android 2.1+ devices: -->
<link rel="apple-touch-icon-precomposed" href="apple-touch-icon-precomposed.png">
<!-- Regular browser favicon: -->
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
mkrogh / fileless_file.rb
Created Mar 3, 2012
Carrierwave proccess zipfile contents
View fileless_file.rb
#A nice little carrierwave IO faker class
class FilelessFile < StringIO
attr_accessor :original_filename
attilagyorffy / css_colour_validator.rb
Created Oct 29, 2011
CSS colour validation in Rails 3
View css_colour_validator.rb
# Put this file under Rails.root /lib
class CssColourValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return false unless value
record.errors[attribute] << (options[:message] || 'is not a valid CSS colour') unless ::HexadecimalColourValidator.matches?(value) or ::WebSafeColourValidator.matches?(value)
Amitesh / unzip-folder.rb
Created Sep 28, 2011
unzip folder in ruby
View unzip-folder.rb
require 'rubygems'
require 'zip/zip'
def unzip_file (file, destination) { |zip_file|
zip_file.each { |f|
zip_file.extract(f, f_path) unless File.exist?(f_path)
runemadsen / description.markdown
Created Sep 26, 2011
Reverse polymorphic associations in Rails
View description.markdown

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

You can’t perform that action at this time.