Class: Twirly::Post

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Serializers::Post
Defined in:
lib/twirly/post.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializers::Post

#as_json, #to_json

Class Method Details

.find(id) ⇒ Object



76
77
78
# File 'lib/twirly/post.rb', line 76

def find(id)
  Post.new(Twirly.board.cards.find { |card| card.short_id == id })
end

.publishedObject



69
70
71
72
73
74
# File 'lib/twirly/post.rb', line 69

def published
  Twirly.board.cards.map { |card|
    post = Post.new(card)
    if post.published? then post else nil end
  }.compact.sort { |p1, p2| p2.published_at <=> p1.published_at }
end

Instance Method Details

#authorsObject



56
57
58
# File 'lib/twirly/post.rb', line 56

def authors
  members.map { |member| Twirly::User.new(member) }
end

#bodyObject



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

def body
  @body ||= Liquid::Template.parse(card.desc.sub(/---.*---/mi, '').strip).render
end

#categoryObject



15
# File 'lib/twirly/post.rb', line 15

def category; card.list.name end

#existing_front_matterObject



21
22
23
# File 'lib/twirly/post.rb', line 21

def existing_front_matter
  @existing_front_matter ||= card.desc.scan(/(---.*---)/mi).flatten.first
end

#front_matterObject



60
61
62
# File 'lib/twirly/post.rb', line 60

def front_matter
  "#{processed_front_matter.to_yaml}\n---"
end

#processed_front_matterObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/twirly/post.rb', line 25

def processed_front_matter
  @processed_front_matter =
  begin
    matter = YAML::load(existing_front_matter || '{}')
    matter[:title] = title
    matter[:slug] = slug
    matter[:category] = category
    matter[:date] = short_published_at
    matter[:tags] = tags.join(', ')
    matter[:authors] = authors.map(&:username).join(', ')
    matter[:attachments] = attachments.map(&:url).join(', ') if card.attachments.any?
    matter
  end
end

#published?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/twirly/post.rb', line 48

def published?
  published_at && card.labels.any? { |label| label.name == 'published' }
end

#short_published_atObject



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

def short_published_at
  published_at.strftime('%Y-%m-%d')
end

#slugObject



52
53
54
# File 'lib/twirly/post.rb', line 52

def slug
  "#{short_published_at}-#{title.parameterize}-#{card_id}"
end

#tagsObject



17
18
19
# File 'lib/twirly/post.rb', line 17

def tags
  labels.map(&:name).reject { |label| label == 'published' }
end

#to_mdObject



64
65
66
# File 'lib/twirly/post.rb', line 64

def to_md
  "#{front_matter}\n\n#{body}"
end