Sha256: 3813380638c0daf4cce724a3732e6b87b01380a2e61fe63a3a90440254866d4f

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module Locomotive
  class ContentEntryPresenter < BasePresenter

    delegate :_label, :_slug, :_position, :seo_title, :meta_keywords, :meta_description, :file_custom_fields, :has_many_custom_fields, :many_to_many_custom_fields, :to => :source

    # Lists of all the attributes editable thru the html form for instance
    #
    # @returns [ List ] a list of attributes (string)
    #
    def safe_attributes
      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
        else
          true
        end
      end
    end

    def errors
      self.source.errors.to_hash.stringify_keys
    end

    def content_type_slug
      self.source.content_type.slug
    end

    def included_methods
      default_list = %w(_label _slug _position content_type_slug file_custom_fields has_many_custom_fields many_to_many_custom_fields safe_attributes)
      default_list << 'errors' if !!self.options[:include_errors]
      super + self.filtered_custom_fields_methods + default_list
    end

    def method_missing(meth, *arguments, &block)
      if self.source.custom_fields_methods.include?(meth.to_s)
        if self.source.is_a_custom_field_many_relationship?(meth.to_s)
          # go deeper
          self.source.send(meth).map { |entry| entry.to_presenter(:depth => self.depth + 1) }
        else
          self.source.send(meth) rescue nil
        end
      else
        super
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locomotive_cms-2.0.0.rc1 app/presenters/locomotive/content_entry_presenter.rb