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: {
    fallback_on_blank: NoCms::Blocks.i18n_fallback_on_blank
  },
  duplicate: :dup,
  multiple: false
}
DEFAULT_BLOCK_CONFIGURATION =
{
  skeleton_template: 'default',
  css_templates: ''
}

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



24
25
26
# File 'app/models/no_cms/blocks/layout.rb', line 24

def initialize config
  @config = DEFAULT_BLOCK_CONFIGURATION.merge 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



114
115
116
117
# File 'app/models/no_cms/blocks/layout.rb', line 114

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?



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

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

#cache_enabledObject Also known as: cache_enabled?



104
105
106
107
108
# File 'app/models/no_cms/blocks/layout.rb', line 104

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

#css_templatesObject



89
90
91
# File 'app/models/no_cms/blocks/layout.rb', line 89

def css_templates
  config[:css_templates]
end

#field(field_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/no_cms/blocks/layout.rb', line 55

def field field_name
  field_name = field_name.to_sym
  if fields.has_key? field_name
    fields[field_name]
  elsif field_name.to_s.ends_with? "_id"
    field_name = field_name.to_s.gsub(/\_id$/, '').to_sym
    fields[field_name] if fields.has_key? field_name
  elsif field_name.to_s.ends_with? "_ids"
    field_name = field_name.to_s.gsub(/\_ids$/, '').pluralize.to_sym
    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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/no_cms/blocks/layout.rb', line 34

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



100
101
102
# File 'app/models/no_cms/blocks/layout.rb', line 100

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

#not_translated_fieldsObject

This method returns only the configuration for not translated fields



77
78
79
# File 'app/models/no_cms/blocks/layout.rb', line 77

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

#skeleton_templateObject



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

def skeleton_template
  config[:skeleton_template]
end

#templateObject



81
82
83
# File 'app/models/no_cms/blocks/layout.rb', line 81

def template
  config[:template]
end

#translated_fieldsObject

This method returns only the configuration for translated fields



70
71
72
# File 'app/models/no_cms/blocks/layout.rb', line 70

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