Skip to content

Instantly share code, notes, and snippets.

View rubypanther's full-sized avatar

Paris Sinclair rubypanther

  • Eugene, Oregon, USA
View GitHub Profile
class ApiKey < ActiveRecord::Base
belongs_to :user
around_create :generate_api_key
def generate_api_key
self.api_key = ""
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-.,"
@rubypanther
rubypanther / undefined_method_oddity_spec.rb
Created April 1, 2012 10:36
An RSpec "undefined method" oddity
class UndefinedMethodOddity
def foo
bar 1
bar 2
end
def bar(arg)
end
end
def self.active
s = arel_table
where(
s[:begins_at].lteq(Time.now.utc).or(s[:begins_at].eq(nil)).and(
s[:ends_at].gt(Time.now.utc).or(s[:ends_at]).eq(nil))
)
end
scope :voted_since, lambda{|since|
joins(:votes)
.where("`votes`.created_at >= ? AND `votes`.project_id = `users`.project_id", since.to_s(:db))
.group("`votes`.user_id")
.order("COUNT(`votes`.user_id) DESC")
}
def last_activity_description(date, user_id)
user_activity = UserActivity.find(:first, :conditions => ["Date(updated_at) <= ? and user_id = ?", dat
e, user_id], :order => 'updated_at DESC')
user_activity.activity.description
end
# better
def last_activity(date, user_id)
UserActivity.find(:first, :conditions => ["Date(updated_at) <= ? and user_id = ?", dat
e, user_id], :order => 'updated_at DESC').activity
def last_activity(date, user_id)
active_id = UserActivity.find(:first, :conditions => ["Date(updated_at) <= ? and user_id = ?", dat
e, user_id], :order => 'updated_at DESC').activity_id
end
digraph G {
size = "5,5"; node[fontsize=16];
struct3 [shape=record,label="hello\nworld |{ b |{c|<here> d|e}| f}| g | h"];
// incoming
client -> server;
server -> client;
}
class Photo
def photo_filename= filename
if (self.photo = File.read(filename) rescue nil)
save!
end
end
end
# ...