Collection of concerns for your Rails application
Copy to your app/models/concerns
directory
# A terrible idea | |
# Spent a couple minutes experimenting with the idea of using Ruby to initialize | |
# my Vim session instead of something reliable, like a .vimrc. | |
# | |
# Why? Because I love terrible ideas. | |
# BTW - only works for the simplest of options right now. | |
class VimSession | |
include VIM | |
def initialize &options |
module Subscription | |
extend ActiveSupport::Concern | |
included do | |
attr_accessor :billing_card_token | |
end | |
def save_with_payment | |
if valid? | |
customer = ::Stripe::Customer.create(email: email, |
# Expirable is a module that lets you easily cache | |
# groups of records, either across an entire record: | |
# cache(Snippet) => cache(Snippet.cache_key) | |
# or, across a scope: | |
# cache(page.blocks) => cache(page.blocks.cache_key) | |
# | |
# Why not just use `maximum(:updated_at)`? | |
# Because it requires 2 queries: the total count, | |
# and the updated_at timestamp |
module Resourceable | |
protected | |
def create_response( saved, resource, format, name, url = false ) | |
if saved | |
format.html { redirect_to url ? url : resource, notice: name + ' was successfully created.' } | |
format.json { render action: 'show', status: :created, location: resource } | |
else | |
render_error( format, 'new' ) | |
end |
require 'fileutils' | |
class Fsh | |
def cmd(c) | |
# parse a command line and call appropriate function, returning output | |
c.match /^(\w+)\s*(.*)/ # only handles one argument | |
cmd = $1 | |
arg = $2 | |
case cmd |
#!/usr/bin/ruby | |
# coding: utf-8 | |
# author : Marc Quinton, march 2013, licence : http://fr.wikipedia.org/wiki/WTFPL | |
libdir = 'lib' | |
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) | |
require 'pp' |
class Range | |
def overlaps?(other) | |
(self.first <= other.last) and (other.first <= self.last) | |
end | |
end |
require 'io/console' | |
# Reads keypresses from the user including 2 and 3 escape character sequences. | |
def read_char | |
STDIN.echo = false | |
STDIN.raw! | |
input = STDIN.getc.chr | |
if input == "\e" then | |
input << STDIN.read_nonblock(3) rescue nil |
# via http://www.megasolutions.net/ruby/Getting-the-size-of-the-terminal-in-a-portable-way-26006.aspx | |
TIOCGWINSZ = 0x40087468 | |
def get_winsize | |
str = [0, 0, 0, 0].pack('SSSS') | |
if STDIN.ioctl(TIOCGWINSZ, str) >= 0 | |
rows, cols, xpixels, ypixels = str.unpack("SSSS") | |
p rows, cols, xpixels, ypixels | |
else | |
puts "Unable to get window size" |