Sha256: 7f847dc2322ec852c86ccc702285713294f4b37342f739d4c68f00f137c031b5

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module Workarea
  class Content
    class BlockDraft
      include ApplicationDocument

      field :content_id, type: String
      Block.fields.except('_id').each do |name, field_instance|
        field name, field_instance.options.except(:klass)
      end

      # Added as a field to ensure copying this value to any blocks created
      # from a draft.
      field :activate_with, type: String

      index(
        { created_at: 1 },
        { expire_after_seconds: 1.hour.seconds.to_i }
      )

      before_save :typecast_data!

      def content
        Content.find(content_id)
      end

      def block_type
        return unless type_id.present?
        Workarea.config.content_block_types.detect { |bt| bt.id == type_id.to_sym }
      end

      def typecast_data!
        return unless type_id.present? && data.present?
        self.data = block_type.try(:typecast!, data)
        self
      end

      def to_block
        result = Content::Block.instantiate(as_document.except('content_id'))
        result.new_record = true
        result
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workarea-core-3.5.4 app/models/workarea/content/block_draft.rb
workarea-core-3.5.3 app/models/workarea/content/block_draft.rb
workarea-core-3.5.2 app/models/workarea/content/block_draft.rb
workarea-core-3.5.1 app/models/workarea/content/block_draft.rb