2010-06-01 00:06:46 +00:00
|
|
|
class ContentType
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
2010-05-24 00:18:23 +00:00
|
|
|
|
|
|
|
## fields ##
|
|
|
|
field :name
|
|
|
|
field :description
|
|
|
|
field :slug
|
|
|
|
field :order_by
|
2010-05-25 00:32:12 +00:00
|
|
|
field :highlighted_field_name
|
2010-06-10 14:50:50 +00:00
|
|
|
field :api_enabled, :type => Boolean, :default => false
|
2010-05-24 00:18:23 +00:00
|
|
|
|
|
|
|
## associations ##
|
|
|
|
belongs_to_related :site
|
|
|
|
embeds_many :contents, :class_name => 'ContentInstance'
|
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
before_validate :normalize_slug
|
2010-05-26 00:41:10 +00:00
|
|
|
before_save :set_default_values
|
2010-05-24 00:18:23 +00:00
|
|
|
|
|
|
|
## validations ##
|
|
|
|
validates_presence_of :site, :name, :slug
|
|
|
|
validates_uniqueness_of :slug, :scope => :site
|
2010-05-26 00:41:10 +00:00
|
|
|
validates_size_of :content_custom_fields, :minimum => 1, :message => :array_too_short
|
2010-05-24 00:18:23 +00:00
|
|
|
|
|
|
|
## behaviours ##
|
2010-05-25 00:32:12 +00:00
|
|
|
custom_fields_for :contents
|
2010-05-24 00:18:23 +00:00
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def ordered_contents(conditions = {})
|
2010-05-26 00:41:10 +00:00
|
|
|
column = self.order_by.to_sym
|
2010-05-30 23:57:33 +00:00
|
|
|
|
|
|
|
(if conditions.nil? || conditions.empty?
|
|
|
|
self.contents
|
|
|
|
else
|
|
|
|
self.contents.where(conditions)
|
|
|
|
end).sort { |a, b| (a.send(column) || 0) <=> (b.send(column) || 0) }
|
2010-05-25 00:32:12 +00:00
|
|
|
end
|
|
|
|
|
2010-05-26 00:41:10 +00:00
|
|
|
def sort_contents!(order)
|
|
|
|
order.split(',').each_with_index do |id, position|
|
|
|
|
self.contents.find(id)._position_in_list = position
|
|
|
|
end
|
|
|
|
self.save
|
2010-05-25 00:32:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def highlighted_field
|
|
|
|
self.content_custom_fields.detect { |f| f._name == self.highlighted_field_name }
|
|
|
|
end
|
|
|
|
|
2010-05-24 00:18:23 +00:00
|
|
|
protected
|
|
|
|
|
2010-05-26 00:41:10 +00:00
|
|
|
def set_default_values
|
|
|
|
self.order_by ||= 'updated_at'
|
|
|
|
self.highlighted_field_name ||= self.content_custom_fields.first._name
|
|
|
|
end
|
|
|
|
|
2010-05-24 00:18:23 +00:00
|
|
|
def normalize_slug
|
|
|
|
self.slug = self.name.clone if self.slug.blank? && self.name.present?
|
|
|
|
self.slug.slugify! if self.slug.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|