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
|
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
|
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 ##
|
2010-07-16 20:36:07 +00:00
|
|
|
before_save :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
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-26 00:41:10 +00:00
|
|
|
## named scopes ##
|
2010-08-01 06:49:04 +00:00
|
|
|
scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.lastest_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
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
alias :visible? :_visible?
|
2010-10-26 13:06:57 +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
|
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
def visible?
|
|
|
|
self._visible || self._visible.nil?
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def aliased_attributes # TODO: move it to the custom_fields gem
|
|
|
|
hash = { :created_at => self.created_at, :updated_at => self.updated_at }
|
|
|
|
|
|
|
|
self.custom_fields.each do |field|
|
|
|
|
case field.kind
|
|
|
|
when 'file' then hash[field._alias] = self.send(field._name.to_sym).url
|
|
|
|
else
|
|
|
|
hash[field._alias] = self.send(field._name.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
hash
|
|
|
|
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
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
def set_slug
|
|
|
|
_alias = self.highlighted_field_alias
|
|
|
|
self._slug = self.send(_alias).parameterize('_')
|
|
|
|
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
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
def add_to_list_bottom
|
|
|
|
Rails.logger.debug "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_value
|
|
|
|
self.send(self.content_type.highlighted_field._name)
|
|
|
|
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
|
|
|
|
|
|
|
end
|