Module: Blabs::BlogPostsHelper

Defined in:
app/helpers/blabs/blog_posts_helper.rb

Constant Summary collapse

EXTRACT_LENGTH =
600

Instance Method Summary collapse

Instance Method Details

#admin_controls(blog_post) ⇒ Object



18
19
20
21
22
23
# File 'app/helpers/blabs/blog_posts_helper.rb', line 18

def admin_controls(blog_post)
  edit_link = link_to("Edit", edit_blabs_blog_post_path(blog_post))
  publish_link = publish_or_unpublish_link(blog_post)
  delete_link = link_to("Delete", blog_post, method: :delete, data: { confirm: "Are you sure?" })
  raw [edit_link, publish_link, delete_link].join(" • ")
end


85
86
87
# File 'app/helpers/blabs/blog_posts_helper.rb', line 85

def auto_link_username(body)
  body.gsub(/@(\w+)/, %Q{<a href="https://twitter.com/\\1">@\\1</a>})
end

#display_author(blog_post) ⇒ Object



89
90
91
92
93
94
95
# File 'app/helpers/blabs/blog_posts_helper.rb', line 89

def display_author(blog_post)
  if blog_post.author_twitter_username.blank?
    blog_post.author
  else
    link_to(blog_post.author, "https://twitter.com/#{blog_post.author_twitter_username.delete("@")}")
  end
end

#extract(blog_post) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/blabs/blog_posts_helper.rb', line 48

def extract(blog_post)
  if blog_post.extract.present?
    extract = blog_post.extract + " " + link_to("Read more", pretty_blog_post_path(blog_post), :class=>"more")
    formatted_body(extract)
  else
    truncate_html(formatted_body(blog_post.body), length: EXTRACT_LENGTH,
    omission: "... #{link_to("Read more", pretty_blog_post_path(blog_post), :class=>"more")}",
    escape: false)
  end
end

#field_error(form, object, field) ⇒ Object



11
12
13
14
15
16
# File 'app/helpers/blabs/blog_posts_helper.rb', line 11

def field_error(form, object, field)
  if object.errors[field] && object.errors[field].size > 0
     'div', object.errors[field].first,
      class: 'i i-info form-error'
  end
end

#formatted_body(text) ⇒ Object



25
26
27
28
29
# File 'app/helpers/blabs/blog_posts_helper.rb', line 25

def formatted_body(text)
  options = { autolink: true, lax_spacing: true, space_after_headers: true}
  renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
  auto_link_username(renderer.render(text)).html_safe
end

#index_meta_tagsObject



78
79
80
81
82
83
# File 'app/helpers/blabs/blog_posts_helper.rb', line 78

def index_meta_tags
  content_for(:meta_tags) do
    concat auto_discovery_link_tag :atom, "/blog/feed"
    concat auto_discovery_link_tag :rss, "/blog/feed.rss"
  end
end

#pretty_date(date) ⇒ Object



44
45
46
# File 'app/helpers/blabs/blog_posts_helper.rb', line 44

def pretty_date(date)
  date.strftime("#{date.day.ordinalize} %B %Y") unless date.blank?
end

#show_meta_tagsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/blabs/blog_posts_helper.rb', line 59

def show_meta_tags
  content_for(:meta_tags) do
    concat twitter_meta_tag("card", "summary")
    concat twitter_meta_tag("site", Blabs.twitter_site_username)
    concat twitter_meta_tag("creator", @blog_post.try(:author_twitter_username))
    concat twitter_meta_tag("title", @blog_post.title)

    sample = truncate(strip_tags(formatted_body(@blog_post.body)), length: EXTRACT_LENGTH)

    concat twitter_meta_tag("description", sample.strip)
    concat twitter_meta_tag("image", @blog_post.post_image_url)

    concat open_graph_meta_tag("title", @blog_post.title)
    concat open_graph_meta_tag("description", sample.strip)
    concat open_graph_meta_tag("image", @blog_post.post_image_url)
    concat open_graph_meta_tag("url", pretty_blog_post_url(@blog_post))
  end
end

#title(title) ⇒ Object



6
7
8
9
# File 'app/helpers/blabs/blog_posts_helper.rb', line 6

def title(title)
  content_for(:title, title)
  (:h1, title)
end

#validate_layoutObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/blabs/blog_posts_helper.rb', line 31

def validate_layout
  if !content_for?(:title) && !@blog_post.meta_title.blank?
    raise "You need to put a title yield in your layouts title tag.\n
          e.g. <title>My Blog - <%= yield(:title) %> </title>\n"
  end

  if !content_for?(:meta_tags)
    raise "You need to put a meta_tags yield in your layouts head tag.\n
          e.g. <%= yield(:meta_tags) %> </head>\n"
  end

end