Skip to content

Instantly share code, notes, and snippets.

@thephw
Created November 8, 2013 16:40
Show Gist options
  • Select an option

  • Save thephw/7373825 to your computer and use it in GitHub Desktop.

Select an option

Save thephw/7373825 to your computer and use it in GitHub Desktop.
My weird way of not rewriting all of the json for to_json and to_indexed_json
# ...
# Helper Function
def index_content
require 'open-uri'
if self.content.present? && self.content.url.present? && self.content.url.downcase.ends_with?("pdf")
uri = URI.join HOSTNAME, self.content.url
io = open(uri)
reader = PDF::Reader.new(io)
reader.pages.collect(&:text).join
end
end
# Class level helper function
def self.index_content(filepath)
reader = PDF::Reader.new(filepath)
reader.pages.collect(&:text).join
end
#Overwrite as_json which generates the hash for to_json
def as_json(options={})
return super(options) if options.include?(:skip)
default_options = {
:methods => [:index_content]
}
all_options = default_options.merge(options)
super(all_options)
end
#For elasticsearch indexing
def to_indexed_json
to_json(methods: [:tag_list]) #For instance if we also want to index the tag_list method
end
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment