Sha256: c6bc3148fc1700cf9ea74a9cafd737c3c620ec3eed506dfc57e34d49d4b46e5e

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

module Workarea
  class Content
    class Preset
      include ApplicationDocument

      # Create and persist a preset from an existing block.
      #
      # @param [Hash]
      # @param [Workarea::Content::Block]
      #
      # @return [Workarea::Content::Preset]
      #
      def self.create_from_block(attributes, block)
        instance = new(attributes)
        instance.apply_block(block)
        instance.save
      end

      # @!attribute type_id
      #   @return [Symbol] the content block type id
      #
      field :type_id, type: Symbol

      # @!attribute name
      #   @return [String]
      #
      field :name, type: String

      # @!attribute data
      #   @return [Hash] the pieces of data passed to partials to render
      #
      field :data, type: Hash, default: {}, localize: true

      validates :name, presence: true

      # Populate a preset from an existing {Workarea::Content::Block}
      #
      # @param [Workarea::Content::Block] block the block to copy
      #
      # @return [Boolean]
      #
      def apply_block(block)
        self.attributes = block.as_json.slice('data', 'type_id')
        self.name ||= block.type.name
      end

      # Return attributes needed to create a {Workarea::Content::Block}
      # from a preset.
      #
      # @return [Hash]
      #
      def block_attributes
        as_json.slice('data', 'type_id')
      end

      # The {Workarea::Content::BlockType} that this block is. See documentation
      # for {Workarea.define_content_block_types} for info how to define block types.
      #
      # @return [Workarea::Content::BlockType]
      #
      def type
        Workarea.config.content_block_types.detect { |bt| bt.id == type_id }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
workarea-core-3.5.4 app/models/workarea/content/preset.rb
workarea-core-3.5.3 app/models/workarea/content/preset.rb
workarea-core-3.5.2 app/models/workarea/content/preset.rb
workarea-core-3.5.1 app/models/workarea/content/preset.rb
workarea-core-3.5.0 app/models/workarea/content/preset.rb
workarea-core-3.5.0.beta.1 app/models/workarea/content/preset.rb