Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Hoffman nickhoffman

  • Toronto, Canada
View GitHub Profile
@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 / 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 / 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
View user_spec.rb
describe ProductSearch do
describe '#sanitize_term' do
it 'only allows printable characters' do
# ...
@nickhoffman
nickhoffman / app--helpers--application_helper.rb
Created Oct 18, 2011
pjax is awesome, but causes code within #content_for not to be rendered. Here's a solution.
View app--helpers--application_helper.rb
module ApplicationHelper
def content_for_or_pjax(name, &block)
request.headers['X-PJAX'] ? capture(&block) : content_for(name, &block)
end
end
View gist:1284790
// 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 Oct 13, 2011
Why does a search for a misspelling ("optius" instead of "optimus") find no documents?
View gist:1283380
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" : {
View gist:1282105
###################
# CREATE THE INDEX:
###################
curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' -d '
{
"settings" : {
"analysis" : {
"filter" : {
"edge_ngram" : {
"side" : "front",
View app-cells-product_have_cell.rb
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
View app-controllers-sessions_controller.rb
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',