Class: SirTrevorRails::Block

Inherits:
OpenStruct
  • Object
show all
Defined in:
app/models/sir_trevor_rails/block.rb

Overview

Forked from (former) upstream sir-trevor-rails 0.6.2 gem: github.com/madebymany/sir-trevor-rails/blob/931b9554f5268158b4da8817477cdc82e4e2e69c/lib/sir_trevor_rails/block.rb Copyright © 2013-2014 by ITV plc - www.itv.com

Constant Summary collapse

DEFAULT_FORMAT =
:markdown

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, parent) ⇒ Block

Returns a new instance of Block.



27
28
29
30
31
32
# File 'app/models/sir_trevor_rails/block.rb', line 27

def initialize(hash, parent)
  @raw_data = hash
  @parent  = parent
  @type    = hash[:type].to_sym
  super(hash[:data])
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



34
35
36
# File 'app/models/sir_trevor_rails/block.rb', line 34

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



34
35
36
# File 'app/models/sir_trevor_rails/block.rb', line 34

def type
  @type
end

Class Method Details

.block_class(type) ⇒ Object

Infers the block class. Safe lookup that tries to identify user created block class.

Parameters:

  • type (Symbol)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/sir_trevor_rails/block.rb', line 51

def self.block_class(type)
  type_name = type.to_s.camelize
  block_name = "#{type_name}Block"
  if custom_block_types.include?(type_name)
    begin
      block_name.constantize
    rescue NameError
      block_class!(block_name)
    end
  else
    block_class!(block_name)
  end
end

.block_class!(block_name) ⇒ Object

Infers the block class. Failover from block_class. Safe lookup against the SirTevor::Blocks namespace If no block is found, create one with given name and inherit from Block class

Parameters:

  • block_name (Constant)


71
72
73
74
75
# File 'app/models/sir_trevor_rails/block.rb', line 71

def self.block_class!(block_name)
  SirTrevorRails::Blocks.const_get(block_name)
rescue NameError
  SirTrevorRails::Blocks.const_set(block_name, Class.new(Block))
end

.custom_block_typesObject

Sets a list of custom block types to speed up lookup at runtime.



22
23
24
25
# File 'app/models/sir_trevor_rails/block.rb', line 22

def self.custom_block_types
  # You can define your custom block types directly here or in your engine config.
  Spotlight::Engine.config.sir_trevor_widgets
end

.from_hash(hash, parent = nil) ⇒ Object



12
13
14
15
# File 'app/models/sir_trevor_rails/block.rb', line 12

def self.from_hash(hash, parent = nil)
  hash = hash.deep_dup.with_indifferent_access
  type_klass(hash).new(hash, parent)
end

.type_klass(hash) ⇒ Object



77
78
79
80
81
82
83
# File 'app/models/sir_trevor_rails/block.rb', line 77

def self.type_klass(hash)
  if self == Block
    block_class(hash[:type].to_sym)
  else
    self
  end
end

Instance Method Details

#as_json(*_attrs) ⇒ Object



40
41
42
43
44
45
# File 'app/models/sir_trevor_rails/block.rb', line 40

def as_json(*_attrs)
  {
    type: @type.to_s,
    data: marshal_dump
  }
end

#formatObject



17
18
19
# File 'app/models/sir_trevor_rails/block.rb', line 17

def format
  send(:[], :format).present? ? send(:[], :format).to_sym : DEFAULT_FORMAT
end

#to_partial_pathObject



36
37
38
# File 'app/models/sir_trevor_rails/block.rb', line 36

def to_partial_path
  "sir_trevor/blocks/#{self.class.name.demodulize.underscore}"
end