Class: Topic

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/topic.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.visibility(value) ⇒ Object



26
27
28
29
30
31
32
# File 'app/models/topic.rb', line 26

def self.visibility(value)
  case value
  when 0 then "everyone"
  when 1 then "any registered user"
  else "a user with a forum level of at least #{value}" 
  end
end

Instance Method Details



53
54
55
# File 'app/models/topic.rb', line 53

def link
  "/forums/#{self.url}"
end

#read_visibilityObject



34
35
36
# File 'app/models/topic.rb', line 34

def read_visibility
  Topic.visibility(self.read_access_level)
end

#recent_threads(user, count) ⇒ Object



20
21
22
23
24
# File 'app/models/topic.rb', line 20

def recent_threads(user, count)
  read_level = user ? user.forum_level : 0

  self.topic_threads.limit(count).order("topic_threads.id desc").where("topic_threads.is_visible = 1").includes(:topic).where("topics.read_access_level <= #{read_level}")
end

#unique_name_within_categoryObject



42
43
44
45
46
47
# File 'app/models/topic.rb', line 42

def unique_name_within_category
  cnt = Topic.sys(self.system_id).where(:topic_category_id=>self.topic_category_id).where(:name=>self.name).where(["id<>?", self.new_record? ? -1 : self.id]).count
  if cnt > 0
    errors.add(:name, "is not unique within this category")
  end
end

#update_urlObject



49
50
51
# File 'app/models/topic.rb', line 49

def update_url
  self.url = self.name.urlise
end

#write_visibilityObject



38
39
40
# File 'app/models/topic.rb', line 38

def write_visibility
  Topic.visibility([self.topic_category.write_access_level, self.write_access_level].max)
end