Skip to content

Instantly share code, notes, and snippets.

# 2.3
named_scope :last_active_since, lambda { |la| la = 10.minutes.ago if la.nil?; {:conditions => ['last_active_at >= ?', la]}}
# 3 scopes
named_scope :last_active_since, lambda { |la| la = 10.minutes.ago if la.nil?; where('last_active_at >= ?', la) }
# 3 method
@rumblex
rumblex / gist:177795
Created August 30, 2009 00:36
Rails Rumble Alumni
Rails Rumble 'Alumni'
2007:
* http://tastyplanner.com
* ???
2008:
* http://prioritizd.com
module ActiveRecord
module Batches # :nodoc:
def self.included(base)
base.extend(ClassMethods)
end
# When processing large numbers of records, it's often a good idea to do
# so in batches to prevent memory ballooning.
module ClassMethods
# Yields each record that was found by the find +options+. The find is
define_index do
indexes :name, :sortable => true
indexes entry(:subdomain), :as => :entry_subdomain, :sortable => true
indexes entry(:description), :as => :entry_description, :sortable => true
indexes entry(:name), :as => :entry_name, :sortable => true
indexes entry(:resources_used), :as => :entry_resources, :sortable => true
has :state
where "state IN ('approved', 'qualified', 'verified')"
end
#!/usr/bin/env ruby
require 'rubygems'
require 'fastercsv'
require 'json'
def jsonify(set)
Dir.chdir set
images = []
folders = Dir["*/"]
folders.each do |folder|
/* GitHub Commits, version 0.0.1
* (c) 2009 Jeff Rafter
*
* Based on GitHub Badge by Dr Nic Williams (but with the good code removed)
*
* You need: jquery.js, md5.js
*
* See http://pajhome.org.uk/crypt/md5 for more info.
*
*--------------------------------------------------------------------------*/
# Require RubyGems by default.
require 'rubygems'
# Require UtilityBelt for lots of functionality
# http://utilitybelt.rubyforge.org/
require 'utility_belt'
UtilityBelt::Equipper.equip(:defaults)
UtilityBelt::Equipper.equip(:with, :string_to_proc, :pipe, :not,
:language_greps, :is_an, :clipboard,
:interactive_editor)
def rating(*args)
options = args.extract_options!
category = options.delete(:category)
as_float = options.delete(:as_float)
certified = options.delete(:certified)
user_or_ip = options.delete(:user) || options.delete(:ip)
if user_or_ip && Rating.instance_methods.include?('user')
options[:conditions] = ["user_id = ?", user_or_ip.id]
elsif user_or_ip
>> e = Entry.find(:first, :conditions => {:name => 'MyConf'})
=> #<Entry id: 137, name: "MyConf", ....
>> e.ratings.average(:value)
=> 2.469
>> e.ratings.certified.average(:value)
=> 2.6507
>> e.rating(:as_float => true)
=> 2.6507
>> e.rating(:certified => true, :as_float => true)
=> 2.469
>> users = User.all
<< snip >>
>> def trim_percentage(users, cutoff)
>> above, below, novote = 0, 0, 0
>> users.each do |user|
?> avg = user.ratings.average(:value).to_f
>> if avg < 1
>> novote += 1
>> elsif avg < cutoff
>> below += 1