Skip to content

Instantly share code, notes, and snippets.

View SirRawlins's full-sized avatar

Robert Rawlins SirRawlins

  • Sorry™
  • Chichester, UK
View GitHub Profile
@dwabnitz
dwabnitz / customdomain.rb
Created October 22, 2009 19:18 — forked from speedmax/customdomain.rb
rack middleware to resolve a custom domain to an original subdomain in a multi-tenant application
require 'net/dns/resolver'
# Custom Domain
#
# Require net-dns gem
#
# A Rack middleware to to resolve the custom domain to original subdomain
# for your multi telent application.
#
# It's all transperant to your application, it performs cname lookup and
@runemadsen
runemadsen / description.markdown
Created September 26, 2011 15:23
Reverse polymorphic associations in Rails

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 @article.media

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.

@Amitesh
Amitesh / unzip-folder.rb
Created September 28, 2011 07:26
unzip folder in ruby
require 'rubygems'
require 'zip/zip'
def unzip_file (file, destination)
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
@attilagyorffy
attilagyorffy / css_colour_validator.rb
Created October 29, 2011 12:27
CSS colour validation in Rails 3
# 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)
end
end
@mkrogh
mkrogh / fileless_file.rb
Created March 3, 2012 00:51
Carrierwave proccess zipfile contents
#A nice little carrierwave IO faker class
class FilelessFile < StringIO
attr_accessor :original_filename
end
@mrsweaters
mrsweaters / favicons.html
Created April 2, 2012 17:16
Favicon Markup
<!-- 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" />
@gaganawhad
gaganawhad / i18n conventions.md
Last active July 24, 2023 19:11
Moved to obsidian. Some conventions / guidelines / best practices that helped me while working on internationalization of a rails app.

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
@jasny
jasny / bootstrap-em.less
Last active January 5, 2020 15:36
Use em or rem font-size in Bootstrap 3
/**
* 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
@stereoscott
stereoscott / copy_attachments.rb
Created April 6, 2014 21:56
Paperclip Copy Attachments
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)
@skanev
skanev / rubocop.rb
Last active March 13, 2024 08:24
A Rubocop wrapper that checks only added/modified code
#!/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:
#