2010-06-01 00:06:46 +00:00
|
|
|
class AssetCollection
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
include Locomotive::Mongoid::Document
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
## fields ##
|
2010-06-10 14:50:50 +00:00
|
|
|
field :name
|
|
|
|
field :slug
|
2010-09-15 00:19:34 +00:00
|
|
|
field :internal, :type => Boolean, :default => false
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
## associations ##
|
2010-07-31 02:15:24 +00:00
|
|
|
referenced_in :site
|
2010-05-21 00:41:33 +00:00
|
|
|
embeds_many :assets
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-17 22:51:53 +00:00
|
|
|
## behaviours ##
|
2010-05-19 23:42:19 +00:00
|
|
|
custom_fields_for :assets
|
2010-05-30 23:57:33 +00:00
|
|
|
liquid_methods :name, :ordered_assets
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
## callbacks ##
|
2010-07-20 10:15:53 +00:00
|
|
|
before_validation :normalize_slug
|
2010-05-12 00:16:39 +00:00
|
|
|
before_save :store_asset_positions!
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
## validations ##
|
|
|
|
validates_presence_of :site, :name, :slug
|
|
|
|
validates_uniqueness_of :slug, :scope => :site_id
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-09-15 00:19:34 +00:00
|
|
|
## named scopes ##
|
|
|
|
scope :internal, :where => { :internal => true }
|
2010-09-15 15:48:10 +00:00
|
|
|
scope :not_internal, :where => { :internal => false }
|
2010-09-15 00:19:34 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
## methods ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
def ordered_assets
|
|
|
|
self.assets.sort { |a, b| (a.position || 0) <=> (b.position || 0) }
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
def assets_order
|
|
|
|
self.ordered_assets.collect(&:id).join(',')
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
def assets_order=(order)
|
|
|
|
@assets_order = order
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-09-15 00:19:34 +00:00
|
|
|
def self.find_or_create_internal(site)
|
2010-09-15 15:48:10 +00:00
|
|
|
site.asset_collections.internal.first || site.asset_collections.create(:name => 'system', :slug => 'system', :internal => true)
|
2010-09-15 00:19:34 +00:00
|
|
|
end
|
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
protected
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
def normalize_slug
|
2010-07-23 20:09:54 +00:00
|
|
|
self.slug = self.name.clone if self.slug.blank? && self.name.present?
|
2010-05-12 00:16:39 +00:00
|
|
|
self.slug.slugify! if self.slug.present?
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
def store_asset_positions!
|
2010-10-19 10:20:09 +00:00
|
|
|
return if @assets_order.blank?
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
ids = @assets_order.split(',').collect { |id| BSON::ObjectId(id) }
|
|
|
|
|
|
|
|
ids.each_with_index do |asset_id, index|
|
2010-05-12 00:16:39 +00:00
|
|
|
self.assets.find(asset_id).position = index
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-24 09:14:08 +00:00
|
|
|
self.assets.clone.each do |asset|
|
2010-10-19 10:20:09 +00:00
|
|
|
if !ids.include?(asset._id)
|
2010-07-23 20:09:54 +00:00
|
|
|
self.assets.delete(asset)
|
2010-05-12 00:16:39 +00:00
|
|
|
asset.send(:delete)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|