Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

day spec stuff needs help

  • Loading branch information...
commit 7486515f3e36bbec02f3e3613f5e12fe5d5b3e43 1 parent 8866a11
@sb1752 authored
View
0  FETCH_HEAD
No changes.
View
17 app/models/day.rb
@@ -0,0 +1,17 @@
+class Day < ActiveRecord::Base
+ attr_accessor :service_id, :status_id, :date
+ validates :service_id, :date, :presence => true
+
+ # This essentially removes all records for this service and replaces them with new ones
+ def self.update_statuses_for(service_id)
+ # First delete all the day records for this particular service_id
+ delete_all("service_id = #{service_id}")
+
+ # Then, insert the most recent event for each of the last 6 days
+ 6.times do |i|
+ most_recent_event = Event.where("created_at < (CURRENT_TIMESTAMP - interval '#{i}' day) AND service_id = #{service_id}").order("created_at DESC").first
+ create( :service_id => service_id, :status_id => most_recent_event.status_id, :date => Date.parse(current_timestamp.to_s) )
+ end
+
+ end
+end
View
11 app/models/event.rb
@@ -1,3 +1,14 @@
class Event < ActiveRecord::Base
+ attr_accessor :message, :service_id, :status_id
validates :message, :service_id, :status_id, :presence => true
+
+ after_save :update_days_records
+ after_update :update_days_records
+
+ private
+
+ def update_days_records
+ Day.update_statuses_for self.service_id
+ end
+
end
View
9 db/migrate/20120219234738_create_days.rb
@@ -0,0 +1,9 @@
+class CreateDays < ActiveRecord::Migration
+ def change
+ create_table :days do |t|
+ t.integer :status_id
+ t.integer :service_id
+ t.date :date
+ end
+ end
+end
View
8 db/schema.rb
@@ -11,7 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120218193113) do
+ActiveRecord::Schema.define(:version => 20120219234738) do
+
+ create_table "days", :force => true do |t|
+ t.integer "status_id"
+ t.integer "service_id"
+ t.date "date"
+ end
create_table "events", :force => true do |t|
t.string "message"
View
5 spec/fabricators/day_fabricator.rb
@@ -0,0 +1,5 @@
+Fabricator(:day) do
+ status_id 1
+ service_id 1
+ day "2012-02-19"
+end
View
6 spec/fabricators/event_fabricator.rb
@@ -0,0 +1,6 @@
+Fabricator(:event) do
+ message 'test message'
+ service_id 1
+ status
+ created_at { Fabricate.sequence(:created_at) { |i| Time.now().end_of_day - 60*60*24*i } }
+end
View
2  spec/fabricators/service_fabricator.rb
@@ -1,3 +1,3 @@
Fabricator(:service) do
name { Fabricate.sequence(:name) { |i| "Service_#{i}" } }
-end
+end
View
4 spec/fabricators/status_fabricator.rb
@@ -0,0 +1,4 @@
+Fabricator(:status) do
+ name { Fabricate.sequence(:name) { |i| "Status_#{i}" } }
+ image { Fabricate.sequence(:image) { |i| "Image_#{i}" } }
+end
View
29 spec/models/day_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Day do
+ it { should validate_presence_of (:service_id) }
+ it { should validate_presence_of (:status_id) }
+ it { should validate_presence_of (:event_date) }
+
+ describe "all days events" do
+ # Fabricate events
+ 10.times do
+ Fabricate(:event)
+ end
+
+ let(:statuses) { Day.get_all_statuses }
+ let(:services) { Service.all }
+
+ it "returns the most recent status for each of the last 6 days for every service" do
+ services.each do |service|
+ 6.times do |i|
+ # Get the date to look at and make sure the status is the most recent status past that date
+ end_time = Time.now.end_of_day - 60*60*24*i
+ recent_status_image = service.statuses.where("events.created_at < #{end_time}").order("events.created_at DESC").first.image
+ recent_status_image.should == statuses[service.name][i]
+ end
+ end
+ end
+ end
+
+end
View
7 spec/models/event_spec.rb
@@ -26,6 +26,13 @@
event.save.should be_true
Event.count.should == 1
end
+
+ it "updates day records upon save" do
+ event.save
+ recent_status = Day.where("service_id = #{event.service_id}").order("date DESC").first
+ recent_status.status_id.should == event.status_id
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.