Skip to content

Instantly share code, notes, and snippets.

@vajradog
vajradog / auto_expiration.md
Last active April 5, 2021 22:33
Set auto expiration for rails models

##Set auto expiration for rails models.

This is a simple view logic that moves objects based on a predetermined date.

Let's say we have a bunch of events(or posts) on our index page and we want to sort out which of these are still active and which are not (based on the time).

We want the active ones to be clickable and prominent, we want the non-active ones to be greyed-out or even hidden. So how do we do this?

Rails provide a simple yet powerful method: future?()

@andykirk
andykirk / truncate_html.php
Last active October 8, 2019 10:00
PHP Truncate HTML Function
/**
* truncate_html()
*
* Truncates a HTML string to a given length of _visisble_ (content) characters.
* E.g.
* "This is some <b>bold</b> text" has a visible/content length of 22 characters,
* though the total string length is 29 characters.
* This function allows you to limit the visible/content length whilst preserving any HTML formatting.
*
* @param string $html
@markbates
markbates / gist:4240848
Created December 8, 2012 16:06
Getting Started with Rack

If you're writing web applications with Ruby there comes a time when you might need something a lot simpler, or even faster, than Ruby on Rails or the Sinatra micro-framework. Enter Rack.

Rack describes itself as follows:

Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks.

Before Rack came along Ruby web frameworks all implemented their own interfaces, which made it incredibly difficult to write web servers for them, or to share code between two different frameworks. Now almost all Ruby web frameworks implement Rack, including Rails and Sinatra, meaning that these applications can now behave in a similar fashion to one another.

At it's core Rack provides a great set of tools to allow you to build the most simple web application or interface you can. Rack applications can be written in a single line of code. But we're getting ahead of ourselves a bit.

@davidjrice
davidjrice / redcarpet.rb
Created June 29, 2012 00:34
Rails 3.2 Markdown Template Handler
# config/initializers/redcarpet.rb
module ActionView
module Template::Handlers
class Markdown
class_attribute :default_format
self.default_format = Mime::HTML
def call(template)
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
markdown.render(template.source).html_safe.inspect
@botimer
botimer / yesno.rb
Created June 7, 2012 19:56
Handy yes/no prompt for little ruby scripts
# This is a reasonably well-behaved helper for command-line scripts needing to ask a simple yes/no question.
# It optionally accepts a prompt and a default answer that will be returned on enter keypress.
# It keeps asking and echoes the answer on the same line until it gets y/n/Y/N or enter.
# I tried to get Highline to behave like this directly, but even though it's sophisticated, I didn't like the result.
# This isn't especially elegant, but it is straightforward and gets the job done.
require 'highline/import'
def yesno(prompt = 'Continue?', default = true)
a = ''
s = default ? '[Y/n]' : '[y/N]'
<!doctype html>
<!-- http://taylor.fausak.me/2015/01/27/ios-8-web-apps/ -->
<html>
<head>
<title>iOS 8 web app</title>
<!-- CONFIGURATION -->
@frane
frane / ArrayObjectDemo.coffee
Created December 31, 2011 02:19
Traversing arrays and objects in CoffeeScript
# Traversing arrays and objects in CoffeeScript
# The array and object we use for testing
arr = [1, 2, 3, 4, 5]
obj = {a: 1, b: 2, c: 3, d: 4, e: 5}
# 'in' has a different meaning in CoffeeScript than in JavaScript
# CS: element in array -> JS: array.indexOf(element) >= 0
console.log '5 in arr: ' + (5 in arr)
@mathewbyrne
mathewbyrne / slugify.js
Created October 12, 2011 04:34
Javascript Slugify
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
@jcasimir
jcasimir / friendly_urls.markdown
Created September 11, 2011 15:48
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@borgar
borgar / Tiny JavaScript tokenizer.js
Created June 24, 2010 12:33
A compact tokenizer written in JavaScript.
/*
* Tiny tokenizer
*
* - Accepts a subject string and an object of regular expressions for parsing
* - Returns an array of token objects
*
* tokenize('this is text.', { word:/\w+/, whitespace:/\s+/, punctuation:/[^\w\s]/ }, 'invalid');
* result => [{ token="this", type="word" },{ token=" ", type="whitespace" }, Object { token="is", type="word" }, ... ]
*
*/