Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Hoffman nickhoffman

  • Toronto, Canada
View GitHub Profile
View user_spec.rb
describe ProductSearch do
describe '#sanitize_term' do
it 'only allows printable characters' do
# ...
@nickhoffman
nickhoffman / query_output.txt
Created Nov 16, 2011
ElasticSearch: sorting on strings doesn't support the "missing" option
View query_output.txt
// Setup
{"ok":true,"acknowledged":true}
{"ok":true,"acknowledged":true}
{"ok":true,"_index":"test","_type":"tweets","_id":"1","_version":1}
{"ok":true,"_index":"test","_type":"tweets","_id":"2","_version":1}
{"ok":true,"_index":"test","_type":"tweets","_id":"3","_version":1}
// The first query, with the "missing" option, fails.
{
"error" : "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[Z5lN30goQ0euwj3ce-OPrA][test][1]: SearchParseException[[test][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{ sort : [ { name : {order : \"asc\", missing: \"_last\"} } ] }\n]]]; nested: ElasticSearchIllegalArgumentException[Sorting on string type field does not support missing parameter]; }{[Z5lN30goQ0euwj3ce-OPrA][test][4]: SearchParseException[[test][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{ sort : [ { name : {order : \"asc\", missing: \"_last\"} } ] }\n]]]; nested: ElasticSearchIllegalArgumentException[Sorting on string type field d
@nickhoffman
nickhoffman / gist:1562222
Created Jan 4, 2012
How to find filterable values when each result can have different filterable fields
View gist:1562222

Say you have an online store, and each product belongs to one catalog. In each catalog, you specify what custom attributes its products have. Eg:

  • Catalog: Transformers
    • Product Attributes: Character, Allegiance
  • Catalog: Games
    • Product Attributes: Number of Players, Average Game Time

When viewing a catalog, you want to enable users to filter by any custom product attribute. This is done by choosing which attribute to filter by from a dropdown, and then choosing the desired value for that attribute from a dropdown. Eg: filter products where “Character” is “Megatron”.

Here’s the question: How do you determine what values to put in the second dropdown?

@nickhoffman
nickhoffman / gist:1590041
Created Jan 10, 2012
An alternative to halting in a helper
View gist:1590041
get '/users/:user_id/projects/:project_id/tasks-due-on/:task_date' do
user = User.find(params[:user_id])
halt 404 unless user
project = user.projects.find(params[:project_id])
halt 404 unless project
# etc...
end
@nickhoffman
nickhoffman / option_1.rb
Created Jan 13, 2012
Would you duplicate the validation's regex, or reference a constant?
View option_1.rb
class User
field :username, :type => String
validates :username, :format => { :with => /\A[[:print:] ]{3,20}\Z/ }
end
class ScoreCard
belongs_to :user
field :username, :type => String
View query_with_exclamation.sh
curl 'localhost:9200/development_products/product/_search?pretty=true' -d '
{
"query" : {
"dis_max" : {
"queries" : [
{ "field" : {"name" : "Arise!"}},
{ "field" : {"catalog.name" : "Arise!"}}
]
}
}
@nickhoffman
nickhoffman / gist:1666614
Created Jan 24, 2012
Nesting text within a content tag.
View gist:1666614
# The HTML that should be generated is:
# <h3 class="curated_by">Curated by <span>Bob</span></h3>
# Please tell me that there's a better way to do this.
def curated_by
html = ''
html += h.content_tag(:h3, :class => 'curated_by') do
h.concat h.t('phrases.Curated_by')
h.concat ' '
@nickhoffman
nickhoffman / gist:2481672
Created Apr 24, 2012
MySQL's "REPEATABLE READ" and "SELECT ... FOR UPDATE"
View gist:2481672
mysql> show create table numbers\G
*************************** 1. row ***************************
Table: numbers
Create Table: CREATE TABLE `numbers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
View output.txt
The first context
Creating Person with name Nick
✗ Errored » callback not fired
in Create a Person via JavaScript: When a person has a name,
in Creating a Person
in undefined✗ Errored » 1 errored ∙ 1 dropped
View true_false_comparison.rb
module TrueFalseComparison
# Enable TrueClass and FalseClass values to be comparable.
# TrueClass values are valued higher than FalseClass values.
#
# See this article for more info:
# http://grosser.it/2010/07/30/ruby-true-false-comparison-with/
#
# Yes, this is evil monkey-patching. The alternative is much, much worse:
#
# [false, true, false].sort {|a, b| a ? (b ? 0 : -1) : (b ? 1 : 0) }