Sha256: 10bc7398c32c82c47af7461aba7775ffae0611a3eba9f87451e31348cb94b8ab

Contents?: true

Size: 1.73 KB

Versions: 22

Compression:

Stored size: 1.73 KB

Contents

class Document < ActiveRecord::Base
  include SocialStream::Models::Object

  has_attached_file :file, 
                    :url => '/:class/:id.:content_type_extension',
                    :path => ':rails_root/documents/:class/:id_partition/:style/:filename.:extension'

  paginates_per 20
  
  validates_attachment_presence :file
  validates_presence_of :title
  
  before_validation(:on => :create) do
    set_title
  end
  
  define_index do
    activity_object_index

    indexes file_file_name, :as => :file_name
  end
  
  class << self 
    def new(*args)
      # If already called from subtype, continue through the stack
      return super if self.name != "Document"

      doc = super
      
      return doc if doc.file_content_type.blank?
      
      if klass = lookup_subtype_class(doc)
        return klass.new *args
      end

      doc
    end

    # Searches for the suitable class based on its mime type
    def lookup_subtype_class(doc)
      SocialStream::Documents.subtype_classes_mime_types.each_pair do |klass, mime_types|
        return klass.to_s.classify.constantize if mime_types.include?(doc.mime_type.to_sym)
      end

      nil
    end
  end

  # The Mime::Type of this document's file
  def mime_type
    Mime::Type.lookup(file_content_type)
  end

  # The type part of the {#mime_type}
  def mime_type_type_sym
    mime_type.to_s.split('/').first.to_sym
  end

  # {#mime_type}'s symbol
  def format
    mime_type.to_sym
  end

 # JSON, generic version for most documents
  def as_json(options = nil)
    {:id => id,
     :title => title,
     :description => description,
     :author => author.name,
     :src => file.to_s
    }
  end
  
  protected

  def set_title
    self.title = file_file_name if self.title.blank?
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
social_stream-2.2.1 documents/app/models/document.rb
social_stream-2.2.0 documents/app/models/document.rb
social_stream-documents-2.2.0 app/models/document.rb
social_stream-2.1.1 documents/app/models/document.rb
social_stream-2.1.0 documents/app/models/document.rb
social_stream-documents-2.1.0 app/models/document.rb
social_stream-2.0.4 documents/app/models/document.rb
social_stream-documents-2.0.4 app/models/document.rb
social_stream-2.0.3 documents/app/models/document.rb
social_stream-documents-2.0.3 app/models/document.rb
social_stream-2.0.2 documents/app/models/document.rb
social_stream-documents-2.0.2 app/models/document.rb
social_stream-2.0.1 documents/app/models/document.rb
social_stream-documents-2.0.1 app/models/document.rb
social_stream-2.0.0 documents/app/models/document.rb
social_stream-documents-2.0.0 app/models/document.rb
social_stream-2.0.0.beta3 documents/app/models/document.rb
social_stream-documents-2.0.0.beta3 app/models/document.rb
social_stream-2.0.0.beta2 documents/app/models/document.rb
social_stream-documents-2.0.0.beta2 app/models/document.rb