app/models/announcement.rb in cartoonist-announcements-0.0.16 vs app/models/announcement.rb in cartoonist-announcements-0.0.17
- old
+ new
@@ -1,7 +1,11 @@
+# TODO: Make this searchable
class Announcement < ActiveRecord::Base
- attr_accessible :posted_at, :expired_at, :title, :content, :location, :enabled
+ include Postable
+ include Expirable
+ include Lockable
+ attr_accessible :posted_at, :expired_at, :title, :content, :location
validate :posted_at_must_be_before_expired_at, :posted_at_must_exist_if_expired_at_exists
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"
@@ -12,27 +16,49 @@
if expired_at && !posted_at
errors.add :posted_at, "must exist if expiration exists"
end
end
+ def as_hash
+ {
+ :id => id,
+ :title => title,
+ :content => Markdown.render(content),
+ :location => location
+ }
+ end
+
class << self
- def disabled
- where :enabled => false
+ def create_announcement(params)
+ create :title => params[:title], :content => params[:content], :location => params[:location], :locked => true
end
- def enabled
- where :enabled => true
+ 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
- def active
- where "posted_at < ? AND (expired_at IS NULL OR expired_at > ?)", DateTime.now, DateTime.now
+ def actives_as_hash
+ active.creation_order.map &:as_hash
end
- def expired
- where "expired_at < ?", DateTime.now
+ def creation_order
+ order :id
end
+ def active
+ posted.unexpired
+ end
+
def future
- where "posted_at IS NULL OR posted_at > ?", DateTime.now
+ unposted
end
end
end