Skip to content

Instantly share code, notes, and snippets.

View nickhoffman's full-sized avatar

Nick Hoffman nickhoffman

  • Toronto, Canada
View GitHub Profile
@nickhoffman
nickhoffman / app--helpers--application_helper.rb
Created October 18, 2011 15:20
pjax is awesome, but causes code within #content_for not to be rendered. Here's a solution.
module ApplicationHelper
def content_for_or_pjax(name, &block)
request.headers['X-PJAX'] ? capture(&block) : content_for(name, &block)
end
end
describe ProductSearch do
describe '#sanitize_term' do
it 'only allows printable characters' do
# ...
@nickhoffman
nickhoffman / query_output.txt
Created November 16, 2011 20:15
ElasticSearch: sorting on strings doesn't support the "missing" option
// 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 January 4, 2012 21:23
How to find filterable values when each result can have different filterable fields

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 January 10, 2012 17:07
An alternative to halting in a helper
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 January 13, 2012 21:06
Would you duplicate the validation's regex, or reference a constant?
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
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 January 24, 2012 00:01
Nesting text within a content tag.
# 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 April 24, 2012 17:21
MySQL's "REPEATABLE READ" and "SELECT ... FOR UPDATE"
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)
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