Module: PostsHelper

Included in:
BaseCell, CommentsController, PingerJob, PostsController
Defined in:
app/helpers/posts_helper.rb

Overview

helpers for posts

Instance Method Summary collapse

Instance Method Details


7
8
9
# File 'app/helpers/posts_helper.rb', line 7

def permalink_url(post)
  post_permalink_url(post.slug_id.presence || post.id)
end

#post_description_short(post, max_length = 500) ⇒ Object


34
35
36
# File 'app/helpers/posts_helper.rb', line 34

def post_description_short(post, max_length = 500)
  text_short(post.body, max_length)
end

#post_descritpion_has_image?(post) ⇒ Boolean

Returns:

  • (Boolean)

30
31
32
# File 'app/helpers/posts_helper.rb', line 30

def post_descritpion_has_image?(post)
  !Nokogiri::HTML(post_description_with_cut(post)).css('img').empty?
end

#post_teaser_for(post) ⇒ Object


54
55
56
57
# File 'app/helpers/posts_helper.rb', line 54

def (post)
  return nil unless PostTeaser.exists?
  PostTeaser.offset(post.id % PostTeaser.count).first.body
end

#smart_post_path(post, options = {}) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/posts_helper.rb', line 11

def smart_post_path(post, options = {})
  blog_slug = Rails.cache.fetch("Blog#slug##{post.blog_id}", expires_in: 1.hour) { post.blog.slug }

  options = {
    controller: :posts, action: :show,
    blog: blog_slug,
    year: post.created_at.year,
    month: post.created_at.strftime('%m'),
    day: post.created_at.strftime('%d')
  }.merge(options)

  options[:id] = post.to_param
  post_url options.merge(only_path: true)
end

#smart_post_url(post, options = {}) ⇒ Object


26
27
28
# File 'app/helpers/posts_helper.rb', line 26

def smart_post_url(post, options = {})
  "http://#{Rails.configuration.x.domain}#{smart_post_path(post, options)}"
end

#text_short(html, max_length = 500) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/posts_helper.rb', line 38

def text_short(html, max_length = 500)
  text = strip_tags(CGI.unescapeHTML(html))

  if text.mb_chars.length > max_length
    text = text.mb_chars[0..max_length - 1]

    result = text.sub(/[^.!?]+\z/, '')

    result = text[0..-6] + ' […] ' if result.size < (max_length * 0.65)

    raw(result)
  else
    text
  end
end

#typography(html) ⇒ Object


3
4
5
# File 'app/helpers/posts_helper.rb', line 3

def typography(html)
  raw Typogruby.improve(html)
end