2011-12-22 22:28:12 +00:00
|
|
|
module Locomotive
|
2011-12-22 23:45:32 +00:00
|
|
|
class ContentEntryPresenter < BasePresenter
|
2011-12-22 22:28:12 +00:00
|
|
|
|
2012-02-04 01:10:55 +00:00
|
|
|
delegate :_label, :_slug, :_position, :seo_title, :meta_keywords, :meta_description, :file_custom_fields, :has_many_custom_fields, :many_to_many_custom_fields, :to => :source
|
2012-01-02 13:54:01 +00:00
|
|
|
|
2012-02-04 01:10:55 +00:00
|
|
|
# Lists of all the attributes editable thru the html form for instance
|
2012-01-09 14:49:59 +00:00
|
|
|
#
|
|
|
|
# @returns [ List ] a list of attributes (string)
|
|
|
|
#
|
2012-01-02 13:54:01 +00:00
|
|
|
def safe_attributes
|
2012-02-04 01:10:55 +00:00
|
|
|
self.source.custom_fields_safe_attributes + %w(_slug seo_title meta_keywords meta_description _destroy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_custom_fields_methods
|
|
|
|
self.source.custom_fields_methods do |rule|
|
|
|
|
if self.source.is_a_custom_field_many_relationship?(rule['name'])
|
|
|
|
# avoid circular dependencies, it should accept only one deep level
|
|
|
|
self.depth == 0
|
2012-01-02 13:54:01 +00:00
|
|
|
else
|
2012-02-04 01:10:55 +00:00
|
|
|
true
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
2012-02-04 01:10:55 +00:00
|
|
|
end
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
def errors
|
|
|
|
self.source.errors.to_hash.stringify_keys
|
|
|
|
end
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def content_type_slug
|
|
|
|
self.source.content_type.slug
|
|
|
|
end
|
|
|
|
|
|
|
|
def included_methods
|
2012-02-04 01:10:55 +00:00
|
|
|
default_list = %w(_label _slug _position content_type_slug file_custom_fields has_many_custom_fields many_to_many_custom_fields safe_attributes)
|
2012-01-09 14:49:59 +00:00
|
|
|
default_list << 'errors' if !!self.options[:include_errors]
|
2012-02-04 01:10:55 +00:00
|
|
|
super + self.filtered_custom_fields_methods + default_list
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
|
|
|
|
2012-03-07 01:56:56 +00:00
|
|
|
def as_json(methods = nil)
|
|
|
|
methods ||= self.included_methods
|
|
|
|
{}.tap do |hash|
|
|
|
|
methods.each do |meth|
|
|
|
|
hash[meth]= (if self.source.custom_fields_methods.include?(meth.to_s)
|
|
|
|
if self.source.is_a_custom_field_many_relationship?(meth.to_s)
|
|
|
|
# go deeper
|
2012-03-28 15:33:20 +00:00
|
|
|
self.source.send(meth).ordered.map { |entry| entry.to_presenter(:depth => self.depth + 1) }
|
2012-03-07 01:56:56 +00:00
|
|
|
else
|
|
|
|
self.source.send(meth) rescue nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.send(meth.to_sym) rescue nil
|
|
|
|
end)
|
2012-02-04 01:10:55 +00:00
|
|
|
end
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
|
|
|
end
|
2011-12-22 22:28:12 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|