geetarista (owner)

Fork Of

Revisions

gist: 62732 Download_button fork
public
Public Clone URL: git://gist.github.com/62732.git
announcement.rb
1
2
3
4
5
6
7
8
9
10
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
announcement_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
  //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;
  }
application.html.haml
1
2
3
4
5
6
        - 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'
application_helper.rb
1
2
3
4
5
6
7
8
  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
application_helper_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
javascripts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
routes.rb
1
2
  map.hide_announcements '/hide_announcements', :controller => 'javascripts', :action => 'hide_announcements'