Created
November 8, 2013 16:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ... | |
| # 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