Sha256: c7c79a68049f2fa07637d139d1c14b670a4d0ffe039db6cb190e1607e407e736

Contents?: true

Size: 1.71 KB

Versions: 22

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
        Configuration::ContentBlocks.types.detect { |bt| bt.id == type_id }
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/content/preset.rb
workarea-core-3.5.26 app/models/workarea/content/preset.rb
workarea-core-3.5.25 app/models/workarea/content/preset.rb
workarea-core-3.5.23 app/models/workarea/content/preset.rb
workarea-core-3.5.22 app/models/workarea/content/preset.rb
workarea-core-3.5.21 app/models/workarea/content/preset.rb
workarea-core-3.5.20 app/models/workarea/content/preset.rb
workarea-core-3.5.19 app/models/workarea/content/preset.rb
workarea-core-3.5.18 app/models/workarea/content/preset.rb
workarea-core-3.5.17 app/models/workarea/content/preset.rb
workarea-core-3.5.16 app/models/workarea/content/preset.rb
workarea-core-3.5.15 app/models/workarea/content/preset.rb
workarea-core-3.5.14 app/models/workarea/content/preset.rb
workarea-core-3.5.13 app/models/workarea/content/preset.rb
workarea-core-3.5.12 app/models/workarea/content/preset.rb
workarea-core-3.5.11 app/models/workarea/content/preset.rb
workarea-core-3.5.10 app/models/workarea/content/preset.rb
workarea-core-3.5.9 app/models/workarea/content/preset.rb
workarea-core-3.5.8 app/models/workarea/content/preset.rb
workarea-core-3.5.7 app/models/workarea/content/preset.rb