Class: NoCms::Blocks::Template

Inherits:
Object
  • Object
show all
Defined in:
app/models/no_cms/blocks/template.rb

Overview

This class will manage all the information about the template, the collection of zones and allowed blocks to manage restrictions about where every block is allowed to be.

Constant Summary collapse

DEFAULT_TEMPLATE_CONFIGURATION =
{ blocks: [ ], lazy_blocks: [ ] }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Template

We receive a configuration hash like the ones defined in the configuration files



14
15
16
17
# File 'app/models/no_cms/blocks/template.rb', line 14

def initialize name, config
  @config = DEFAULT_TEMPLATE_CONFIGURATION.merge config
  @name = name
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'app/models/no_cms/blocks/template.rb', line 7

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'app/models/no_cms/blocks/template.rb', line 7

def name
  @name
end

Class Method Details

.find(template_id) ⇒ Object

We look for the template_id into the engine configuration and return a template initialized with the corresponding configuration data



53
54
55
56
# File 'app/models/no_cms/blocks/template.rb', line 53

def self.find template_id
  template_config = NoCms::Blocks.templates.stringify_keys[template_id.to_s]
  NoCms::Blocks::Template.new template_id, template_config unless template_config.nil?
end

Instance Method Details

#==(another) ⇒ Object

Two templates are the same if they have the same template name



60
61
62
# File 'app/models/no_cms/blocks/template.rb', line 60

def == another
  self.name == another.name
end

#allowed_layoutsObject

This method returns the blocks allowed globally for this template. If no block is allowed then we return an empty array



22
23
24
25
# File 'app/models/no_cms/blocks/template.rb', line 22

def allowed_layouts
  return @allowed_layouts if @allowed_layouts
  @allowed_layouts = [config[:blocks], config[:lazy_blocks]].compact.flatten.uniq
end

#appliable?(model) ⇒ Boolean

This method returns wether the template is appliable to the model parameter according to the configuration

Returns:

  • (Boolean)


71
72
73
# File 'app/models/no_cms/blocks/template.rb', line 71

def appliable? model
  config[:models].blank? || config[:models].include?(model.name)
end

#human_nameObject



64
65
66
# File 'app/models/no_cms/blocks/template.rb', line 64

def human_name
  I18n.t("no_cms.blocks.templates.#{self.name}")
end

#is_lazy_layout?(layout) ⇒ Boolean

This method checks that the layout sent as param is configured as a lazy block in the whole template

Returns:

  • (Boolean)


29
30
31
# File 'app/models/no_cms/blocks/template.rb', line 29

def is_lazy_layout? layout
  config[:lazy_blocks].map(&:to_s).include?(layout)
end

#zone(zone_id) ⇒ Object

This method returns the configuration for just one zone which name is passed as parameter



46
47
48
# File 'app/models/no_cms/blocks/template.rb', line 46

def zone zone_id
  zones.detect{|b| b.name.to_s == zone_id.to_s }
end

#zonesObject

This method returns an array of the zones contained by this template with its configuration



37
38
39
40
41
# File 'app/models/no_cms/blocks/template.rb', line 37

def zones
  @zones ||= config[:zones].map do |zone_name, zone_config|
    NoCms::Blocks::Zone.new zone_name, zone_config, self
  end
end