aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers/refinery/blog/posts_helper.rb
blob: dfb742a86fa874f925ec56ccc77470463f75df7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
69
70
71
72
module Refinery
  module Blog
    module PostsHelper
      def next_or_previous?(post)
        post.next.present? or post.prev.present?
      end

      def blog_post_teaser_enabled?
        Refinery::Blog::Post.teasers_enabled?
      end

      def blog_post_teaser(post)
        if post.respond_to?(:custom_teaser) && post.custom_teaser.present?
         post.custom_teaser.html_safe
        else
         truncate(post.body, {
           :length => Refinery::Blog.post_teaser_length,
           :preserve_html_tags => true
          }).html_safe
        end
      end

      def blog_archive_widget(dates=blog_archive_dates)
        ArchiveWidget.new(dates, self).display
      end

      def blog_archive_dates(cutoff=Time.now.beginning_of_month)
        Refinery::Blog::Post.published_dates_older_than(cutoff)
      end

      class ArchiveWidget
        delegate :t, :link_to, :refinery, :render, :to => :view_context
        attr_reader :view_context

        def initialize(dates, view_context, cutoff=3.years.ago.end_of_year)
          @recent_dates, @old_dates = dates.sort_by {|date| -date.to_i }.
            partition {|date| date > cutoff }

          @view_context = view_context
        end

        def recent_links
          @recent_dates.group_by {|date| [date.year, date.month] }.
            map {|(year, month), dates| recent_link(year, month, dates.count) }
        end

        def recent_link(year, month, count)
          link_to "#{t("date.month_names")[month]} #{year} (#{count})",
            refinery.blog_archive_posts_path(:year => year, :month => month)
        end

        def old_links
          @old_dates.group_by {|date| date.year }.
            map {|year, dates| old_link(year, dates.size) }
        end

        def old_link(year, count)
          link_to "#{year} (#{count})", refinery.blog_archive_posts_path(:year => year)
        end

        def links
          recent_links + old_links
        end

        def display
          return "" if links.empty?
          render "refinery/blog/widgets/blog_archive", :links => links
        end
      end
    end
  end
end