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 / 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 / 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 / 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
describe ProductSearch do
describe '#sanitize_term' do
it 'only allows printable characters' do
# ...
@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
// curl -X GET 'localhost:9200/development_products/_search?pretty=true' -d '
{
explain: true,
query: {
dis_max: {
queries: [
{ field: { "name": "grmlock" } },
{ field: { "catalog.name": "grmlock" } },
{ field: { "items.name": "grmlock" } },
{ field: { "properties.character": "grmlock" } },
@nickhoffman
nickhoffman / gist:1283380
Created October 13, 2011 04:30
Why does a search for a misspelling ("optius" instead of "optimus") find no documents?
echo 'Delete the index.'
curl -X DELETE 'http://localhost:9200/test_products/?pretty=true'
echo; echo
echo 'Create the index. Copied directly from https://gist.github.com/961303 .'
curl -X PUT 'http://localhost:9200/test_products/?pretty=true' -d '
{
"settings" : {
"analysis" : {
@nickhoffman
nickhoffman / gist:1282105
Created October 12, 2011 18:32 — forked from clintongormley/gist:961303
Ngram example
###################
# CREATE THE INDEX:
###################
curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' -d '
{
"settings" : {
"analysis" : {
"filter" : {
"edge_ngram" : {
"side" : "front",
class ProductHaveCell < Cell::Rails
def description(have)
condition = have.condition
desc = "#{have.quantity}x #{condition.name}"
if have.sealed
desc += ' ' + I18n.t('words.Sealed').downcase
elsif have.packaging
desc += ' ' + I18n.t('phrases.product_haves.with_packaging')
end
class SessionsController < Devise::SessionsController
def create
if request.xhr?
resource = warden.authenticate! :scope => resource_name
if resource.nil?
render :json => {
:status => 'fail',
:data => {
:cause => 'invalid',