Class: Layout

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Layout) initialize(id, config)

A new instance of Layout



5
6
7
8
9
10
11
# File 'app/models/layout.rb', line 5

def initialize(id, config)
  self.id                     = id
  self.path                   = config.delete("path")
  self.name                   = config.delete("name")
  self.custom_representations = config.delete('custom_representations')
  self.config                 = config
end

Instance Attribute Details

- (Object) config

Returns the value of attribute config



3
4
5
# File 'app/models/layout.rb', line 3

def config
  @config
end

- (Object) custom_representations

Returns the value of attribute custom_representations



3
4
5
# File 'app/models/layout.rb', line 3

def custom_representations
  @custom_representations
end

- (Object) id

Returns the value of attribute id



3
4
5
# File 'app/models/layout.rb', line 3

def id
  @id
end

- (Object) name

Returns the value of attribute name



3
4
5
# File 'app/models/layout.rb', line 3

def name
  @name
end

- (Object) path

Returns the value of attribute path



3
4
5
# File 'app/models/layout.rb', line 3

def path
  @path
end

Class Method Details

+ (Object) all



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/layout.rb', line 64

def self.all
  if @all_layouts.blank?
    configs = {}
    Dir[File.join(Rails.root, 'app', 'layouts', '*'), File.join(DevCMS.core_root, 'app', 'layouts', '*')].each do |layout_path|
      configs[layout_path.split('/').last] = YAML.load_file( File.join(layout_path, 'config.yml')).merge({'path' => layout_path})
    end
    @all_layouts = configs.collect do |id, config|
      if parent = config['extends']
        config = configs[parent].merge(config) 
      end
      self.new(id, config.dup)
    end
  end
  return @all_layouts
end

+ (Object) find(id)



60
61
62
# File 'app/models/layout.rb', line 60

def self.find(id)
  self.all.find{ |layout| layout.id == id }
end

Instance Method Details

- (Object) find_variant(id)



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/layout.rb', line 19

def find_variant(id)
  var = self.config[id]
  if var
    var[:id]=id
    # Set column defaults
    self.config["column_defaults"].each do |col, conf|
      if var.has_key?(col)
        var[col] = conf.dup.merge(var[col]||{})
      end
    end
  end
  var
end

- (Object) parent



56
57
58
# File 'app/models/layout.rb', line 56

def parent
  @parent ||= Layout.find(self.config["extends"])
end

- (Object) settings_partial



37
38
39
40
41
42
43
44
# File 'app/models/layout.rb', line 37

def settings_partial
  partial_path = File.join(self.path, 'settings.html.haml')
  if File.exists?(partial_path)
    return partial_path
  elsif self.parent.present?
    return self.parent.settings_partial
  end
end

- (Object) targets_for_variant(variant)



33
34
35
# File 'app/models/layout.rb', line 33

def targets_for_variant(variant)
  variant.select { |key,val| ![:id, "name", 'inheritable'].include?(key) }  
end

- (Object) targets_partial(variant)



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

def targets_partial(variant)
  variant_name = variant[:id] != "default" ? variant[:id] : ''
  partial_path = File.join(self.path, variant_name, 'targets.html.haml')
  if File.exists?(partial_path)
    return partial_path
  elsif self.parent.present?
    return self.parent.targets_partial(variant)
  end
end

- (Object) variants



13
14
15
16
17
# File 'app/models/layout.rb', line 13

def variants
  self.config.collect do |variant, config|
    [config["name"], variant] unless ['extends', "column_defaults"].include?(variant) || !config
  end.compact
end