cirandas.net

ref: master

plugins/recent_content/lib/recent_content_block.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
class RecentContentBlock < Block

  settings_items :presentation_mode, :type => String, :default => 'title_only'
  settings_items :total_items, :type => Integer, :default => 5
  settings_items :show_blog_picture, :type => :boolean, :default => false
  settings_items :selected_folder, :type => Integer

  attr_accessible :presentation_mode, :total_items, :show_blog_picture, :selected_folder

  VALID_CONTENT = ['TextArticle']

  def self.description
    c_('Recent content')
  end

  def help
    _('This block displays all articles inside the blog you choose. You can edit the block to select which of your blogs is going to be displayed in the block.')
  end

  def articles_of_folder(folder, limit)
   holder.articles.where(type: VALID_CONTENT, parent_id: folder.id).
     order('created_at DESC').limit(limit)
  end

  def holder
    return nil if self.box.nil? || self.box.owner.nil?
    if self.box.owner.kind_of?(Environment)
      return nil if self.box.owner.portal_community.nil?
      self.box.owner.portal_community
    else
      self.box.owner
    end
  end

  def parents
    self.holder.nil? ? [] : self.holder.articles.where(type: 'Blog')
  end

  def root
    unless self.selected_folder.nil?
      holder.articles.where(id: self.selected_folder).first
    end
  end

  include DatesHelper

  def mode?(attr)
    attr == self.presentation_mode
  end

  def api_content(params = {})
    children = self.articles_of_folder(self.root, self.total_items)
    {:articles => Api::Entities::ArticleBase.represent(children)}.as_json
  end

  def display_api_content_by_default?
    false
  end
end