Class: Workarea::Content::BlockType

Inherits:
Object
  • Object
show all
Includes:
AssetLookup
Defined in:
app/models/workarea/content/block_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AssetLookup

#find_asset_id_by_file_name

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

#configString (readonly)

Returns miscellaneous configuration specified in the DSL.

Returns:

  • (String)

    miscellaneous configuration specified in the DSL



13
14
15
# File 'app/models/workarea/content/block_type.rb', line 13

def config
  @config
end

#fieldsetsArray<Workarea::Content::Fieldset> (readonly)

Returns list of fieldsets.

Returns:



10
11
12
# File 'app/models/workarea/content/block_type.rb', line 10

def fieldsets
  @fieldsets
end

#nameString (readonly)

Returns admin-facing name of the block type.

Returns:

  • (String)

    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).

Parameters:

  • (Symbol)

Returns:



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

#defaultsHash

The default values for this block type’s fields in a hash of the form

:field_slug => 'field default value'

Returns:

  • (Hash)


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.

Returns:



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

Parameters:

  • name (String)
  • type (Symbol)

    a slug version of a Field subclass

  • options (Hash) (defaults to: {})


139
140
141
142
# File 'app/models/workarea/content/block_type.rb', line 139

def field(name, type, options = {})
  settings = find_or_initialize_fieldset('Settings')
  settings.field(name, type, options)
end

#fieldsArray<Workarea::Content::Field>

All fields that belong to this block type.

Returns:



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.

Parameters:

Yields:



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.

Returns:



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

#idSymbol 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.

Returns:

  • (Symbol)


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.

Parameters:



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.

Parameters:

  • size (Integer) (defaults to: @series)

Yields:



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.

Returns:



68
69
70
# File 'app/models/workarea/content/block_type.rb', line 68

def tags(value = nil)
  @tags = value.presence || @tags.presence || []
end

#typecast!(data) ⇒ Hash

Passes data through the typecasting system provided by Field. Values on the hash get converted by the correlating Field.

Parameters:

  • data (Hash)

    hash of data to be converted

Returns:

  • (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.

Returns:



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