2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
2011-12-22 23:45:32 +00:00
|
|
|
class ContentEntry
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
include Locomotive::Mongoid::Document
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## extensions ##
|
2011-12-19 13:15:11 +00:00
|
|
|
include ::CustomFields::Target
|
2011-10-30 23:02:41 +00:00
|
|
|
include Extensions::Shared::Seo
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
## fields ##
|
2011-10-30 23:02:41 +00:00
|
|
|
field :_slug
|
2012-02-01 01:01:42 +00:00
|
|
|
field :_label_field_name
|
2012-01-02 13:54:01 +00:00
|
|
|
field :_position, :type => Integer, :default => 0
|
|
|
|
field :_visible, :type => Boolean, :default => true
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## validations ##
|
2011-12-17 17:18:56 +00:00
|
|
|
validates :_slug, :presence => true, :uniqueness => { :scope => :content_type_id }
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## associations ##
|
2012-02-16 11:42:41 +00:00
|
|
|
belongs_to :site, :class_name => 'Locomotive::Site'
|
|
|
|
belongs_to :content_type, :class_name => 'Locomotive::ContentType', :inverse_of => :entries
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
before_validation :set_slug
|
2012-02-16 11:42:41 +00:00
|
|
|
before_save :set_site
|
2012-01-02 13:54:01 +00:00
|
|
|
before_save :set_visibility
|
2012-02-01 01:01:42 +00:00
|
|
|
before_save :set_label_field_name
|
2011-12-16 12:02:10 +00:00
|
|
|
before_create :add_to_list_bottom
|
2012-01-04 01:07:53 +00:00
|
|
|
after_create :send_notifications
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## named scopes ##
|
2011-12-16 12:02:10 +00:00
|
|
|
scope :visible, :where => { :_visible => true }
|
2012-03-01 23:31:45 +00:00
|
|
|
scope :latest_updated, :order_by => :updated_at.desc, :limit => Locomotive.config.ui[:latest_entries_nb]
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
|
|
|
alias :visible? :_visible?
|
|
|
|
alias :_permalink :_slug
|
|
|
|
alias :_permalink= :_slug=
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def _label(type = nil)
|
2012-03-05 22:17:33 +00:00
|
|
|
value = if self._label_field_name
|
2012-02-01 01:01:42 +00:00
|
|
|
self.send(self._label_field_name.to_sym)
|
|
|
|
else
|
|
|
|
self.send((type || self.content_type).label_field_name.to_sym)
|
|
|
|
end
|
2012-03-05 22:17:33 +00:00
|
|
|
|
|
|
|
value.respond_to?(:to_label) ? value.to_label : value
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2012-03-14 00:16:01 +00:00
|
|
|
def translated?
|
|
|
|
if self.respond_to?(:"#{self._label_field_name}_translations")
|
|
|
|
self.send(:"#{self._label_field_name}_translations").key?(::Mongoid::Fields::I18n.locale.to_s) #rescue false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def next
|
|
|
|
next_or_previous :gt
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def previous
|
|
|
|
next_or_previous :lt
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2012-02-09 23:57:57 +00:00
|
|
|
def self.find_by_permalink(permalink)
|
|
|
|
self.where(:_slug => permalink).first
|
|
|
|
end
|
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def self.sort_entries!(ids)
|
|
|
|
list = self.any_in(:_id => ids.map { |id| BSON::ObjectId.from_string(id.to_s) }).to_a
|
|
|
|
ids.each_with_index do |id, position|
|
|
|
|
if entry = list.detect { |e| e._id.to_s == id.to_s }
|
|
|
|
entry.update_attributes :_position => position
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
def to_liquid
|
2012-01-04 01:07:53 +00:00
|
|
|
Locomotive::Liquid::Drops::ContentEntry.new(self)
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
def to_presenter(options = {})
|
|
|
|
Locomotive::ContentEntryPresenter.new(self, options)
|
2011-12-22 22:28:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(options = {})
|
|
|
|
self.to_presenter.as_json
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
protected
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def next_or_previous(matcher = :gt)
|
2012-01-04 01:07:53 +00:00
|
|
|
order_by = self.content_type.order_by_definition
|
2012-02-16 11:42:41 +00:00
|
|
|
criterion = :_position.send(matcher)
|
2012-01-02 13:54:01 +00:00
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
self.class.where(criterion => self._position).order_by([order_by]).limit(1).first
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
|
|
|
|
2011-12-17 17:18:56 +00:00
|
|
|
# Sets the slug of the instance by using the value of the highlighted field
|
|
|
|
# (if available). If a sibling content instance has the same permalink then a
|
|
|
|
# unique one will be generated
|
2011-10-30 23:02:41 +00:00
|
|
|
def set_slug
|
2012-01-04 01:07:53 +00:00
|
|
|
self._slug = self._label.dup if self._slug.blank? && self._label.present?
|
|
|
|
|
|
|
|
if self._slug.present?
|
|
|
|
self._slug.permalink!
|
|
|
|
self._slug = self.next_unique_slug if self.slug_already_taken?
|
|
|
|
end
|
2011-12-17 17:18:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return the next available unique slug as a string
|
|
|
|
def next_unique_slug
|
2012-01-04 01:07:53 +00:00
|
|
|
slug = self._slug.gsub(/-\d*$/, '')
|
|
|
|
last_slug = self.class.where(:_id.ne => self._id, :_slug => /^#{slug}-?\d*?$/i).order_by(:_slug).last._slug
|
|
|
|
next_number = last_slug.scan(/-(\d)$/).flatten.first.to_i + 1
|
|
|
|
[slug, next_number].join('-')
|
2011-12-17 17:18:56 +00:00
|
|
|
end
|
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def slug_already_taken?
|
|
|
|
self.class.where(:_id.ne => self._id, :_slug => self._slug).any?
|
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
def set_site
|
|
|
|
self.site ||= self.content_type.site
|
|
|
|
end
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def set_visibility
|
2012-02-16 11:42:41 +00:00
|
|
|
if self.respond_to?(:visible)
|
|
|
|
self.visible = true if self.visible.nil?
|
|
|
|
self._visible = self.visible
|
|
|
|
return
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
def set_label_field_name
|
|
|
|
self._label_field_name = self.content_type.label_field_name
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
def add_to_list_bottom
|
2012-01-02 13:54:01 +00:00
|
|
|
self._position = self.class.max(:_position).to_i + 1
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2012-01-04 01:07:53 +00:00
|
|
|
def send_notifications
|
2012-01-09 14:49:59 +00:00
|
|
|
return if !self.content_type.public_submission_enabled? || self.content_type.public_submission_accounts.blank?
|
2012-01-04 01:07:53 +00:00
|
|
|
|
2012-02-16 11:42:41 +00:00
|
|
|
self.site.accounts.each do |account|
|
|
|
|
next unless self.content_type.public_submission_accounts.include?(account._id)
|
2012-01-04 01:07:53 +00:00
|
|
|
|
|
|
|
Locomotive::Notifications.new_content_entry(account, self).deliver
|
|
|
|
end
|
|
|
|
end
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
end
|
2011-11-20 10:59:12 +00:00
|
|
|
end
|