Class: Broadway::Post

Inherits:
Object
  • Object
show all
Includes:
Assetable, Configurable, Convertible, Hierarchical, Layoutable, Linkable, Processable, Publishable, Readable, Resourceful, Sluggable, Sortable, Taggable, Themeable, Comparable
Defined in:
lib/broadway/resources/post.rb

Instance Attribute Summary

Attributes included from Themeable

#template

Attributes included from Layoutable

#layout

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Themeable

#template_extensions, #theme

Methods included from Linkable

included

Methods included from Publishable

included

Methods included from Assetable

included

Methods included from Taggable

included

Methods included from Sortable

included

Methods included from Sluggable

included

Methods included from Configurable

included

Methods included from Readable

included

Methods included from Hierarchical

included

Methods included from Resourceful

included

Methods included from Convertible

included

Methods included from Processable

included

Class Method Details

.from_yaml(content) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/broadway/resources/post.rb', line 116

def from_yaml(content)
  if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    self.data = YAML.load($1)
    self.data.each do |k, v|
      if self.respond_to?("#{k}=")
        self[k] = v
      end
    end
    content = content[($1.size + $2.size)..-1]
  end
  self.body = content
end

.to_yaml(record) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/broadway/resources/post.rb', line 106

def to_yaml(record)
  result = "---\n"
  @yaml[0].each do |attribute|
    result << "#{attribute.to_s}: \"#{record.send(attribute)}\"\n"
  end
  result << "\n"
  result << record.instance_eval(&@yaml[1])
  result
end

.yaml(attributes = nil, &block) ⇒ Object



102
103
104
# File 'lib/broadway/resources/post.rb', line 102

def yaml(attributes = nil, &block)
  @yaml = [attributes, block]
end

Instance Method Details

#bodyObject



44
45
46
# File 'lib/broadway/resources/post.rb', line 44

def body
  read
end

#descriptionObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/broadway/resources/post.rb', line 48

def description
  unless @description
    if data["description"]
      @description = data["description"]
    else
      @description = "" # read
    end
  end

  @description
end

#formatObject



40
41
42
# File 'lib/broadway/resources/post.rb', line 40

def format
  extension
end


85
86
87
# File 'lib/broadway/resources/post.rb', line 85

def gallery
  data["gallery"] || nil
end


89
90
91
# File 'lib/broadway/resources/post.rb', line 89

def menu_title
  data["menu_title"] || title
end

#parent_pathObject



97
98
99
# File 'lib/broadway/resources/post.rb', line 97

def parent_path
  ("/" + categories.join("/")).squeeze("/")
end

#self_and_siblingsObject



60
61
62
63
64
65
66
# File 'lib/broadway/resources/post.rb', line 60

def self_and_siblings
  if parent
    parent.children.blank? ? nil : parent.children
  else
    nil
  end
end

#sluggifyObject



20
21
22
23
24
25
26
27
28
# File 'lib/broadway/resources/post.rb', line 20

def sluggify
  {
    "year"       => "",# date ? date.strftime("%Y") : "",
    "month"      => "",# date ? date.strftime("%m") : "",
    "day"        => "",# date ? date.strftime("%d") : "",
    "title"      => kind == "page" ? "" : slug.value,
    "categories" => categories.join('/') # page is "categories[0..-2].join('/')"
  }
end


68
69
70
71
72
73
# File 'lib/broadway/resources/post.rb', line 68

def submenu
  return nil if data["submenu"].blank?
  data["submenu"].map do |path|
    site.find_post_by_path(path)
  end.compact
end

#subpagesObject



75
76
77
78
79
80
81
82
83
# File 'lib/broadway/resources/post.rb', line 75

def subpages
  (self_and_siblings || submenu || children).sort do |a, b|
    if a.data["position"] && b.data["position"]
      a.data["position"].to_i <=> b.data["position"]
    else
      a <=> b
    end
  end
end

#subtitleObject



93
94
95
# File 'lib/broadway/resources/post.rb', line 93

def subtitle
  data["subtitle"] || nil
end

#to_hashObject



30
31
32
33
34
35
36
37
38
# File 'lib/broadway/resources/post.rb', line 30

def to_hash
  { "title"      => self.title,
    "url"        => self.url,
    "date"       => self.date,
    "id"         => self.id,
    "categories" => self.categories,
    "tags"       => self.tags,
    "content"    => self.content }.deep_merge(self.data)
end