cirandas.net

ref: master

plugins/context_content/lib/context_content_plugin/context_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
60
61
62
63
64
65
class ContextContentPlugin::ContextContentBlock < Block

  settings_items :show_name, :type => :boolean, :default => true
  settings_items :show_image, :type => :boolean, :default => true
  settings_items :use_parent_title, :type => :boolean, :default => false
  settings_items :show_parent_content, :type => :boolean, :default => true
  settings_items :types, :type => Array, :default => ['UploadedFile']
  settings_items :limit, :type => :integer, :default => 6

  attr_accessible :show_image, :show_name, :use_parent_title, :show_parent_content, :types

  alias :profile :owner

  include Noosfero::Plugin::HotSpot

  def self.description
    _('Display context content')
  end

  def help
    _('This block displays content based on context.')
  end

  def available_content_types
    @available_content_types ||= [UploadedFile, Event, TextArticle, Folder, Blog, Forum, Gallery, RssFeed] + plugins.dispatch(:content_types)
    checked_types = types.map {|t| t.constantize}
    checked_types + (@available_content_types - checked_types)
  end

  def first_content_types
    available_content_types.first(first_types_count)
  end

  def more_content_types
    available_content_types.drop(first_types_count)
  end

  def first_types_count
    [2, types.length].max
  end

  def types=(new_types)
    settings[:types] = new_types.reject(&:blank?)
  end

  def contents(page, p=1)
    return @children unless @children.blank?
    if page
      @children = page.children.with_types(types).order(:name).paginate(:per_page => limit, :page => p)
      (@children.blank? && show_parent_content) ? contents(page.parent, p) : @children
    else
      nil
    end
  end

  def parent_title(contents)
    return nil if contents.blank?
    contents.first.parent.name
  end

  def cacheable?
    false
  end

end