Collection of concerns for your Rails application
Copy to your app/models/concerns
directory
# Call scopes directly from your URL params: | |
# | |
# @products = Product.filter(params.slice(:status, :location, :starts_with)) | |
module Filterable | |
extend ActiveSupport::Concern | |
module ClassMethods | |
# Call the class methods with names based on the keys in <tt>filtering_params</tt> | |
# with their associated values. For example, "{ status: 'delayed' }" would call |
class Superclass | |
@@var1 = "var 1 Superclass" | |
@var2 = "var 2 Superclass" | |
def self.var1 | |
@@var1 | |
end | |
def self.var2 | |
@var2 |
# 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 |