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-01 01:01:42 +00:00
|
|
|
delegate :_label, :_slug, :_position, :seo_title, :meta_keywords, :meta_description, :to => :source
|
2012-01-02 13:54:01 +00:00
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
# Returns the value of a field in the context of the current entry.
|
|
|
|
#
|
|
|
|
# @params [ CustomFields::Field ] field The field
|
|
|
|
#
|
|
|
|
# @returns [ Object ] The value of the field for the entry
|
|
|
|
#
|
|
|
|
def value_for(field)
|
|
|
|
getter = [*self.getters_for(field.name, field.type)].first.to_sym
|
|
|
|
self.source.send(getter)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the list of getters for an entry
|
|
|
|
#
|
|
|
|
# @returns [ List ] a list of method names (string)
|
|
|
|
#
|
2012-01-02 13:54:01 +00:00
|
|
|
def custom_fields_methods
|
2012-01-09 14:49:59 +00:00
|
|
|
self.source.custom_fields_recipe['rules'].map do |rule|
|
|
|
|
self.getters_for rule['name'], rule['type']
|
2012-01-02 13:54:01 +00:00
|
|
|
end.flatten
|
|
|
|
end
|
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
# Lists of all the attributes editable by a html form
|
|
|
|
#
|
|
|
|
# @returns [ List ] a list of attributes (string)
|
|
|
|
#
|
2012-01-02 13:54:01 +00:00
|
|
|
def safe_attributes
|
2012-01-09 14:49:59 +00:00
|
|
|
self.source.custom_fields_recipe['rules'].map do |rule|
|
2012-01-31 00:50:09 +00:00
|
|
|
case rule['type'].to_sym
|
|
|
|
when :date then "formatted_#{rule['name']}"
|
|
|
|
when :file then [rule['name'], "remove_#{rule['name']}"]
|
2012-02-01 01:01:42 +00:00
|
|
|
when :select, :belongs_to then ["#{rule['name']}_id", "position_in_#{rule['name']}"]
|
|
|
|
when :has_many then nil
|
2012-01-02 13:54:01 +00:00
|
|
|
else
|
|
|
|
rule['name']
|
|
|
|
end
|
2012-02-01 01:01:42 +00:00
|
|
|
end.compact.flatten + %w(_slug seo_title meta_keywords meta_description _destroy)
|
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 _file_fields
|
|
|
|
self.source.custom_fields_recipe['rules'].find_all { |rule| rule['type'] == 'file' }.map { |rule| rule['name'] }
|
|
|
|
end
|
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
def _has_many_fields
|
|
|
|
self.source.custom_fields_recipe['rules'].find_all { |rule| rule['type'] == 'has_many' }.map { |rule| [rule['name'], rule['inverse_of']] }
|
|
|
|
end
|
|
|
|
|
2012-01-02 13:54:01 +00:00
|
|
|
def included_methods
|
2012-02-01 01:01:42 +00:00
|
|
|
default_list = %w(_label _slug _position content_type_slug _file_fields _has_many_fields safe_attributes)
|
2012-01-09 14:49:59 +00:00
|
|
|
default_list << 'errors' if !!self.options[:include_errors]
|
|
|
|
super + self.custom_fields_methods + default_list
|
2012-01-02 13:54:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(meth, *arguments, &block)
|
|
|
|
if self.custom_fields_methods.include?(meth.to_s)
|
|
|
|
self.source.send(meth) rescue nil
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2011-12-22 22:28:12 +00:00
|
|
|
|
2012-01-09 14:49:59 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Gets the names of the getter methods for a field.
|
|
|
|
# The names depend on the field type.
|
|
|
|
#
|
|
|
|
# @params [ String ] name Name of the field
|
|
|
|
# @params [ String ] type Type of the field
|
|
|
|
#
|
|
|
|
# @returns [ Object ] A string or an array of names
|
|
|
|
def getters_for(name, type)
|
2012-01-31 00:50:09 +00:00
|
|
|
case type.to_sym
|
2012-02-01 01:01:42 +00:00
|
|
|
when :select then [name, "#{name}_id"]
|
|
|
|
when :date then "formatted_#{name}"
|
|
|
|
when :file then "#{name}_url"
|
|
|
|
when :belongs_to then "#{name}_id"
|
|
|
|
# when :has_many then nil
|
2012-01-09 14:49:59 +00:00
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-22 22:28:12 +00:00
|
|
|
end
|
|
|
|
end
|