Collection of concerns for your Rails application
Copy to your app/models/concerns directory
| # 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" |
| 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 |
| class Range | |
| def overlaps?(other) | |
| (self.first <= other.last) and (other.first <= self.last) | |
| end | |
| end |
| # Helper method to fix Apple's stupid png optimizations | |
| # Adapted from: | |
| # http://www.axelbrz.com.ar/?mod=iphone-png-images-normalizer | |
| # https://github.com/peperzaken/iPhone-optimized-PNG-reverse-script/blob/master/Peperzaken/Ios/DecodeImage.php | |
| # PNG spec: http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html | |
| require 'zlib' | |
| require 'logger' | |
| module PNG |
| #!/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' |
| 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 |
| 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 |
| # 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 Subscription | |
| extend ActiveSupport::Concern | |
| included do | |
| attr_accessor :billing_card_token | |
| end | |
| def save_with_payment | |
| if valid? | |
| customer = ::Stripe::Customer.create(email: email, |