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



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

def active
  posted.unexpired
end

.actives_as_hashObject



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

def actives_as_hash
  active.creation_order.map &:as_hash
end

.create_announcement(params) ⇒ Object



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

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

.creation_orderObject



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

def creation_order
  order :id
end

.futureObject



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

def future
  unposted
end

.update_announcement(params) ⇒ Object



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

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



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

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

#posted_at_must_be_before_expired_atObject



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

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



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

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