Class: SirTrevorRails::Block
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- SirTrevorRails::Block
- 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
Direct Known Subclasses
SirTrevorRails::Blocks::BrowseBlock, SirTrevorRails::Blocks::BrowseGroupCategoriesBlock, SirTrevorRails::Blocks::FeaturedPagesBlock, SirTrevorRails::Blocks::OembedBlock, SirTrevorRails::Blocks::SearchResultsBlock, SirTrevorRails::Blocks::SolrDocumentsBlock, SirTrevorRails::Blocks::UploadedItemsBlock
Constant Summary collapse
- DEFAULT_FORMAT =
:markdown
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .alt_text? ⇒ Boolean
-
.block_class(type) ⇒ Object
Infers the block class.
-
.block_class!(block_name) ⇒ Object
Infers the block class.
- .custom_block_type_alt_text_settings ⇒ Object
-
.custom_block_types ⇒ Object
Sets a list of custom block types to speed up lookup at runtime.
- .from_hash(hash, parent = nil) ⇒ Object
- .type_klass(hash) ⇒ Object
Instance Method Summary collapse
- #alt_text? ⇒ Boolean
- #as_json(*_attrs) ⇒ Object
- #format ⇒ Object
-
#initialize(hash, parent) ⇒ Block
constructor
A new instance of Block.
- #to_partial_path ⇒ Object
Constructor Details
#initialize(hash, parent) ⇒ Block
Returns a new instance of Block.
39 40 41 42 43 44 |
# File 'app/models/sir_trevor_rails/block.rb', line 39 def initialize(hash, parent) @raw_data = hash @parent = parent @type = hash[:type].to_sym super(hash[:data]) end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
46 47 48 |
# File 'app/models/sir_trevor_rails/block.rb', line 46 def parent @parent end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
46 47 48 |
# File 'app/models/sir_trevor_rails/block.rb', line 46 def type @type end |
Class Method Details
.alt_text? ⇒ Boolean
25 26 27 |
# File 'app/models/sir_trevor_rails/block.rb', line 25 def self.alt_text? false end |
.block_class(type) ⇒ Object
Infers the block class. Safe lookup that tries to identify user created block class.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/sir_trevor_rails/block.rb', line 63 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
83 84 85 86 87 |
# File 'app/models/sir_trevor_rails/block.rb', line 83 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_type_alt_text_settings ⇒ Object
35 36 37 |
# File 'app/models/sir_trevor_rails/block.rb', line 35 def self.custom_block_type_alt_text_settings custom_block_types.index_with { |block_type| SirTrevorRails::Block.block_class(block_type).alt_text? } end |
.custom_block_types ⇒ Object
Sets a list of custom block types to speed up lookup at runtime.
30 31 32 33 |
# File 'app/models/sir_trevor_rails/block.rb', line 30 def self.custom_block_types # You can define your custom block types directly here or in your engine config. Spotlight::Engine.config. 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
89 90 91 92 93 94 95 |
# File 'app/models/sir_trevor_rails/block.rb', line 89 def self.type_klass(hash) if self == Block block_class(hash[:type].to_sym) else self end end |
Instance Method Details
#alt_text? ⇒ Boolean
21 22 23 |
# File 'app/models/sir_trevor_rails/block.rb', line 21 def alt_text? self.class.alt_text? end |
#as_json(*_attrs) ⇒ Object
52 53 54 55 56 57 |
# File 'app/models/sir_trevor_rails/block.rb', line 52 def as_json(*_attrs) { type: @type.to_s, data: marshal_dump } end |
#format ⇒ Object
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_path ⇒ Object
48 49 50 |
# File 'app/models/sir_trevor_rails/block.rb', line 48 def to_partial_path "sir_trevor/blocks/#{self.class.name.demodulize.underscore}" end |