app/models/concerns/loft_asset.rb in loft-0.2.9 vs app/models/concerns/loft_asset.rb in loft-0.3.0
- old
+ new
@@ -1,22 +1,15 @@
-require 'autoinc'
-
module LoftAsset
extend ActiveSupport::Concern
-
included do
-
include Mongoid::Timestamps
include Mongoid::Autoinc
include Mongoid::Search
-
include Ants::Id
-
include ActionView::Helpers::DateHelper
include ActionView::Helpers::NumberHelper
-
## Attributes
field :name, default: ''
field :filename, default: ''
field :size, type: Integer
field :humanized_size, default: ''
@@ -29,98 +22,83 @@
# increment value, used by uploader
field :_number, type: Integer
increments :_number
-
## Uploaders
mount_uploader :file, AssetFileUploader
-
## Validations
validates :file, presence: true
-
## Search
search_in :name, :filename
-
## Scopes
- default_scope -> { desc(:created_at) }
+ default_scope -> { desc(:created_at) }
scope :by_type, -> asset_type { where(type: asset_type) }
+ scope :images, -> { where(type: "image") }
+ scope :not_images, -> { where(:type.ne => "image" ) }
-
## Indexes
index({ created_at: -1 })
-
## Callbacks
before_save :update_asset_attributes
-
## Helpers
def _list_item_title
name
end
-
def _list_item_subtitle
time_ago_in_words(self.created_at) + " ago"
end
-
def _list_item_thumbnail
if is_image?
{ medium: file._200x150_2x.url, small: file._40x40_2x.url }
else
{}
end
end
-
def content_type
@content_type ||= file.content_type
end
-
def is_image?
return false unless file?
content_type.match(/image\//) ? true : false
end
-
def is_text?
return false unless file?
content_type.match(/text\//) ? true : false
end
-
def is_pdf?
return false unless file?
content_type.match(/pdf/) ? true : false
end
-
def is_archive?
return false unless file?
# need to add more archive types: rar, gz, bz2, gzip
content_type.match(/zip/) ? true : false
end
-
def is_audio?
return false unless file?
content_type.match(/audio\//) ? true : false
end
-
def is_video?
return false unless file?
content_type.match(/video\//) ? true : false
end
-
def update_asset_attributes
if file.present? && file_changed?
# save original file name for search
self.filename = file.file.original_filename
@@ -142,8 +120,7 @@
# use filename as an asset name if name is empty ''
self.name = self.name.empty? ? self.filename : self.name
end
private :update_asset_attributes
-
end
end