Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c8e341dc0b
Fetching contributors…

Cannot retrieve contributors at this time

file 92 lines (77 sloc) 2.647 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
class Gist < ActiveRecord::Base

  include Tire::Model::Search

  attr_accessible :gh_id, :user_id, :description, :url, :git_pull_url, :git_push_url, :public,
    :comment_count, :gh_created_at, :gh_updated_at

  belongs_to :user
  has_many :files, :class_name => 'GistFile', :dependent => :delete_all

  index_name BONSAI_INDEX_NAME

  mapping do
    indexes :description, :analyzer => 'snowball', :boost => 10
    indexes :gh_created_at, type: 'date'
    indexes :user_id, :analyzer => :not_analyzed
    indexes :files do
      indexes :filename, analyzer: 'keyword'
      indexes :content, analyzer: 'snowball'
      indexes :language, analyzer: 'keyword'
      indexes :file_type, analyzer: 'keyword'
    end
  end

  class << self

    def import(gh_gist)

      user = User.where(gh_id: gh_gist.user.id).first
      gh_id = gh_gist['id']

      attributes = {
        gh_id: gh_id, user_id: user.id, description: gh_gist.description,
        url: gh_gist.html_url, git_push_url: gh_gist.git_push_url, git_pull_url: gh_gist.git_pull_url,
        public: gh_gist.public, comment_count: gh_gist.comments,
        gh_created_at: gh_gist.created_at, gh_updated_at: gh_gist.updated_at
      }

      if(existing_gist = where(gh_id: gh_id).first)
        log({ns: self, fn: __method__, measure: true, at: 'gist-imported'}, user, existing_gist)
        existing_gist.update_attributes(attributes)
        existing_gist
      else
        new_gist = create(attributes)
        log({ns: self, fn: __method__, measure: true, at: 'gist-created'}, user, new_gist)
        new_gist
      end
    end

    def search(user, q)
      q = q.blank? ? '*' : q
      log({ns: self, fn: __method__, query: q, measure: true, at: 'search'}, user)
      log({ns: self, fn: __method__, query: q, measure: true}, user) do
        tire.search do
          query { string q }
          sort { by :gh_created_at, 'desc' }
          filter :term, :user_id => user.id
          highlight :description, :'files.content'
          size 15
        end
      end
    end

    def reindex
      log({ns: self, fn: __method__}) do
        find_each { |gist| gist.update_index }
        tire.index.refresh
      end
    end
  end

  def to_log
    { gist_id: id, gist_gh_id: gh_id, gist_description: description }
  end

  # Required for Tire/Elasticsearch
  def to_indexed_json
    indexed_attributes.to_json
  end

  def indexed_attributes
    {
      user_id: user_id,
      description: description,
      url: url,
      public: public?,
      gh_created_at: gh_created_at,
      gh_updated_at: gh_updated_at,
      files: files.collect(&:indexed_attributes)
    }
  end
end
Something went wrong with that request. Please try again.