Skip to content

Instantly share code, notes, and snippets.

View mrpunkin's full-sized avatar

Bryan Corey mrpunkin

View GitHub Profile
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")
}
module BreakBlank
def blank?
return to_a.blank?
end
end
ActiveRecord::Relation.send(:include, BreakBlank)
def self.by_growth(start, stop=Time.now)
votes = Vote.arel_table
select("`photos`.*, COALESCE(SUM(`votes`.value),0.0) AS score_diff")
.joins(:votes)
.where(votes[:created_at].gt(start), votes[:created_at].lt(stop))
.group(votes[:photo_id])
.order("score_diff DESC,score DESC")
end
def self.active
s = arel_table
where(
s[:begins_at].lteq(Time.now.utc).or(s[:begins_at].eq(nil)),
s[:ends_at].gt(Time.now.utc).or(s[:ends_at]).eq(nil))
)
end
@mrpunkin
mrpunkin / arel_based.rb
Created March 7, 2012 21:47
Using arel to query SQL functions
dc = Arel::Nodes::NamedFunction.new "DATE", [ p[:created_at] ] ## second argument must be an array
## Sub this...
arel.project("DATE(`photos`.`created_at`)")
## For this...
arel.project(dc.to_sql)
arel.to_sql >> "SELECT DATE(`photos`.`created_at`) FROM `photos` INNER JOIN `votes` ON `photos`.`id` = `votes`.`photo_id`"
set :max_asset_age, 2 ## Set asset age in minutes to test modified date against.
after "deploy:finalize_update", "deploy:assets:determine_modified_assets", "deploy:assets:conditionally_precompile"
namespace :deploy do
namespace :assets do
desc "Figure out modified assets."
task :determine_modified_assets, :roles => assets_role, :except => { :no_release => true } do
set :updated_assets, capture("find #{latest_release}/app/assets -type d -name .git -prune -o -mmin -#{max_asset_age} -type f -print", :except => { :no_release => true }).split
after "deploy:finalize_update", "deploy:assets:conditionally_precompile"
namespace :deploy do
namespace :assets do
desc "Remove callback for asset precompiling unless assets were updated based on Git fetch/merge."
task :conditionally_precompile, :roles => assets_role, :except => { :no_release => true } do
updated_assets = capture("cd #{latest_release}; git diff --name-only #{old_commit} #{new_commit}", :except => { :no_release => true }).split.find_all{|fname| File.dirname(fname).match(/^(vendor|app|lib)\/assets/) }
if(updated_assets.empty?)
def time_left(timestamp)
diff = timestamp - Time.zone.now
d = (diff / 86400).floor
h = ((diff / 3600) % 24).floor
m = ((diff / 60) % 60).round
{:days=>d, :hours=>h, :minutes=>m}
end
@mrpunkin
mrpunkin / flickrie_benchmark.rb
Created July 10, 2012 16:26
Benchmark looping through response hashes for _content keys.
require 'rubygems'
require 'json'
require 'benchmark'
user = JSON.parse(<<EOS)
{ "person": { "id": "19198543@N06", "nsid": "19198543@N06", "ispro": 1, "iconserver": "3280", "iconfarm": 4, "path_alias": "hopskipandjump", "gender": "M", "ignored": 0, "contact": 0, "friend": 0, "family": 0, "revcontact": 0, "revfriend": 0, "revfamily": 0,
"username": { "_content": "hopskipandjump" },
"location": { "_content": "" },
"timezone": { "label": "Central Time (US & Canada)", "offset": "-06:00" },
"description": { "_content": "Family" },
def profile_url
Rails.application.routes.url_helpers.user_url(self, :host=>project.domain)
end
def as_json(options={})
super({
:only=>[:first_name, :last_name],
:methods=>[:profile_url]
}.merge(options))
end