Skip to content

Instantly share code, notes, and snippets.

@rubish
rubish / publisher.rb
Last active October 1, 2019 08:26
Pub/Sub using ActiveSupport::Notifications
module Publisher
extend self
def broadcast_event(event_name, payload={})
if block_given?
ActiveSupport::Notifications.instrument(event_name, payload) do
yield
end
else
ActiveSupport::Notifications.instrument(event_name, payload)
@rubish
rubish / recreation.rb
Last active December 15, 2015 02:48
Mongoid duping embedded document doesn't clear metadata
class Address
include Mongoid::Document
field :line1, type: String
field :line2, type: String
field :city, type: String
embedded_in :addressable, polymorphic: true
end
class Person
@rubish
rubish / shallow_document.rb
Created March 5, 2013 23:28
custom serializer to handle root and embedded documents alike in the association. WARNING: behaves like a field rather than an association
##
# class Activity
# include Mongoid::Document
#
# field :trackable, type: ShallowDocument
# end
##
class ShallowDocument
include Mongoid::Fields::Serializable
@rubish
rubish / recreation.curl
Created November 14, 2011 08:23
ArrayIndexOutOfBound for ES nested facets
curl -X DELETE "http://localhost:9200/test_users"
curl -X POST "http://localhost:9200/test_users" -d '
{
"mappings":{
"user":{
"properties":{
"email":{ "index":"no", "type":"string" },
"alma_info":{
"type":"object",
@rubish
rubish / mapping.json
Created November 11, 2011 07:26
elasticsearch nested facets problem
{
"user":{
"properties":{
"profile_completeness":{ "type":"double" },
"created_at":{ "format":"dateOptionalTime", "type":"date" },
"user_type":{ "type":"string" },
"professions":{
"type":"nested",
"properties":{
"to":{ "format":"dateOptionalTime", "type":"date" },
@rubish
rubish / irb.txt
Created October 9, 2011 23:57
Tire/curl inconsistency
pry(main)> UserFilter.new(:college => College.first, :user_type => User::ALUMNI).tire_name_facets('virgil')
=> {"name"=>{"_type"=>"terms", "missing"=>0, "total"=>0, "other"=>0, "terms"=>[]}}
Tables:
Users(:id, :username, etc.)
Questions(:id, :text)
Answers(:id, :question_id, :text)
UserAnswers(:id, :user_id, :question_id, :answer_id, :importance)
AcceptedAnswers(:id, :user_answer_id, :answer_id)
Relations
Questions <-> Answers: one-to-many
Questions <-> UserAnswers: one-to-many
@rubish
rubish / free.txt
Created July 23, 2011 00:43
ES memory usage
rubish@elastic1:~$ free -m
total used free shared buffers cached
Mem: 496 476 19 0 6 71
-/+ buffers/cache: 397 98
Swap: 1023 677 346
@rubish
rubish / backtrace.txt
Created July 20, 2011 05:06
socket error, connection refused, while uploading file to cloudfiles using carrierwave-fog
excon (0.6.2) lib/excon/connection.rb:262:in `initialize'
excon (0.6.2) lib/excon/connection.rb:262:in `open'
excon (0.6.2) lib/excon/connection.rb:262:in `open_socket'
excon (0.6.2) lib/excon/connection.rb:203:in `connect'
excon (0.6.2) lib/excon/connection.rb:268:in `socket'
excon (0.6.2) lib/excon/connection.rb:148:in `request'
fog (0.7.2) lib/fog/core/connection.rb:20:in `request'
fog (0.7.2) lib/fog/providers/rackspace.rb:17:in `authenticate'
fog (0.7.2) lib/fog/storage/rackspace.rb:86:in `initialize'
fog (0.7.2) lib/fog/core/service.rb:58:in `new'
@rubish
rubish / _footer.html.erb
Created July 18, 2011 17:23 — forked from joemsak/_footer.html.erb
Using jQuery TokenInput with ActsAsTaggableOn