Skip to content

Instantly share code, notes, and snippets.

@replaid
replaid / ddd-rails-tree.txt
Last active March 23, 2023 02:19
A Rails directory tree that expresses DDD layers and bounded contexts.
components/
my_bounded_context/
app/
presentation/ # This is called the "UI" layer in DDD literature, but it is also
# where things like JSON interfaces live -- any kind of presentation
# or handshake to anything outside. So "presentation" rather than "ui".
assets/
helpers/
mailers/
views/
@leods92
leods92 / group_date.rb
Last active December 26, 2015 14:18
Adds time zone aware data grouping method to ActiveRecord. If you want to group a large set of database rows by date using a timestamp as reference, the optimal way is to do it right in the database. When doing it though there are some issues with time zones that #group_date solves. For example, if a UTC timestamp is set to 2013-12-01 01:00 and …
module ActiveRecord
module Querying
delegate :group_date, :to => :scoped
end
module QueryMethods
def group_date(column_name = nil)
return self if column_name.blank?
# Rails uses a non standard time zone naming.
@mattgoldman
mattgoldman / hash_filter.rb
Last active December 26, 2015 08:59
Ruby module to evaluate a Hash against a set of filters. i.e. Does `{name: "John", age: 21}` pass the following filter?: `age > 20`
module HashFilter
# Runs filters on Hash and determines whether or not it shall pass
#
# ==== Attributes
#
# * +hash+ - +Hash+ to be filtered
# * +filters+ - +Array+ of objects containing the following keys for comparison
# <tt>:hash_key</tt> - Dot-notated, nestable hash key
# <tt>:comparison_operator</tt> - Any of the following comparison operators: +==+, +!=+, +<=+, +>=+, +<+, +>+
# <tt>:value</tt> - Desired value to compare for
@madrobby
madrobby / gist:5489174
Created April 30, 2013 14:43
Install Phantom.js on Ubuntu 10.04 and tweak for better rendering quality.
sudo apt-get update
sudo apt-get install libfreetype6 libfreetype6-dev libfontconfig
# /etc/apt/sources.list should contain these:
# deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ lucid multiverse
# deb http://us.archive.ubuntu.com/ubuntu/ lucid-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ lucid-updates multiverse
sudo echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | sudo debconf-set-selections
@hijonathan
hijonathan / notifications.md
Created December 7, 2012 19:12
Mark a notification as read

In your email:

<img src="https://path/to/mark-as-read?notificationId=1234&source=email" width="1" height="1" />

Bonus server-side:

When that endpoint is hit from an email, delay the response for 5-10s and listen for the connection being terminated early from the client. If they make it past 5-10s, then mark the notification as read. If not, then either they skimmed it or it was a fluke request.

@mrrooijen
mrrooijen / force-ssl.rb
Created June 20, 2011 03:41
Secure with SSL (Subdomain and Protocol) in Rails 3.1
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :secure_with_ssl
private
def secure_with_ssl
if request.subdomain != 'secure' or request.protocol != 'https'
redirect_to :subdomain => 'secure', :protocol => 'https'