Class: Workarea::Content::BlockType
- Inherits:
-
Object
- Object
- Workarea::Content::BlockType
- Includes:
- AssetLookup
- Defined in:
- app/models/workarea/content/block_type.rb
Instance Attribute Summary collapse
-
#config ⇒ String
readonly
Miscellaneous configuration specified in the DSL.
-
#fieldsets ⇒ Array<Workarea::Content::Fieldset>
readonly
List of fieldsets.
-
#name ⇒ String
readonly
Admin-facing name of the block type.
Class Method Summary collapse
-
.find(id) ⇒ Workarea::Content::BlockType?
Find a BlockType by its id (which should be a symbol).
Instance Method Summary collapse
-
#defaults ⇒ Hash
The default values for this block type’s fields in a hash of the form :field_slug => ‘field default value’.
-
#description(value = nil) ⇒ String
A short description to describe the block type to the admin user when selecting a block type.
-
#field(name, type, options = {}) ⇒ Object
Define a Field on this block type.
-
#fields ⇒ Array<Workarea::Content::Field>
All fields that belong to this block type.
-
#fieldset(name, replaces: nil) { ... } ⇒ Object
Define a fieldset on the block type.
-
#icon(value = nil) ⇒ String
Get a path to an icon to show to represent the block type to the admin user when selecting block types.
-
#id ⇒ Symbol
(also: #slug)
The unique identifier for this block type.
-
#initialize(name) ⇒ BlockType
constructor
A new instance of BlockType.
- #method_missing(name, *args, &block) ⇒ Object
-
#remove_fieldset(name) ⇒ Object
Remove a previously-defined fieldset on the block type.
-
#series(size = @series) { ... } ⇒ Object
A series allows multiple instances of the same fields in a group.
-
#tags(value = nil) ⇒ Array<String>
Tags are used to filter block types for admin users when selecting which type they’d like for the new block.
-
#typecast!(data) ⇒ Hash
Passes data through the typecasting system provided by Field.
-
#view_model(value = nil) ⇒ String
The string representing the view model class to be used to render this content block type.
Methods included from AssetLookup
Constructor Details
#initialize(name) ⇒ BlockType
Returns a new instance of BlockType.
25 26 27 28 29 |
# File 'app/models/workarea/content/block_type.rb', line 25 def initialize(name) @name = name @fieldsets = [] @config = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
180 181 182 |
# File 'app/models/workarea/content/block_type.rb', line 180 def method_missing(name, *args, &block) @config[name] = args.first end |
Instance Attribute Details
#config ⇒ String (readonly)
Returns miscellaneous configuration specified in the DSL.
13 14 15 |
# File 'app/models/workarea/content/block_type.rb', line 13 def config @config end |
#fieldsets ⇒ Array<Workarea::Content::Fieldset> (readonly)
Returns list of fieldsets.
10 11 12 |
# File 'app/models/workarea/content/block_type.rb', line 10 def fieldsets @fieldsets end |
#name ⇒ String (readonly)
Returns admin-facing name of the block type.
7 8 9 |
# File 'app/models/workarea/content/block_type.rb', line 7 def name @name end |
Class Method Details
.find(id) ⇒ Workarea::Content::BlockType?
Find a Workarea::Content::BlockType by its id (which should be a symbol).
21 22 23 |
# File 'app/models/workarea/content/block_type.rb', line 21 def self.find(id) Workarea.config.content_block_types.detect { |bt| bt.id == id } end |
Instance Method Details
#defaults ⇒ Hash
The default values for this block type’s fields in a hash of the form
:field_slug => 'field default value'
157 158 159 160 161 162 |
# File 'app/models/workarea/content/block_type.rb', line 157 def defaults fields.reduce({}) do |memo, field| memo[field.slug] = field.default memo end end |
#description(value = nil) ⇒ String
A short description to describe the block type to the admin user when selecting a block type.
59 60 61 |
# File 'app/models/workarea/content/block_type.rb', line 59 def description(value = nil) @description = value.presence || @description.presence || name end |
#field(name, type, options = {}) ⇒ Object
Define a Field on this block type. Given that this is being declared without a Fieldset, this will be added to a default Fieldset called ‘Settings’. See more info on the block type DSL at Workarea.define_content_block_types
139 140 141 142 |
# File 'app/models/workarea/content/block_type.rb', line 139 def field(name, type, = {}) settings = find_or_initialize_fieldset('Settings') settings.field(name, type, ) end |
#fields ⇒ Array<Workarea::Content::Field>
All fields that belong to this block type.
148 149 150 |
# File 'app/models/workarea/content/block_type.rb', line 148 def fields fieldsets.map(&:fields).reduce(&:+) || [] end |
#fieldset(name, replaces: nil) { ... } ⇒ Object
Define a fieldset on the block type. The block will be evaluated in the Fieldset to provide the DSL. See more info on the block type DSL at Workarea.define_content_block_types.
91 92 93 94 95 |
# File 'app/models/workarea/content/block_type.rb', line 91 def fieldset(name, replaces: nil, &block) remove_fieldset(replaces) if replaces.present? fieldset = find_or_initialize_fieldset(name) fieldset.instance_eval(&block) end |
#icon(value = nil) ⇒ String
Get a path to an icon to show to represent the block type to the admin user when selecting block types. Based on the name by default, can be specified in the content block DSL.
48 49 50 51 52 |
# File 'app/models/workarea/content/block_type.rb', line 48 def icon(value = nil) @icon = value.presence || @icon.presence || "workarea/admin/content_block_types/#{name.systemize}.svg" end |
#id ⇒ Symbol Also known as: slug
The unique identifier for this block type. Used to determine which partial to render for this block type and which view model to try to use. It is based on the block type’s name.
37 38 39 |
# File 'app/models/workarea/content/block_type.rb', line 37 def id name.to_s.systemize.to_sym end |
#remove_fieldset(name) ⇒ Object
Remove a previously-defined fieldset on the block type. Used by the replaces: option in #fieldset to remove a fieldset prior to creating a new one with the new name in its place.
103 104 105 |
# File 'app/models/workarea/content/block_type.rb', line 103 def remove_fieldset(name) @fieldsets.reject! { |fieldset| fieldset.name == name } end |
#series(size = @series) { ... } ⇒ Object
A series allows multiple instances of the same fields in a group. This can be useful when you want to reuse the same fields multiple times in the content block, like image groups or multiple-column blocks.
Each member of the series corresponds to a fieldset and a view model that will be instanciated for that instance.
Each block type can only have one series.
119 120 121 122 123 124 125 126 127 128 |
# File 'app/models/workarea/content/block_type.rb', line 119 def series(size = @series, &block) @series = size 1.upto(@series.to_i).map do |i| fieldset = find_or_initialize_fieldset(i.ordinalize) fieldset.field_suffix = " #{i}" fieldset.instance_eval(&block) if block_given? fieldset end end |
#tags(value = nil) ⇒ Array<String>
Tags are used to filter block types for admin users when selecting which type they’d like for the new block.
68 69 70 |
# File 'app/models/workarea/content/block_type.rb', line 68 def (value = nil) @tags = value.presence || @tags.presence || [] end |
#typecast!(data) ⇒ Hash
172 173 174 175 176 177 178 |
# File 'app/models/workarea/content/block_type.rb', line 172 def typecast!(data) data.deep_dup.tap do |hash| fields.each do |field| hash[field.slug.to_s] = field.typecast(hash[field.slug.to_s]) end end end |
#view_model(value = nil) ⇒ String
The string representing the view model class to be used to render this content block type. This value will get constantized when rendering to get the class constant.
78 79 80 81 82 |
# File 'app/models/workarea/content/block_type.rb', line 78 def view_model(value = nil) @view_model = value.presence || @view_model.presence || "Workarea::Storefront::ContentBlocks::#{slug.to_s.camelize}ViewModel" end |