class Attachment < ActiveRecord::Base include Tire::Model::Search include Tire::Model::Callbacks # after_save :save_to_git belongs_to :attachable, :polymorphic => true validates_presence_of :attachable def self.create_from_uploaded_file(file, user, options = {}) filename = file.original_filename attachment = Attachment.create( options.merge( filename: filename, content_type: file.content_type, size: file.size, )) if attachment.errors.none? attachment.save_to_gollum( file, user, "uploading file: #{filename}" ) end attachment end def update(file, user) filename = file.original_filename if update_attributes( filename: filename, content_type: file.content_type, size: file.size) save_to_gollum( file, user, "uploading file: #{filename}" ) end end def wiki_file $wiki.file(wiki_page.path) end def file_data wiki_file.raw_data end def wiki_page $wiki.page(wiki_attachment_data, nil, wiki_dir) end def versions wiki_page.versions end def save_to_gollum(file, user, message) commit = { message: message, name: user.name, email: user.email } data_file = file.try(:tempfile) || file if wiki_page.present? $wiki.update_page(wiki_page, wiki_page.name, wiki_page.format, data_file, commit) else $wiki.write_page(wiki_attachment_data, :textile, data_file, commit, wiki_dir) end end protected def wiki_attachment_data "#{id}-data" end def wiki_dir "/attachments/#{id}" end end