Sha256: 0ceb72aa9efce60d787525a646938392f1b9e10269e4a58ee91b7cec4e8fed1b

Contents?: true

Size: 1.94 KB

Versions: 25

Compression:

Stored size: 1.94 KB

Contents

require 'mime/types'

class Ckeditor::Asset
  include Mongoid::Document
  include Mongoid::Timestamps

  field :data
  field :data_file_name
  field :data_content_type
  field :data_file_size, :type => Integer
  field :assetable_id, :type => String
  field :assetable_type
  field :kind
  field :guid
  field :locale, :default => 0, :type => Integer

  mount_uploader :data, BlogImageUploader

  before_validation :make_content_type
  before_create :make_dimensions

  def url(*args)
    data.url(*args)
  end

  alias :public_filename :url

  def filename
    data_file_name
  end

  def content_type
    data_content_type
  end

  def size
    data_file_size
  end

  def path
    data.path
  end

  def styles
    data.styles
  end

  def format_created_at
    I18n.l(self.created_at, :format=>"%d.%m.%Y %H:%M")
  end

  def to_xml(options = {})
    builder = options[:builder] ||= Nokogiri::XML::Builder.new(options)

    builder.send(self.type.to_s.downcase) do |xml|
      xml.id_ self.id
      xml.filename self.filename
      xml.size self.size
      xml.path self.url

      xml.styles do
        self.styles.each do |style|
          xml.send(style.first, self.url(style.first))
        end
      end unless self.styles.empty?
    end

    builder.to_xml
  end

  def has_dimensions?
    self.respond_to?(:width) && self.respond_to?(:height)
  end

  def image?
    ["image/jpeg", "image/tiff", "image/png", "image/gif", "image/bmp"].include?(self.data_content_type)
  end

  def geometry
    @geometry ||= Paperclip::Geometry.from_file(data.to_file)
    @geometry
  end

  private

    def make_dimensions
      if image? && has_dimensions?
        self.width = geometry.width
        self.height = geometry.height
      end
    end

    def make_content_type
      if data_content_type == "application/octet-stream"
        content_types = MIME::Types.type_for(filename)
        self.data_content_type = content_types.first.to_s unless content_types.empty?
      end
    end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
blog_logic-1.4.5 app/models/ckeditor/asset.rb
blog_logic-1.4.4 app/models/ckeditor/asset.rb
blog_logic-1.4.3 app/models/ckeditor/asset.rb
blog_logic-1.4.2 app/models/ckeditor/asset.rb
blog_logic-1.4.1 app/models/ckeditor/asset.rb
blog_logic-1.4.0 app/models/ckeditor/asset.rb
blog_logic-1.3.1 app/models/ckeditor/asset.rb
blog_logic-1.3.0 app/models/ckeditor/asset.rb
blog_logic-1.2.4 app/models/ckeditor/asset.rb
blog_logic-1.2.3 app/models/ckeditor/asset.rb
blog_logic-1.2.2 app/models/ckeditor/asset.rb
blog_logic-1.2.1 app/models/ckeditor/asset.rb
blog_logic-1.2.0 app/models/ckeditor/asset.rb
blog_logic-1.1.9 app/models/ckeditor/asset.rb
blog_logic-1.1.8 app/models/ckeditor/asset.rb
blog_logic-1.1.7 app/models/ckeditor/asset.rb
blog_logic-1.1.6 app/models/ckeditor/asset.rb
blog_logic-1.1.5 app/models/ckeditor/asset.rb
blog_logic-1.1.4 app/models/ckeditor/asset.rb
blog_logic-1.1.3 app/models/ckeditor/asset.rb