Class: Announcement

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Expirable, Lockable, Postable
Defined in:
app/models/announcement.rb

Overview

TODO: Make this searchable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activeObject



55
56
57
# File 'app/models/announcement.rb', line 55

def active
  posted.unexpired
end

.actives_as_hashObject



47
48
49
# File 'app/models/announcement.rb', line 47

def actives_as_hash
  active.creation_order.map &:as_hash
end

.create_announcement(params) ⇒ Object



30
31
32
# File 'app/models/announcement.rb', line 30

def create_announcement(params)
  create :title => params[:title], :content => params[:content], :location => params[:location], :locked => true
end

.creation_orderObject



51
52
53
# File 'app/models/announcement.rb', line 51

def creation_order
  order :id
end

.futureObject



59
60
61
# File 'app/models/announcement.rb', line 59

def future
  unposted
end

.update_announcement(params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/announcement.rb', line 34

def update_announcement(params)
  announcement = find params[:id].to_i
  announcement.ensure_unlocked!
  announcement.title = params[:title]
  announcement.location = params[:location]
  announcement.content = params[:content]
  announcement.post_from params
  announcement.expire_from params
  announcement.locked = true
  announcement.save!
  announcement
end

Instance Method Details

#as_hashObject



20
21
22
23
24
25
26
27
# File 'app/models/announcement.rb', line 20

def as_hash
  {
    :id => id,
    :title => title,
    :content => Markdown.render(content),
    :location => location
  }
end

#posted_at_must_be_before_expired_atObject



8
9
10
11
12
# File 'app/models/announcement.rb', line 8

def posted_at_must_be_before_expired_at
  if posted_at && expired_at && posted_at > expired_at
    errors.add :posted_at, "must be before expiration"
  end
end

#posted_at_must_exist_if_expired_at_existsObject



14
15
16
17
18
# File 'app/models/announcement.rb', line 14

def posted_at_must_exist_if_expired_at_exists
  if expired_at && !posted_at
    errors.add :posted_at, "must exist if expiration exists"
  end
end