Class: NoCms::Blocks::Layout

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

Overview

This class will manage all the information about block layouts (e.g: How is it retrieved from configuration files? Which is the default configuration for any field?).

Constant Summary collapse

DEFAULT_FIELD_CONFIGURATION =
{ translated: true, duplicate: :dup }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Layout

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



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

def initialize config
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.find(layout_id) ⇒ Object

We look for the layout_id into the engine configuration and return a layout initialized with the corresponding configuration data



93
94
95
96
# File 'app/models/no_cms/blocks/layout.rb', line 93

def self.find layout_id
  layout_config = NoCms::Blocks.block_layouts.stringify_keys[layout_id.to_s]
  NoCms::Blocks::Layout.new layout_config unless layout_config.nil?
end

Instance Method Details

#allow_nested_blocksObject Also known as: allow_nested_blocks?



72
73
74
75
76
# File 'app/models/no_cms/blocks/layout.rb', line 72

def allow_nested_blocks
  config.has_key?(:allow_nested_blocks) ?
    config[:allow_nested_blocks] :
    false
end

#cache_enabledObject Also known as: cache_enabled?



83
84
85
86
87
# File 'app/models/no_cms/blocks/layout.rb', line 83

def cache_enabled
  config.has_key?(:cache_enabled) ?
    config[:cache_enabled] :
    NoCms::Blocks.cache_enabled
end

#field(field_name) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/models/no_cms/blocks/layout.rb', line 45

def field field_name
  field_name = field_name.to_sym
  if fields.has_key? field_name
    fields[field_name]
  else
    field_name = field_name.to_s.gsub(/\_id$/, '')
    fields[field_name] if fields.has_key? field_name
  end
end

#fieldsObject

This method parses the fields block from the layout configuration and takes into account if they are declared in a verbose mode (with a hash) or a quick one (just the field type).

It uses default values for the configuration that is not declared



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/no_cms/blocks/layout.rb', line 24

def fields
  return @fields if @fields

  @fields = {}

  ## If we have a fields config we fill the fields hash
  unless config[:fields].nil?
    config[:fields].each do | field, field_config|
      # If configuration is not a hash it means that we are only receiving the
      # field type. We turn it into a proper hash and then merge it with the
      # default configuration
      field_config = { type: field_config } unless field_config.is_a? Hash
      @fields[field] = DEFAULT_FIELD_CONFIGURATION.merge field_config
    end
  end

  @fields = @fields.symbolize_keys

  @fields
end

#nest_levelsObject



79
80
81
# File 'app/models/no_cms/blocks/layout.rb', line 79

def nest_levels
  config[:nest_levels] || []
end

#not_translated_fieldsObject

This method returns only the configuration for not translated fields



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

def not_translated_fields
  @not_translated_fields ||= fields.reject{|field, config| config[:translated] }
end

#templateObject



68
69
70
# File 'app/models/no_cms/blocks/layout.rb', line 68

def template
  config[:template]
end

#translated_fieldsObject

This method returns only the configuration for translated fields



57
58
59
# File 'app/models/no_cms/blocks/layout.rb', line 57

def translated_fields
  @translated_fields ||= fields.select{|field, config| config[:translated] }
end