2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
|
|
|
class ContentType
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
|
|
|
|
## extensions ##
|
2011-12-19 13:15:11 +00:00
|
|
|
include CustomFields::Source
|
2011-10-30 23:02:41 +00:00
|
|
|
include Extensions::ContentType::ItemTemplate
|
|
|
|
|
|
|
|
## fields ##
|
|
|
|
field :name
|
|
|
|
field :description
|
|
|
|
field :slug
|
2012-01-09 14:49:59 +00:00
|
|
|
field :label_field_id, :type => BSON::ObjectId
|
2012-01-02 13:54:01 +00:00
|
|
|
field :label_field_name
|
2012-01-09 14:49:59 +00:00
|
|
|
field :group_by_field_id, :type => BSON::ObjectId
|
2011-10-30 23:02:41 +00:00
|
|
|
field :order_by
|
2012-01-09 14:49:59 +00:00
|
|
|
field :order_direction, :default => 'asc'
|
|
|
|
field :public_submission_enabled, :type => Boolean, :default => false
|
|
|
|
field :public_submission_accounts, :type => Array
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## associations ##
|
2011-12-16 12:02:10 +00:00
|
|
|
belongs_to :site, :class_name => 'Locomotive::Site'
|
2011-12-22 23:45:32 +00:00
|
|
|
has_many :entries, :class_name => 'Locomotive::ContentEntry', :dependent => :destroy
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## named scopes ##
|
|
|
|
scope :ordered, :order_by => :updated_at.desc
|
|
|
|
|
|
|
|
## indexes ##
|
|
|
|
index [[:site_id, Mongo::ASCENDING], [:slug, Mongo::ASCENDING]]
|
|
|
|
|
|
|
|
## callbacks ##
|
2011-11-03 13:01:08 +00:00
|
|
|
before_validation :normalize_slug
|
2011-12-21 14:24:39 +00:00
|
|
|
after_validation :bubble_fields_errors_up
|
2011-11-03 13:01:08 +00:00
|
|
|
before_save :set_default_values
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## validations ##
|
2011-11-03 13:01:08 +00:00
|
|
|
validates_presence_of :site, :name, :slug
|
2011-10-30 23:02:41 +00:00
|
|
|
validates_uniqueness_of :slug, :scope => :site_id
|
2011-12-22 23:45:32 +00:00
|
|
|
validates_size_of :entries_custom_fields, :minimum => 1, :message => :array_too_short
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## behaviours ##
|
2011-12-22 23:45:32 +00:00
|
|
|
custom_fields_for :entries
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def order_manually?
|
|
|
|
self.order_by == '_position'
|
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def order_by_definition
|
|
|
|
direction = self.order_manually? ? 'asc' : self.order_direction || 'asc'
|
|
|
|
[order_by_attribute, direction]
|
2011-12-22 00:31:33 +00:00
|
|
|
end
|
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
def ordered_entries(conditions = {})
|
|
|
|
self.entries.order_by([order_by_definition]).where(conditions)
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def groupable?
|
|
|
|
!!self.group_by_field && group_by_field.type == 'select'
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_by_field
|
|
|
|
self.entries_custom_fields.find(self.group_by_field_id) rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_or_group_entries
|
|
|
|
if self.groupable?
|
|
|
|
self.entries.group_by_select_option(self.group_by_field.name, self.order_by_definition)
|
|
|
|
else
|
|
|
|
self.ordered_entries
|
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2011-12-21 14:24:39 +00:00
|
|
|
def to_presenter
|
|
|
|
Locomotive::ContentTypePresenter.new(self)
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2011-12-21 14:24:39 +00:00
|
|
|
def as_json(options = {})
|
|
|
|
self.to_presenter.as_json
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def order_by_attribute
|
|
|
|
return self.order_by if %w(created_at updated_at _position).include?(self.order_by)
|
|
|
|
self.entries_custom_fields.find(self.order_by).name rescue 'created_at'
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
def set_default_values
|
|
|
|
self.order_by ||= 'created_at'
|
2012-01-09 14:49:59 +00:00
|
|
|
self.label_field_id = self.entries_custom_fields.first._id if self.label_field_id.blank?
|
|
|
|
field = self.entries_custom_fields.find(self.label_field_id)
|
2012-01-02 13:54:01 +00:00
|
|
|
self.label_field_name = field.name
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_slug
|
|
|
|
self.slug = self.name.clone if self.slug.blank? && self.name.present?
|
|
|
|
self.slug.permalink! if self.slug.present?
|
|
|
|
end
|
|
|
|
|
2011-12-21 14:24:39 +00:00
|
|
|
def bubble_fields_errors_up
|
2011-12-22 23:45:32 +00:00
|
|
|
return if self.errors[:entries_custom_fields].empty?
|
2011-12-21 14:24:39 +00:00
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
self.errors.set(:entries_custom_fields, [])
|
2011-12-21 14:24:39 +00:00
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
self.entries_custom_fields.each do |field|
|
2011-12-21 14:24:39 +00:00
|
|
|
next if field.valid?
|
2011-12-22 23:45:32 +00:00
|
|
|
self.errors.add(:entries_custom_fields, field.errors.to_a)
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2011-11-20 10:59:12 +00:00
|
|
|
end
|
2012-01-09 14:49:59 +00:00
|
|
|
|
|
|
|
# def list_or_group_contents
|
|
|
|
# if self.groupable?
|
|
|
|
# groups = self.contents.klass.send(:"group_by_#{self.group_by_field._alias}", :ordered_contents)
|
|
|
|
#
|
|
|
|
# # look for items with no category or unknown ones
|
|
|
|
# items_without_category = self.contents.find_all { |c| !self.group_by_field.category_ids.include?(c.send(self.group_by_field_name)) }
|
|
|
|
# if not items_without_category.empty?
|
|
|
|
# groups << { :name => nil, :items => items_without_category }
|
|
|
|
# else
|
|
|
|
# groups
|
|
|
|
# end
|
|
|
|
# else
|
|
|
|
# self.ordered_contents
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def latest_updated_contents
|
|
|
|
# self.contents.latest_updated.reject { |c| !c.persisted? }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def ordered_contents(conditions = {})
|
|
|
|
# column = self.order_by.to_sym
|
|
|
|
#
|
|
|
|
# list = (if conditions.nil? || conditions.empty?
|
|
|
|
# self.contents
|
|
|
|
# else
|
|
|
|
# conditions_with_names = {}
|
|
|
|
#
|
|
|
|
# conditions.each do |key, value|
|
|
|
|
# # convert alias (key) to name
|
|
|
|
# field = self.entries_custom_fields.detect { |f| f._alias == key }
|
|
|
|
#
|
|
|
|
# case field.kind.to_sym
|
|
|
|
# when :category
|
|
|
|
# if (category_item = field.category_items.where(:name => value).first).present?
|
|
|
|
# conditions_with_names[field._name.to_sym] = category_item._id
|
|
|
|
# end
|
|
|
|
# else
|
|
|
|
# conditions_with_names[field._name.to_sym] = value
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# self.contents.where(conditions_with_names)
|
|
|
|
# end).sort { |a, b| (a.send(column) && b.send(column)) ? (a.send(column) || 0) <=> (b.send(column) || 0) : 0 }
|
|
|
|
#
|
|
|
|
# return list if self.order_manually?
|
|
|
|
#
|
|
|
|
# self.asc_order? ? list : list.reverse
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def sort_contents!(ids)
|
|
|
|
# ids.each_with_index do |id, position|
|
|
|
|
# self.contents.find(BSON::ObjectId(id))._position_in_list = position
|
|
|
|
# end
|
|
|
|
# self.save
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def group_by_field
|
|
|
|
# @group_by_field ||= self.entries_custom_fields.detect { |f| f._name == self.group_by_field_name }
|
|
|
|
# end
|