Sha256: 1b581c8a7e89a846a9fbd4d3ccf9ce551c2694a04aa855bf3e3152c5959e8041

Contents?: true

Size: 1.7 KB

Versions: 34

Compression:

Stored size: 1.7 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 {Content.define_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

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/models/workarea/content/preset.rb
workarea-core-3.4.44 app/models/workarea/content/preset.rb
workarea-core-3.4.43 app/models/workarea/content/preset.rb
workarea-core-3.4.42 app/models/workarea/content/preset.rb
workarea-core-3.4.41 app/models/workarea/content/preset.rb
workarea-core-3.4.40 app/models/workarea/content/preset.rb
workarea-core-3.4.39 app/models/workarea/content/preset.rb
workarea-core-3.4.38 app/models/workarea/content/preset.rb
workarea-core-3.4.37 app/models/workarea/content/preset.rb
workarea-core-3.4.36 app/models/workarea/content/preset.rb
workarea-core-3.4.35 app/models/workarea/content/preset.rb
workarea-core-3.4.34 app/models/workarea/content/preset.rb
workarea-core-3.4.33 app/models/workarea/content/preset.rb
workarea-core-3.4.32 app/models/workarea/content/preset.rb
workarea-core-3.4.31 app/models/workarea/content/preset.rb
workarea-core-3.4.30 app/models/workarea/content/preset.rb
workarea-core-3.4.29 app/models/workarea/content/preset.rb
workarea-core-3.4.28 app/models/workarea/content/preset.rb
workarea-core-3.4.27 app/models/workarea/content/preset.rb
workarea-core-3.4.26 app/models/workarea/content/preset.rb