Skip to content

Instantly share code, notes, and snippets.

@davidwparker
Created September 18, 2008 02:26
Show Gist options
  • Save davidwparker/11359 to your computer and use it in GitHub Desktop.
Save davidwparker/11359 to your computer and use it in GitHub Desktop.
class Announcement < ActiveRecord::Base
named_scope :active, lambda { { :conditions => ['starts_at <= ? AND ends_at >= ?', Time.now.utc, Time.now.utc] } }
named_scope :since, lambda { |hide_time| { :conditions => (hide_time ? ['updated_at > ? OR starts_at > ?', hide_time.utc, hide_time.utc] : nil) } }
def self.current_announcements(hide_time)
active.since(hide_time)
end
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Announcement do
before(:each) do
@announcement = Announcement.new
end
#TODO these are weak/inaccurate specs. These are more-or-less stubs
describe "the current_announcements" do
it "should return announcements that are active" do
time = Time.now.utc
Announcement.active.should == Announcement.find(:all, :conditions => ['starts_at <= ? AND ends_at >= ?', time, time])
end
it "should return announcements since a time provided" do
time = Time.now.utc
Announcement.since.should == Announcement.find(
:all,
:conditions => (time ? ['updated_at > ? OR starts_at > ?', time.utc, time.utc] : nil))
end
it "should return announcements since a time provided (nil)" do
time = nil
Announcement.since.should == Announcement.find(
:all,
:conditions => (time ? ['updated_at > ? OR starts_at > ?', time.utc, time.utc] : nil))
end
end
end
//jgrowl announcements. ajax GET to hide ann/store cookie
$.jGrowl.defaults.closer = false;
$("#announcements_box").css("display","none");
$("#announcements_box .announcement").each(function(){
$.jGrowl(this.textContent,{ sticky:true, close:function(e,m,o){hide_announcements();} });
});
function hide_announcements(){
$.get(
'/hide_announcements'
);
$("#announcements_box").fadeOut();
return false;
}
- unless current_announcements.empty?
#announcements_box
-for announcement in current_announcements
.announcement{:id => 'announcement_' + announcement.id.to_s}
=h announcement.message
= link_to "Hide Announcements", hide_announcements_path, :id => 'hideAnn'
def current_announcements
unless session[:announcement_hide_time].nil?
time = session[:announcement_hide_time]
else
time = cookies[:announcement_hide_time].to_datetime unless cookies[:announcement_hide_time].nil?
end
@current_announcements ||= Announcement.current_announcements(time)
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe ApplicationHelper do
describe "should set the proper html title string" do
it "if the string is blank, it should yield nothing" do
helper.set_html_title(nil).should == nil
end
it "if the string is a valid string, it should yield the string" do
helper.set_html_title("test").should == "test"
end
end
#TODO these are weak/inaccurate specs. These are more-or-less stubs
describe "should find the current announcements" do
it "when the session['announcement_hide_time'] is not nil" do
session['announcement_hide_time'] = Time.now.utc
current_announcements.should == Announcement.current_announcements(session['announcement_hide_time'])
end
it "when the session['announcement_hide_time'] is nil and cookies['announcement_hide_time'] is not nil" do
session['announcement_hide_time'] = nil
# cookies['announcement_hide_time'] = CGI::Cookie.new('announcement_hide_time', Time.now.utc.to_datetime.to_s)
time = Time.now.utc
request.cookies['announcement_hide_time'] = {:value => time.to_datetime.to_s,
:expires => time.next_week}
# current_announcements.should == Announcement.current_announcements(cookies['announcement_hide_time'].value.first.to_datetime)
end
it "when the session['announcement_hide_time'] and cookies['announcement_hide_time'] are not nil" do
session['announcement_hide_time'] = nil
cookies['announcement_hide_time'] = nil
current_announcements.should == Announcement.current_announcements(nil)
end
end
end
class JavascriptsController < ApplicationController
def hide_announcements
time = Time.now.utc
set_session time
set_cookies time
respond_to do |format|
format.html { redirect_to(root_path) }
format.js { redirect_to(root_path) }
end
end
private
def set_session(time)
session[:announcement_hide_time] = time
end
#TODO change expiration time to be the expiration date from the list in current_announcements
def set_cookies(time)
cookies[:announcement_hide_time] = {
:value => time.to_datetime.to_s,
:expires => time.next_week
}
end
end
map.hide_announcements '/hide_announcements', :controller => 'javascripts', :action => 'hide_announcements'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment