Sha256: 53ff67cb95a23446e66bbd4337a16ceb35b712e5799c354b9104ff9ccb57d76f

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module CustomFields
  #:nodoc:
  module Model
    ##
    # Model that represents a single custom field value.
    #
    # @since  0.1
    #
    class CustomFieldValue < Sequel::Model
      many_to_one :custom_field ,
        :class => 'CustomFields::Model::CustomField',
        :eager => [:custom_field_type]

      many_to_one :section_entry,
        :class => 'Sections::Model::SectionEntry'

      many_to_one :revision,
        :class => 'Sections::Model::Revision'

      ##
      # Sets the value and serializes it based on the field type.
      #
      # @since  0.2.8
      # @param  [Mixed] val The value to store.
      #
      def value=(val)
        val  = Zen::Security.sanitize(val)
        type = custom_field.custom_field_type

        if !type.nil? and type.serialize == true
          val = [Marshal.dump(val)].pack('m')
        end

        super(val)
      end

      ##
      # Retrieves the value and optionally unserializes it.
      #
      # @since  0.2.8
      # @return [Mixed]
      #
      def value
        val  = super
        type = custom_field.custom_field_type

        if !type.nil? and type.serialize == true
          val = Marshal.load(val.unpack('m')[0]) rescue Marshal.load(val)
        end

        return val
      end

      ##
      # Retrieves the value of the custom field and converts it to the output
      # based on the markup engine specified in the custom field.
      #
      # @since  0.3
      # @return [String]
      #
      def html
        return ::Zen::Markup.convert(custom_field.format, value)
      end
    end # CustomFieldValue
  end # Model
end # CustomFields

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zen-0.4.3 lib/zen/package/custom_fields/lib/custom_fields/model/custom_field_value.rb