Class: Page

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reposition(ids) ⇒ Object

Re-position: change the sequence of pages for a given course


48
49
50
51
52
53
54
55
# File 'app/models/page.rb', line 48

def self.reposition(ids)
  #if database is mysql
  #    update_all(
  #      ['position = FIND_IN_SET(id, ?)', ids.join(',')],
  #      { :id => ids }
  #    )
  update_all(["position = STRPOS(?, ','||id||',')", ",#{ids.join(',')},"], {:id => ids})
end

Instance Method Details

#course_nameObject


57
58
59
# File 'app/models/page.rb', line 57

def course_name
  self.course.nil? ? nil : self.course.name
end

#course_name=(course_name) ⇒ Object


61
62
63
64
# File 'app/models/page.rb', line 61

def course_name=(course_name)
  course = Course.first_offering_for_course_name(course_name)
  self.course = course
end

#current_semester_courseObject


105
106
107
108
109
110
111
112
113
# File 'app/models/page.rb', line 105

def current_semester_course
  #This little bit of magic finds the current offering of a course. This is handy for deliverable submission
  #and team lists where the static curriculum website points to the latest offering of the course.
  unless self.course.blank? || self.course.number.blank?
    Course.in_current_semester_with_course_number(self.course.number).first
  else
    nil
  end
end

#delete_from_searchObject


99
100
101
102
103
# File 'app/models/page.rb', line 99

def delete_from_search
  api = IndexTank::Client.new(ENV['WHITEBOARD_SEARCHIFY_API_URL'] || '<API_URL>')
  index = api.indexes(ENV['WHITEBOARD_SEARCHIFY_INDEX'] || 'cmux')
  index.document(self.id.to_s).delete
end

#editable?(current_user) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
28
29
30
# File 'app/models/page.rb', line 25

def editable?(current_user)
  return false if self.is_duplicated_page?
  return false if current_user.blank?
  return true if self.is_editable_by_all?
  return (current_user.is_staff? || current_user.is_admin?)
end

#is_someone_else_currently_editing_page(current_user) ⇒ Object


116
117
118
# File 'app/models/page.rb', line 116

def is_someone_else_currently_editing_page(current_user)
  self.current_edit_by != current_user
end

#task_numberObject


67
68
69
70
# File 'app/models/page.rb', line 67

def task_number
  match = self.title.match /\d+/
  match.nil? ? nil : match[0]
end

#timeout_has_not_passedObject


120
121
122
123
124
125
126
# File 'app/models/page.rb', line 120

def timeout_has_not_passed
  if self.current_edit_started_at.nil?
    false
  else
    Time.now - self.current_edit_started_at < 5.minute
  end
end

#to_paramObject


42
43
44
# File 'app/models/page.rb', line 42

def to_param
  url
end

#update_search_indexObject


72
73
74
75
76
77
78
# File 'app/models/page.rb', line 72

def update_search_index
  if self.viewable_by == "staff"
    update_search_index_for_index(ENV['WHITEBOARD_SEARCHIFY_STAFF_INDEX'] || 'cmu_staffx')
  else
    update_search_index_for_index(ENV['WHITEBOARD_SEARCHIFY_INDEX'] || 'cmux')
  end
end

#update_search_index_for_index(index_name) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/page.rb', line 80

def update_search_index_for_index(index_name)
  api = IndexTank::Client.new(ENV['WHITEBOARD_SEARCHIFY_API_URL'] || '<API_URL>')
  index = api.indexes(index_name)
  options_hash = {:title => self.title, :type => "page"}
  if self.course
    options_hash.merge!({:title => self.title + " (" + self.course.name + ")"})
  end

  begin
    index.document(self.id.to_s).add(options_hash.merge!({:text => self.tab_one_contents.gsub(/<\/?[^>]*>/, ""), :url => "pages/" + self.url}))
    if self.is_task?
      index.document(self.id.to_s + "-tabs-1").add(options_hash.merge!({:text => self.tab_two_contents.gsub(/<\/?[^>]*>/, ""), :url => "pages/" + self.url + "?tab=tabs-2"}))
      index.document(self.id.to_s + "-tabs-2").add(options_hash.merge!({:text => self.tab_three_contents.gsub(/<\/?[^>]*>/, ""), :url => "pages/" + self.url + "?tab=tabs-3"}))
    end
  rescue Exception => e
    logger.error("Searchify issue: " + e.message)
  end
end

#viewable?(current_user) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
35
36
37
38
39
40
# File 'app/models/page.rb', line 32

def viewable?(current_user)
  return true if self.viewable_by == "world"
  return false if current_user.blank?
  if self.viewable_by == "users"
    return current_user.present?
  else
    return (current_user.is_staff? || current_user.is_admin?)
  end
end