Sha256: 9e2750ddb184be1c1d815cbca5986a3660cd57914c9c90b092349f3d98201ca3

Contents?: true

Size: 1.61 KB

Versions: 16

Compression:

Stored size: 1.61 KB

Contents

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
tawork-0.0.21 app/models/attachment.rb
tawork-0.0.20 app/models/attachment.rb
tawork-0.0.19 app/models/attachment.rb
tawork-0.0.18 app/models/attachment.rb
tawork-0.0.17 app/models/attachment.rb
tawork-0.0.16 app/models/attachment.rb
tawork-0.0.15 app/models/attachment.rb
tawork-0.0.14 app/models/attachment.rb
tawork-0.0.13 app/models/attachment.rb
tawork-0.0.12 app/models/attachment.rb
tawork-0.0.11 app/models/attachment.rb
tawork-0.0.10 app/models/attachment.rb
tawork-0.0.9 app/models/attachment.rb
tawork-0.0.8 app/models/attachment.rb
tawork-0.0.7 app/models/attachment.rb
tawork-0.0.6 app/models/attachment.rb