cirandas.net

ref: master

app/models/forum.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
90
91
92
93
94
95
class Forum < Folder

  extend ActsAsHavingPosts::ClassMethods
  acts_as_having_posts -> { reorder 'updated_at DESC' }

  include PostsLimit

  attr_accessible :has_terms_of_use, :terms_of_use, :topic_creation

  settings_items :terms_of_use, :type => :string, :default => ""
  settings_items :has_terms_of_use, :type => :boolean, :default => false
  settings_items :topic_creation, :type => :integer, :default => AccessLevels::LEVELS[:self]
  has_and_belongs_to_many :users_with_agreement, :class_name => 'Person', :join_table => 'terms_forum_people'

  before_save do |forum|
    if forum.has_terms_of_use
      last_editor = forum.profile.environment.people.find_by(id: forum.last_changed_by_id)
      if last_editor && !forum.users_with_agreement.exists?(last_editor)
        forum.users_with_agreement << last_editor
      end
    else
      forum.users_with_agreement.clear
    end
  end

  def self.type_name
    _('Forum')
  end

  def self.short_description
    _('Forum')
  end

  def self.description
    _('An internet forum, also called message board, where discussions can be held.')
  end

  module TopicCreation
    BASE = {}
    BASE['users'] = _('Logged users')

    PERSON = {}
    PERSON['self'] = _('Me')
    PERSON['related'] = _('Friends')

    GROUP = {}
    GROUP['self'] = _('Administrators')
    GROUP['related'] = _('Members')

  end

  include ActionView::Helpers::TagHelper
  def to_html(options = {})
    proc do
      render :file => 'content_viewer/forum_page'
    end
  end

  def forum?
    true
  end

  def self.icon_name(article = nil)
    'forum'
  end

  def notifiable?
    true
  end

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

  def add_agreed_user(user)
    self.users_with_agreement << user
    self.save
  end

  def agrees_with_terms?(user)
    return true unless self.has_terms_of_use
    return false unless user
    self.users_with_agreement.exists? user
  end

  def topic_creation_access
    AccessLevels.options(1)
  end

  def allow_create?(user)
    super || AccessLevels.can_access?(topic_creation, user, profile)
  end
end