cirandas.net

ref: master

app/helpers/blog_helper.rb


 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
73
74
75
76
77
78
module BlogHelper

  include ArticleHelper

  def custom_options_for_article(article,tokenized_children)
    @article = article
    hidden_field_tag('article[published]', 1) +
    hidden_field_tag('article[accept_comments]', 0) +
    visibility_options(article,tokenized_children) +
    content_tag('h4', _('Visualization of posts')) +
    content_tag(
      'div',
      check_box(:article, :display_preview) +
      content_tag('label', _('I want to display the preview of posts before the text'), :for => 'article_display_preview')
    )
  end

  def cms_label_for_new_children
    _('New post')
  end

  def cms_label_for_edit
    _('Configure blog')
  end

  def list_posts(articles, conf = { format: 'full', paginate: true })
    pagination = pagination_links(articles, {
      :param_name => 'npage',
      :params => {:action=>"view_page",
                  :page=>articles.first.parent.path.split('/'),
                  :controller=>"content_viewer"}
    }) if articles.present? && conf[:paginate]
    content = []
    artic_len = articles.length
    articles.each_with_index{ |art,i|
      css_add = [ 'blog-post', 'position-'+(i+1).to_s() ]
      position = (i%2 == 0) ? 'odd-post' : 'even-post'
      css_add << 'first' if i == 0
      css_add << 'last'  if i == (artic_len-1)
      css_add << position
      content << (content_tag 'div', id: "post-#{art.id}", class: css_add do
        content_tag 'div', class: position + '-inner blog-post-inner' do
          display_post(art, conf[:format])  +
          '<br style="clear:both"/>'.html_safe
        end
      end).html_safe
    }
    safe_join(content, "\n<hr class='sep-posts'/>\n".html_safe) + (pagination or '').html_safe
  end

  def display_post(article, format = 'full')
    no_comments = (format == 'full' || format == 'compact' ) ? false : true
    title = article_title(article, :no_comments => no_comments)
    method = "display_#{format.split('+')[0]}_format"
    html = send(method, FilePresenter.for(article)).html_safe
    if format.split('+')[1] == 'pic'
      img = article.first_image
      if img.blank?
        '<div class="post-pic empty"></div>'
      else
        '<div class="post-pic" style="background-image:url('+img+')"></div>'
      end
    end.to_s.html_safe +
    title.html_safe + html
  end

  def display_compact_format(article)
    render :file => 'content_viewer/_display_compact_format',
           :locals => { :article => article, :format => "compact" }
  end

  def display_full_format(article)
    html = article_to_html(article)
    html = content_tag('p', html) if ! html.include?('</p>')
    html
  end

end