Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e8662e5b31
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 39 lines (30 sloc) 0.922 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
class GistFile < ActiveRecord::Base

  attr_accessible :gist_id, :filename, :raw_url, :language, :file_type, :content, :size_bytes

  belongs_to :gist

  class << self

    def import(gh_gist)

      gist = Gist.where(gh_id: gh_gist['id']).first

      gh_gist.files.each do |filename, gh_file|

        attributes = {
          gist_id: gist.id, filename: gh_file.filename, raw_url: gh_file.raw_url,
          language: gh_file.language, file_type: gh_file.type, size_bytes: gh_file['size'],
          content: gh_file.content
        }

        if(existing_file = where(gist_id: gist.id, filename: gh_file.filename).first)
          existing_file.update_attributes(attributes)
          existing_file
        else
          create(attributes)
        end
      end
    end
  end

  def indexed_attributes
    {
      name: filename,
      content: content,
      language: language,
      type: file_type
    }
  end
end
Something went wrong with that request. Please try again.