Skip to content
Browse files

add User#progress_for method for querying set of progressed days for …

…a given calendar month
  • Loading branch information...
1 parent 53440d3 commit a71985c9329008010df510ff9262afe21cc52eae @technoweenie technoweenie committed Jul 26, 2008
Showing with 85 additions and 0 deletions.
  1. +17 −0 lib/seinfeld/models.rb
  2. +39 −0 lib/seinfeld_calendar.rb
  3. +1 −0 lib/views/show.haml
  4. +28 −0 spec/user_spec.rb
View
17 lib/seinfeld/models.rb
@@ -3,8 +3,14 @@
require 'open-uri'
require 'dm-core'
require 'feed_me'
+require 'set'
module Seinfeld
+ class << self
+ attr_accessor :connection
+ end
+ self.connection = 'mysql://localhost/seinfeld'
+
class User
include DataMapper::Resource
property :id, Integer, :serial => true
@@ -38,6 +44,11 @@ def scan_for_progress
end.keys.sort
end
+ def progress_for(year, month)
+ start = Date.new(year, month)
+ Set.new progressions(:created_at => start..(start >> 1)).map { |p| Date.new(p.created_at.year, p.created_at.month, p.created_at.day) }
+ end
+
private
def get_feed
feed = nil
@@ -52,4 +63,10 @@ class Progression
property :created_at, DateTime
belongs_to :user, :class_name => "Seinfeld::User"
end
+end
+
+if $0 == __FILE__
+ DataMapper.setup :default, Seinfeld.connection
+ DataMapper.auto_migrate!
+ puts "Database reset"
end
View
39 lib/seinfeld_calendar.rb
@@ -1,3 +1,42 @@
require 'seinfeld/models'
+require 'seinfeld/calendar_helper'
require 'sinatra'
+DataMapper.setup :default, Seinfeld.connection
+
+get '/' do
+ 'oy!'
+end
+
+get '/~:name' do
+ haml :show
+end
+
+get '/~:name/:year' do
+ haml :show
+end
+
+get '/~:name/:year/:month' do
+ haml :show
+end
+
+helpers do
+ include Seinfeld::CalendarHelper
+
+ def seinfeld
+ [:year, :month].each do |key|
+ value = params[key].to_i
+ params[key] = value.zero? ? Date.today.send(key) : value
+ end
+ user = Seinfeld::User.first :login => params[:name]
+ progressions = user.progress_for(params[:year], params[:month])
+ calendar :year => params[:year], :month => params[:month] do |d|
+ if progressions.include? d
+ [d.mday, {:class => "progressed"}]
+ else
+ [d.mday, {:class => "slacked"}]
+ end
+ end
+ end
+end
+
View
1 lib/views/show.haml
@@ -0,0 +1 @@
+= seinfeld
View
28 spec/user_spec.rb
@@ -59,5 +59,33 @@ module Seinfeld
lambda { @user.update_progress }.should change { Progression.all(:user_id => @user.id).size }.by(1)
end
end
+
+ describe "#progress_for(year, month)" do
+ it "finds progress for given calendar month" do
+ @progressions.should include(Date.new(2008, 2, 1))
+ @progressions.should include(Date.new(2008, 2, 2))
+ @progressions.should_not include(Date.new(2008, 2, 3))
+ @progressions.should_not include(Date.new(2008, 1, 1))
+ @progressions.should_not include(Date.new(2008, 3, 1))
+ end
+
+ before :all do
+ User.transaction do
+ @user = User.create(:login => 'bob')
+ @user.progressions.create(:created_at => Date.new(2008, 1, 1))
+ @user.progressions.create(:created_at => Date.new(2008, 2, 1))
+ @user.progressions.create(:created_at => Date.new(2008, 2, 2))
+ @user.progressions.create(:created_at => Date.new(2008, 3, 1))
+ @progressions = @user.progress_for 2008, 2
+ end
+ end
+
+ after :all do
+ User.transaction do
+ User.all.destroy!
+ Progression.all.destroy!
+ end
+ end
+ end
end
end

0 comments on commit a71985c

Please sign in to comment.
Something went wrong with that request. Please try again.