Class: Factories::PostFactory

Inherits:
BaseFactory show all
Defined in:
lib/factories/post_factory.rb

Overview

This class is a factory for parsing post text and creating a correseponding post model

Constant Summary collapse

LEAD =
'{: .lead}'
BREAK =
'<!–-break-–>'
DEFAULT_HERO =

serves as the default hero for a post if none is provided.

'https://source.unsplash.com/collection/145103/'

Instance Method Summary collapse

Instance Method Details

#create_jekyll_post_text(text, author, title, tags = nil, overlay = nil, hero = nil, set_published_property = false, append_lead_break_section = false) ⇒ Object

This method takes parameters for a given post and formats them as a valid post for a Jekyll website

Params:

text

the required markdown contents of the post

author

the required author of the post

title

the required title of the post

tags

optional tags specific to the post, defaults to nil

overlay

the optional overlay color of the post, defaults to nil

hero

a link to an optional background image for a post, defaults to nil

set_published_property::an optional flag to set the published: true property for a post, defaults to false append_lead_break_section::an optional flag indicating whether to append to lead break section to a post, default to false



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/factories/post_factory.rb', line 39

def create_jekyll_post_text(text, author, title, tags = nil, overlay = nil,
                            hero = nil, set_published_property = false, append_lead_break_section = false)
  header_converted_text = fix_header_syntax(text)
  header_converted_text = add_line_break_to_markdown_if_necessary(header_converted_text)

  parsed_tags = nil
  parsed_tags = format_tags(tags) if tags

  tag_section = %(tags:
#{parsed_tags})

  lead_break_section = "{: .lead}\r\n<!–-break-–>"

  hero_to_use = hero
  hero_to_use = DEFAULT_HERO if hero_to_use&.empty?
  result = %(---
layout: post
title: #{title}
author: #{author}\r\n)

  result += "#{tag_section}\r\n" unless !parsed_tags || parsed_tags.empty?
  result += "hero: #{hero_to_use}\n" if hero_to_use
  result += "overlay: #{overlay}\n" if overlay
  result += "published: true\n" if set_published_property
  result += "---\n"
  result += "#{lead_break_section}\n" if append_lead_break_section
  result += header_converted_text

  result
end

#create_post(post_contents, file_path, ref) ⇒ Object

This method parses markdown in a post a returns a post model

Params: post_contents::markdown in a given post file_path::the path on GitHub to the post ref::a sha for a ref indicating the head of a branch a post is pushed to on the GitHub server



22
23
24
# File 'lib/factories/post_factory.rb', line 22

def create_post(post_contents, file_path, ref)
  create_post_model(post_contents, file_path, ref) if !post_contents.nil? && post_contents.is_a?(String)
end