Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created May 6, 2011 19:29
Show Gist options
  • Select an option

  • Save jashkenas/959620 to your computer and use it in GitHub Desktop.

Select an option

Save jashkenas/959620 to your computer and use it in GitHub Desktop.
class Docdata < ActiveRecord::Base
belongs_to :document
after_save :index_document
def self.to_hstore(hash)
hash.map {|k, v| "\"#{sanitize(k)}\"=>\"#{sanitize(v)}\"" }.join(',')
end
def self.sanitize(obj)
obj.to_s.gsub(/[\\"]/, '')
end
def data=(hash)
self[:data] = Docdata.to_hstore(hash)
end
def data
@data ||= Hash[self[:data].scan(/"(.*?[^\\]|)"=>"(.*?[^\\]|)"/)]
end
def index_document
self.document.index
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment