Skip to content

Instantly share code, notes, and snippets.

View ridiculous's full-sized avatar

Ryan Buckley ridiculous

  • California
View GitHub Profile
class PostsController < ActionController::Base
def create
Post.create(post_params)
end
def update
Post.find(params[:id]).update_attributes!(post_params)
end
private
@ridiculous
ridiculous / gist:5428893
Last active December 16, 2015 11:39
Gotta love way you can write stuff in Ruby!
3.years.ago.send([:+, :-].sample, rand(100).days)
@ridiculous
ridiculous / index.html
Created September 25, 2013 17:52 — forked from mbostock/.block
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.29.1"></script>
<style type="text/css">
body {
background: #000;
}
@ridiculous
ridiculous / arel_hackery.rb
Last active August 29, 2015 13:59
Use Arel directly to substitute default "AND" operator with "OR" for joining where() chains
class Hackery < Arel::Visitors::ToSql
def quack(o)
"WHERE #{o.wheres.map { |x| visit x, nil }.join ' OR ' }"
end
end
hack = Hackery.new(ActiveRecord::Base.connection)
condition1 = {:social_network_type => "facebook", :social_network_id => "555555"}
condition2 = {:social_network_type => "linkedin", :social_network_id => "555555"}
hack.quack(SocialUser.where(condition1).where(condition2))
@ridiculous
ridiculous / object_tracker.rb
Last active June 25, 2019 09:47
Method tracking. Could be used to unobtrusively track method calls
module ObjectTracker
def track(*args)
@__tracking ||= []
args.each { |arg| @__tracking << arg unless tracking?(arg) }
end
def track!(*args)
track(*args)
start_tracking!
end
@ridiculous
ridiculous / notes.rb
Last active August 29, 2015 14:11
Notes + operator
def +(other)
new_data = data.merge(other.data) do |_, val, other_val|
case val
when Array, Integer
val + other_val
when Hash
val.merge(other_val) { |__, val1, val2| val1 + val2 }
else
nil
end
module HashieMashExtensions
# To play nice with ActiveSupport Array#extract_options!
def extractable_options?
true
end
end
Hashie::Mash.send(:include, HashieMashExtensions)
class Merchant
include Cequel::Record
key :user_id, :int
key :source, :text
key :source_id, :text
column :name, :text
# @example
# Merchant.by_key(user_id = 1, source = 'facebook', source_id = '4213578641d')
USER_AGENT_ALIASES = ['Windows Mozilla', 'Mac Safari', 'Mac FireFox', 'Mac Mozilla', 'Linux Mozilla', 'Linux Firefox']
agent = Mechanize.new do |mech|
mech.open_timeout = 10
mech.read_timeout = 10
mech.follow_meta_refresh = true
mech.keep_alive = true
mech.max_history = 1
mech.user_agent_alias = USER_AGENT_ALIASES.sample
end
agent.set_proxy(proxy_ip, proxy_port, AppConfig.http_proxy.username, AppConfig.http_proxy.password)
class DummyController
include Logging
protected
def log_file
'log_file_name'
end
end