Module: EffectivePostsHelper

Defined in:
app/helpers/effective_posts_helper.rb

Instance Method Summary collapse

Instance Method Details

#effective_post_category_path(category, opts = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/effective_posts_helper.rb', line 15

def (category, opts = nil)
  return effective_posts.posts_path unless category.present?

  category = category.to_s.downcase
  opts ||= {}

  if EffectivePosts.use_category_routes
    "/#{category}"
  else
    effective_posts.posts_path(opts.merge(category: category))
  end
end

#effective_post_path(post, opts = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'app/helpers/effective_posts_helper.rb', line 4

def effective_post_path(post, opts = nil)
  category = post.category.to_s.downcase
  opts ||= {}

  if EffectivePosts.use_category_routes
    effective_posts.post_path(post, opts).sub('/posts', "/#{category}")
  else
    effective_posts.post_path(post, opts.merge(category: category))
  end
end

#link_to_post_category(category, options = {}) ⇒ Object



79
80
81
82
83
84
# File 'app/helpers/effective_posts_helper.rb', line 79

def (category, options = {})
  category = category.to_s.downcase

  href = EffectivePosts.use_category_routes ? "/#{category}" : effective_posts.posts_path(category: category.to_s)
  link_to(category.to_s.titleize, href, options)
end

#link_to_submit_post(label = 'Submit a post', options = {}) ⇒ Object

Submitting a Post



123
124
125
# File 'app/helpers/effective_posts_helper.rb', line 123

def link_to_submit_post(label = 'Submit a post', options = {})
  link_to(label, effective_posts.new_post_path, options)
end

#post_categoriesObject

Post Categories



71
72
73
# File 'app/helpers/effective_posts_helper.rb', line 71

def post_categories
  categories = EffectivePosts.categories
end

#post_excerpt(post, read_more_link: true, label: 'Read more', omission: '...', length: 200) ⇒ Object

Only supported options are: :label => 'Read more' to set the label of the 'Read More' link :omission => '...' passed to the final text node's truncate :length => 200 to set the max inner_text length of the content All other options are passed to the link_to 'Read more'



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/effective_posts_helper.rb', line 47

def post_excerpt(post, read_more_link: true, label: 'Read more', omission: '...', length: 200)
  content = effective_region(post, :body, :editable => false) { '<p>Default content</p>'.html_safe }

  divider = content.index(Effective::Snippets::ReadMoreDivider::TOKEN)
  read_more = (read_more_link && label.present?) ? readmore_link(post, label: label) : ''

  CGI.unescapeHTML(if divider.present?
    truncate_html(content, Effective::Snippets::ReadMoreDivider::TOKEN, '') + read_more
  elsif length.present?
    truncate_html(content, length, omission) + read_more
  else
    content
  end).html_safe
end

#post_meta(post, date: true, datetime: false, category: true, author: true) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/helpers/effective_posts_helper.rb', line 32

def (post, date: true, datetime: false, category: true, author: true)
  [
    'Published',
    ("on #{post.published_at.strftime('%B %d, %Y')}" if date),
    ("on #{post.published_at.strftime('%B %d, %Y at %l:%M %p')}" if datetime),
    ("to #{(post.category)}" if category && Array(EffectivePosts.categories).length > 1),
    ("by #{post.user.to_s.presence || 'Unknown'}" if author && EffectivePosts. && post.user.present?)
  ].compact.join(' ').html_safe
end


62
63
64
65
66
# File 'app/helpers/effective_posts_helper.rb', line 62

def read_more_link(post, options)
  (:p, class: 'post-read-more') do
    link_to((options.delete(:label) || 'Read more'), effective_post_path(post), (options.delete(:class) || {class: 'btn btn-primary'}).reverse_merge(options))
  end
end

#recent_news(user: current_user, category: 'news', limit: EffectivePosts.per_page) ⇒ Object

Recent News



99
100
101
102
# File 'app/helpers/effective_posts_helper.rb', line 99

def recent_news(user: current_user, category: 'news', limit: EffectivePosts.per_page)
  @recent_news ||= {}
  @recent_news[category] ||= Effective::Post.posts(user: user, category: category).limit(limit)
end

#recent_posts(user: current_user, category: nil, limit: EffectivePosts.per_page) ⇒ Object

Recent Posts



88
89
90
91
# File 'app/helpers/effective_posts_helper.rb', line 88

def recent_posts(user: current_user, category: nil, limit: EffectivePosts.per_page)
  @recent_posts ||= {}
  @recent_posts[category] ||= Effective::Post.posts(user: user, category: category).limit(limit)
end

#render_post(post) ⇒ Object



28
29
30
# File 'app/helpers/effective_posts_helper.rb', line 28

def render_post(post)
  render(partial: 'effective/posts/post', locals: { post: post })
end

#render_post_categories(reverse: false) ⇒ Object



75
76
77
# File 'app/helpers/effective_posts_helper.rb', line 75

def render_post_categories(reverse: false)
  render(partial: '/effective/posts/categories', locals: { categories: (reverse ? post_categories.reverse : post_categories) })
end

#render_recent_news(user: current_user, category: 'news', limit: EffectivePosts.per_page) ⇒ Object



104
105
106
107
# File 'app/helpers/effective_posts_helper.rb', line 104

def render_recent_news(user: current_user, category: 'news', limit: EffectivePosts.per_page)
  posts = recent_news(user: user, category: category, limit: limit)
  render partial: '/effective/posts/recent_posts', locals: { posts: posts }
end

#render_recent_posts(user: current_user, category: nil, limit: EffectivePosts.per_page) ⇒ Object



93
94
95
96
# File 'app/helpers/effective_posts_helper.rb', line 93

def render_recent_posts(user: current_user, category: nil, limit: EffectivePosts.per_page)
  posts = recent_posts(user: user, category: category, limit: limit)
  render partial: '/effective/posts/recent_posts', locals: { posts: posts }
end

#render_upcoming_events(user: current_user, category: 'events', limit: EffectivePosts.per_page) ⇒ Object



117
118
119
120
# File 'app/helpers/effective_posts_helper.rb', line 117

def render_upcoming_events(user: current_user, category: 'events', limit: EffectivePosts.per_page)
  posts = upcoming_events(user: user, category: category, limit: limit)
  render partial: '/effective/posts/upcoming_events', locals: { posts: posts }
end

#upcoming_events(user: current_user, category: 'events', limit: EffectivePosts.per_page) ⇒ Object

Upcoming Events



111
112
113
114
115
# File 'app/helpers/effective_posts_helper.rb', line 111

def upcoming_events(user: current_user, category: 'events', limit: EffectivePosts.per_page)
  @upcoming_events ||= {}
  @upcoming_events[category] ||= Effective::Post.posts(user: user, category: category).limit(limit)
    .reorder(:start_at).where('start_at > ?', Time.zone.now)
end