2010-06-01 00:06:46 +00:00
|
|
|
class ContentInstance
|
|
|
|
|
2010-05-24 00:18:23 +00:00
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-10 13:30:22 +00:00
|
|
|
## extensions ##
|
|
|
|
include CustomFields::ProxyClassEnabler
|
2011-04-29 20:10:13 +00:00
|
|
|
include Extensions::Shared::Seo
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
## fields (dynamic fields) ##
|
2010-07-16 20:36:07 +00:00
|
|
|
field :_slug
|
2010-05-26 00:41:10 +00:00
|
|
|
field :_position_in_list, :type => Integer, :default => 0
|
2010-07-19 00:09:10 +00:00
|
|
|
field :_visible, :type => Boolean, :default => true
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
## validations ##
|
|
|
|
validate :require_highlighted_field
|
2011-12-17 17:11:39 +00:00
|
|
|
validates :_slug, :presence => true, :uniqueness => { :scope => :content_type_id }
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
## associations ##
|
|
|
|
embedded_in :content_type, :inverse_of => :contents
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
## callbacks ##
|
2011-06-21 20:03:24 +00:00
|
|
|
before_validation :set_slug
|
2010-07-19 00:09:10 +00:00
|
|
|
before_save :set_visibility
|
2010-06-16 14:43:29 +00:00
|
|
|
before_create :add_to_list_bottom
|
2011-01-18 14:24:42 +00:00
|
|
|
after_create :send_notifications
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-26 00:41:10 +00:00
|
|
|
## named scopes ##
|
2012-01-19 01:30:12 +00:00
|
|
|
scope :latest_updated, :order_by => :updated_at.desc, :limit => Locomotive.config.latest_items_nb
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
## methods ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-04-29 20:10:13 +00:00
|
|
|
delegate :site, :to => :content_type
|
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
alias :visible? :_visible?
|
2010-10-26 13:06:57 +00:00
|
|
|
alias :_permalink :_slug
|
2011-07-28 09:53:36 +00:00
|
|
|
alias :_permalink= :_slug=
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-10-10 23:16:43 +00:00
|
|
|
def site_id # needed by the uploader of custom fields
|
|
|
|
self.content_type.site_id
|
|
|
|
end
|
|
|
|
|
2011-01-18 00:19:18 +00:00
|
|
|
def highlighted_field_value
|
2011-05-19 15:44:04 +00:00
|
|
|
self.send(self.content_type.highlighted_field_name)
|
2011-01-18 00:19:18 +00:00
|
|
|
end
|
|
|
|
|
2011-05-19 15:44:04 +00:00
|
|
|
alias :_label :highlighted_field_value
|
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
def visible?
|
|
|
|
self._visible || self._visible.nil?
|
|
|
|
end
|
2011-09-16 03:35:40 +00:00
|
|
|
|
|
|
|
def next
|
2011-09-16 03:55:30 +00:00
|
|
|
content_type.contents.where(:_position_in_list => _position_in_list + 1).first()
|
2011-09-16 03:35:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous
|
2011-09-16 03:55:30 +00:00
|
|
|
content_type.contents.where(:_position_in_list => _position_in_list - 1).first()
|
2011-09-16 03:35:40 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-12-16 23:42:38 +00:00
|
|
|
def errors_to_hash
|
|
|
|
Hash.new.replace(self.errors)
|
|
|
|
end
|
|
|
|
|
2011-05-20 23:02:03 +00:00
|
|
|
def reload_parent!
|
|
|
|
self.class.reload_parent!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reload_parent!
|
|
|
|
self._parent = self._parent.reload
|
|
|
|
end
|
|
|
|
|
2010-06-10 13:30:22 +00:00
|
|
|
def to_liquid
|
|
|
|
Locomotive::Liquid::Drops::Content.new(self)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
protected
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-12-17 17:11:39 +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
|
2010-07-16 20:36:07 +00:00
|
|
|
def set_slug
|
2011-12-17 17:11:39 +00:00
|
|
|
self._slug = highlighted_field_value.dup if _slug.blank? && highlighted_field_value.present?
|
|
|
|
|
|
|
|
if _slug.present?
|
|
|
|
self._slug.permalink!
|
|
|
|
self._slug = next_unique_slug if slug_already_taken?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the next available unique slug as a string
|
|
|
|
def next_unique_slug
|
|
|
|
slug = _slug.gsub(/-\d*$/, '')
|
|
|
|
last_slug = _parent.contents.where(:_id.ne => _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('-')
|
|
|
|
end
|
|
|
|
|
|
|
|
def slug_already_taken?
|
|
|
|
_parent.contents.where(:_id.ne => _id, :_slug => _slug).any?
|
2010-07-16 20:36:07 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
def set_visibility
|
|
|
|
field = self.content_type.content_custom_fields.detect { |f| %w{visible active}.include?(f._alias) }
|
|
|
|
self._visible = self.send(field._name) rescue true
|
2012-01-12 15:18:55 +00:00
|
|
|
true
|
2010-07-19 00:09:10 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
def add_to_list_bottom
|
|
|
|
self._position_in_list = self.content_type.contents.size
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-25 00:32:12 +00:00
|
|
|
def require_highlighted_field
|
2010-07-16 20:36:07 +00:00
|
|
|
_alias = self.highlighted_field_alias
|
2010-05-25 00:32:12 +00:00
|
|
|
if self.send(_alias).blank?
|
|
|
|
self.errors.add(_alias, :blank)
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
def highlighted_field_alias
|
|
|
|
self.content_type.highlighted_field._alias.to_sym
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-01-18 14:24:42 +00:00
|
|
|
def send_notifications
|
2011-01-18 15:06:36 +00:00
|
|
|
return unless self.content_type.api_enabled? && !self.content_type.api_accounts.blank?
|
2011-01-18 14:24:42 +00:00
|
|
|
|
|
|
|
accounts = self.content_type.site.accounts.to_a
|
|
|
|
|
|
|
|
self.content_type.api_accounts.each do |account_id|
|
|
|
|
next if account_id.blank?
|
|
|
|
|
|
|
|
account = accounts.detect { |a| a.id.to_s == account_id.to_s }
|
|
|
|
|
|
|
|
Admin::Notifications.new_content_instance(account, self).deliver
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|