Skip to content

Commit

Permalink
Change event date:datetime to date:date & add some more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambutler committed Jan 2, 2015
1 parent cf292cc commit d000387
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/models/event.rb
Expand Up @@ -11,15 +11,19 @@ class Event < ActiveRecord::Base
after_create :create_event_task_list

def days_to_go
(date.to_time - Time.now).to_i / 1.day
(Event.last.date - Date.today).to_int.clamp(0, Float::INFINITY)
end

def formatted_time
date.strftime("%A #{date.day.ordinalize} %B %Y")
end

def in_past?
date < Time.now
date < Date.today
end

def is_today?
date == Date.today
end

def self.send_notifications(id)
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20141119172000_create_events.rb
@@ -1,7 +1,7 @@
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.datetime :date
t.date :date
t.string :location
t.boolean :active
t.string :title
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -39,7 +39,7 @@
end

create_table "events", force: true do |t|
t.datetime "date"
t.date "date"
t.string "location"
t.boolean "active"
t.string "title"
Expand Down
5 changes: 5 additions & 0 deletions lib/core_ext/numeric.rb
@@ -0,0 +1,5 @@
class Numeric
def clamp(min, max)
self < min ? min : self > max ? max : self
end
end
63 changes: 63 additions & 0 deletions spec/models/event_spec.rb
@@ -0,0 +1,63 @@
require "rails_helper"

RSpec.describe Event, :type => :model do
context "days to go" do
it "responds with 1 when the date is tomorrow" do
event = Event.create!(date: Date.today + 1)
expect(event.days_to_go).to be 1
end

it "responds with 0 when the date is in the past" do
[-1, -10, -1000].each do |days_ago|
event = Event.create!(date: Date.today + days_ago)
expect(event.days_to_go).to be 0
end
end

it "responds with 0 when the date is in today" do
event = Event.create!(date: Date.today)
expect(event.days_to_go).to be 0
end
end

context "formatted time" do
it "should format the event date time correctly" do
event = Event.create!(date: Date.parse("1/1/1970"))
expect(event.formatted_time).to eq "Thursday 1st January 1970"
end
end

context "in_past?" do
it "responds with false when the date is today" do
event = Event.create!(date: Date.today)
expect(event.in_past?).to be false
end

it "responds with false when the date is tomorrow" do
event = Event.create!(date: Date.today + 1)
expect(event.in_past?).to be false
end

it "responds with true when the date is yesterday" do
event = Event.create!(date: Date.today - 1)
expect(event.in_past?).to be true
end
end

context "is_today?" do
it "responds with true when the date is today" do
event = Event.create!(date: Date.today)
expect(event.is_today?).to be true
end

it "responds with false when the date is tomorrow" do
event = Event.create!(date: Date.today + 1)
expect(event.is_today?).to be false
end

it "responds with true when the date is yesterday" do
event = Event.create!(date: Date.today - 1)
expect(event.is_today?).to be false
end
end
end

0 comments on commit d000387

Please sign in to comment.