Skip to content

Instantly share code, notes, and snippets.

@yoavmatchulsky
yoavmatchulsky / pluralize_with_parenthesis.rb
Created January 21, 2014 11:50
Add #pluralize_with_parenthesis to ActiveSupport::Inflector and Rails helpers. Example: 'article'.pluralize_with_parenthesis => 'article(s)' 'person'.pluralize_with_parenthesis => 'people'
module ActiveSupport
module Inflector
def pluralize_with_parenthesis(word)
rule, replacement = find_inflections(word, inflections.plurals)
if replacement =~ /\A\w+\Z/
word.gsub!(rule, "(#{replacement})")
else
pluralize(word)
end
end
@yoavmatchulsky
yoavmatchulsky / cleanup_tasks.rb
Last active December 14, 2015 19:18
rake clean:sessions to delete old sessions
desc 'Cleanup tasks'
namespace :clean do
desc 'Clean sessions table (pass DAYS=X default is 30 days)'
task :sessions => :environment do
days = ENV['DAYS'].to_i
days = 30 if days <= 0
result = ActiveRecord::Base.connection.execute("DELETE FROM sessions WHERE updated_at <= (CURRENT_DATE - INTERVAL '#{days} days')")
affected_rows = result.cmd_tuples
@yoavmatchulsky
yoavmatchulsky / gist:4749498
Created February 10, 2013 12:49
Fix for setting a he locale (/he prefix) to named routes. Bug was that #url_for called from views didn't get the correct locale. Solved by set_locale if @locale.nil in #default_url_options
def set_locale
if @locale.nil?
I18n.locale = params[:locale] || I18n.default_locale
@locale = I18n.locale.to_sym
end
end
def default_url_options(options = {})
set_locale if @locale.nil?
(function(doc) {
var ref = doc.referrer;
if (ref.match(/google\./gi)) {
var query = ref.split('?')[1],
keyword = '(not provided)',
pathname = doc.location.pathname,
rank = null,
param;
if (query) {