Skip to content

Instantly share code, notes, and snippets.

View michaelminter's full-sized avatar

Michael Minter michaelminter

View GitHub Profile
@michaelminter
michaelminter / content_with_ad.rb
Created September 10, 2013 15:28
Inject ad container into content after n paragraphs
module PagesHelper
def content_with_ad(content, ad_count=3)
p_count = 0
new_content = content.gsub(/(<p(>|\s+[^>]*>).*?<\/p>)/) { |p|
p_count = p_count + 1
if p_count == ad_count
p + '<div class="above_fold_ad">Ad</div>'
else
p
@michaelminter
michaelminter / archive_by_year.sql
Created September 6, 2013 18:33
Get blog archive by year
SELECT
categories.name,
MONTHNAME( articles.created_at ) AS month_name,
MONTH( articles.created_at ) AS month,
YEAR( articles.created_at ) AS year,
COUNT( articles.id ) as news_count
FROM news_categories categories, news_articles articles
WHERE
articles.published = 1
AND categories.id = articles.category_id
@michaelminter
michaelminter / archive.sql
Last active December 22, 2015 11:09
Get blog archive format of dates by count and grouped by month / year
SELECT
news_categories.name AS category,
YEAR(news_articles.creation_date) AS year,
MONTH(news_articles.creation_date) AS month,
MONTHNAME(news_articles.creation_date) AS month_name,
COUNT(*) AS total
FROM news_articles
INNER JOIN news_categories ON news_articles.category_id=news_categories.id
WHERE DATE(news_articles.creation_date) >= DATE_SUB(NOW(), INTERVAL 2 YEAR)
GROUP BY category, year, month
@michaelminter
michaelminter / scp.bash
Created September 5, 2013 20:02
Copy a file through SSH
scp [email protected]:foobar.txt /some/local/directory
@michaelminter
michaelminter / build_other.rb
Last active December 22, 2015 07:19
Create Active Record associations automatically with polymorphic database models http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Auto-generated+methods
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :imageable_id, :imageable_type, :name, :url
end
class Document < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :title, :content
end
@michaelminter
michaelminter / keywords.rb
Last active December 22, 2015 06:18
Create Keywords from content
# gem install Sanitize
require 'Sanitize'
def generate_keywords(content)
# strip HTML tags
content = Sanitize.clean content
# dump content into array and remove short words
words = content.scan /[A-Za-z0-9]{3,}/
@michaelminter
michaelminter / git-push-all.bash
Created August 31, 2013 20:11
Add multiple remote repos to your git config
git remote add all [email protected]:repo.git
git remote set-url --add all [email protected]:repo.git
# now just, $ git push all
@michaelminter
michaelminter / gist:5585340
Last active December 17, 2015 09:09
rvm create and use line
$ rvm --use --ruby-version 2.1.1@project
@michaelminter
michaelminter / localStorage_limit.js
Created April 16, 2013 08:05
Find out how much localStorage space your browser is limited to
var iterationsData;
var results = document.getElementById('results');
(function () {
if (!('localStorage' in window)) {
results.innerHTML = 'Your browser has no localStorage support.';
return;
}
@michaelminter
michaelminter / people_test.rb
Created April 6, 2013 02:01
Without using any date/time library, write a function or method that accepts two mandatory arguments. The first argument is a string of the format[H]H:MM {AM|PM} and the second argument is an integer. Assume the integer is the number of minutes to add to the string. The return value of the function should be a string of the same format as the fi…
#! /usr/bin/env ruby
# Without using any date/time library, write a function or method that accepts
# two mandatory arguments. The first argument is a string of the format[H]H:MM
# {AM|PM} and the second argument is an integer. Assume the integer is the
# number of minutes to add to the string. The return value of the function
# should be a string of the same format as the first argument. For example
# add_minutes('9:13 AM', 10) would return 9:23 AM.
# To run: