cirandas.net

ref: master

plugins/community_track/lib/community_track_plugin/track.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
79
80
81
82
83
84
85
86
87
88
89
class CommunityTrackPlugin::Track < Folder

  settings_items :goals, :type => :string
  settings_items :expected_results, :type => :string

  validate :validate_categories

  attr_accessible :goals, :expected_results

  def comments_count
    @comments_count = sum_children_comments self unless @comments_count
    @comments_count
  end

  def validate_categories
    errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank?
  end

  def self.icon_name(article = nil)
    'community-track'
  end

  def self.short_description
    _("Track")
  end

  def self.description
    _('Defines a track.')
  end

  def steps
    #XXX article default order is name (acts_as_filesystem) -> should use reorder (rails3)
    steps_unsorted.sort_by(&:position).select{|s| !s.hidden}
  end

  def hidden_steps
    steps_unsorted.select{|s| s.hidden}
  end

  def reorder_steps(step_ids)
    transaction do
      step_ids.each_with_index do |step_id, i|
        step = steps_unsorted.find(step_id)
        step.update_attribute(:position, step.position = i + 1)
      end
    end
  end

  def steps_unsorted
    children.where(:type => 'CommunityTrackPlugin::Step')
  end

  def accept_comments?
    false
  end

  def sum_children_comments node
    result = 0
    node.children.each do |c|
      result += c.comments_count
      result += sum_children_comments c
    end
    result
  end

  def css_class_name
    "community-track-plugin-track"
  end

  def first_paragraph
    return '' if body.blank?
    paragraphs = Nokogiri::HTML.fragment(body).css('p')
    paragraphs.empty? ? '' : paragraphs.first.to_html
  end

  def category_name
    category = categories.first
    category = category.top_ancestor unless category.nil?
    category.nil? ? '' : category.name
  end

  def to_html(options = {})
    track = self
    proc do
      render :file => 'content_viewer/track', :locals => {:track => track}
    end
  end

end