Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
Order the photos by the flickr posted date. Add some specs for photo …
Browse files Browse the repository at this point in the history
…neighbours.
  • Loading branch information
timriley committed Dec 31, 2008
1 parent 679510d commit c707288
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 19 deletions.
4 changes: 4 additions & 0 deletions app/models/flickr_photo.rb
Expand Up @@ -26,6 +26,9 @@ def description
def taken_at
attributes[:taken_at]
end
def flickr_posted_at
attributes[:flickr_posted_at]
end
def flickr_updated_at
attributes[:flickr_updated_at]
end
Expand Down Expand Up @@ -62,6 +65,7 @@ def get_attributes
attrs[:title] = info.title
attrs[:description] = info.description
attrs[:taken_at] = info.dates.taken ? Time.parse(info.dates.taken) : nil # not everything has exif data
attrs[:flickr_posted_at] = Time.at(info.dates.posted.to_i)
# FIXME wrap lastupdate in time.parse?
attrs[:flickr_updated_at] = Time.at(info.dates.lastupdate.to_i)

Expand Down
26 changes: 15 additions & 11 deletions app/models/photo.rb
@@ -1,16 +1,19 @@
class Photo < ActiveRecord::Base


named_scope :active, :conditions => { :active => true }
named_scope :with_tag, lambda { |tag| { :conditions => ['tag = ?', tag] } }

acts_as_audited

validates_uniqueness_of :flickr_id
validates_presence_of :flickr_id, :message => "can't be blank"
validates_presence_of :title, :message => "can't be blank"
validates_presence_of :square_source_url, :message => "can't be blank"
validates_presence_of :thumb_source_url, :message => "can't be blank"
validates_presence_of :medium_source_url, :message => "can't be blank"
validates_presence_of :fullsize_source_url, :message => "can't be blank"
validates_presence_of :title, :message => "can't be blank"
validates_presence_of :flickr_posted_at, :message => "can't be blank" # this is used for photo ordering
validates_presence_of :flickr_updated_at, :message => "can't be blank" # needed for syncing

liquid_methods :id,
:title,
Expand All @@ -22,11 +25,11 @@ class Photo < ActiveRecord::Base
:next

def previous
@previous_photo ||= Photo.first(:order => 'created_at DESC', :conditions => ['created_at < ?', created_at])
@previous_photo ||= Photo.active.first(:order => 'flickr_posted_at DESC, id DESC', :conditions => ['flickr_posted_at < ?', flickr_posted_at])
end

def next
@next_photo ||= Photo.first(:order => 'created_at ASC', :conditions => ['created_at > ?', created_at])
@next_photo ||= Photo.active.first(:order => 'flickr_posted_at ASC, id ASC', :conditions => ['flickr_posted_at > ?', flickr_posted_at])
end

def update_from_flickr
Expand All @@ -41,22 +44,23 @@ def update_from_flickr_photo(flickr_photo)

def self.new_from_flickr_photo(flickr_photo)
Photo.new do |photo|
photo.flickr_id = flickr_photo.id
photo.flickr_id = flickr_photo.id # Basic info
photo.title = flickr_photo.title
photo.description = flickr_photo.description
photo.taken_at = flickr_photo.taken_at
photo.flickr_updated_at = flickr_photo.flickr_updated_at
photo.square_source_url = flickr_photo.square_source_url
photo.square_source_url = flickr_photo.square_source_url # URLs
photo.thumb_source_url = flickr_photo.thumb_source_url
photo.medium_source_url = flickr_photo.medium_source_url
photo.fullsize_source_url = flickr_photo.fullsize_source_url
photo.taken_at = flickr_photo.taken_at # Dates
photo.flickr_posted_at = flickr_photo.flickr_posted_at
photo.flickr_updated_at = flickr_photo.flickr_updated_at
end
end

def self.create_from_flickr_photo(flickr_photo)
photo = Photo.new_from_flickr_photo(flickr_photo)
photo.save
photo
returning Photo.new_from_flickr_photo(flickr_photo) do |photo|
photo.save
end
end

# Only allow the following options, :user_id => user_id, :tag => tag
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20081231022531_add_flickr_posted_at.rb
@@ -0,0 +1,9 @@
class AddFlickrPostedAt < ActiveRecord::Migration
def self.up
add_column :photos, :flickr_posted_at, :datetime
end

def self.down
remove_column :photos, :flickr_posted_at
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20081215112131) do
ActiveRecord::Schema.define(:version => 20081231022531) do

create_table "audits", :force => true do |t|
t.integer "auditable_id"
Expand Down Expand Up @@ -87,6 +87,7 @@
t.datetime "updated_at"
t.string "square_source_url"
t.string "tag"
t.datetime "flickr_posted_at"
end

add_index "photos", ["tag"], :name => "index_photos_on_tag"
Expand Down
2 changes: 1 addition & 1 deletion spec/models/flickr_photo_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe FlickrPhoto do
describe "created with a flickr id" do
before(:each) do
@flickr_dates = mock('flickr dates', :taken => "2006-11-12 15: 05: 00", :lastupdate => "1207365225")
@flickr_dates = mock('flickr dates', :taken => "2006-11-12 15: 05: 00", :lastupdate => "1207365225", :posted => "1207364225")
@flickr_info = mock('flickr info', :title => 'Dinner', :description => 'Tuna Pizza', :dates => @flickr_dates)
@flickr_url_thumb = mock('flickr thumb url', :label => 'Thumbnail', :source => 'http://flickr.com/thumbnail')
@flickr_url_med = mock('flickr medium url', :label => 'Medium', :source => 'http://flickr.com/medium')
Expand Down
75 changes: 69 additions & 6 deletions spec/models/photo_spec.rb
Expand Up @@ -8,22 +8,24 @@ def valid_photo_attributes
:square_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_s.jpg',
:thumb_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_t.jpg',
:medium_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb.jpg',
:fullsize_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_o.jpg'
:fullsize_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_o.jpg',
:flickr_posted_at => 95.days.ago.beginning_of_day,
:flickr_updated_at => 90.days.ago.beginning_of_day
}
end
def all_photo_attributes
{
valid_photo_attributes.merge({
:description => "I hunkered down every night in the library for four weeks to write my last essay. It was about wheether J. S. Mill, for his utilitarianism, can be criticised as an advocate of individualism.",
:taken_at => 100.days.ago.beginning_of_day,
:flickr_updated_at => 99.days.ago.beginning_of_day
}.merge(valid_photo_attributes)
})
end
def flickr_photo_attributes
{
:title => 'Hey',
:description => 'Some people',
:taken_at => 10.days.ago.beginning_of_day,
:flickr_updated_at => 9.days.ago.beginning_of_day,
:flickr_posted_at => 9.days.ago.beginning_of_day,
:flickr_updated_at => 8.days.ago.beginning_of_day,
:square_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_s.jpg',
:thumb_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_t.jpg',
:medium_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb.jpg',
Expand Down Expand Up @@ -77,6 +79,67 @@ def stub_attributes(obj, attrs)
end
end

# Struggling with style here. Not sure how to test this properly without hitting the DB.
describe Photo, "with neighbours" do
include PhotoSpecHelper

before(:each) do
@photos = {}

1.upto(5) do |i|
@photos[i] = Photo.create!(valid_photo_attributes.with(:active => true, :flickr_id => i, :flickr_posted_at => Time.now + i.days))
end
end

# Previous

it "should have a previous photo if there is a photo with an earlier flickr posted date" do
@photos[2].previous.should == @photos[1]
end

it "should have a previous photo only when a photo with an earlier flickr posted date is active" do
@photos[2].update_attribute(:active, false)
@photos[3].previous.should == @photos[1]
end

it "should have a previous photo with the closest date out of all active posted photos with earlier flickr posted dates" do
@photos[4].previous.should == @photos[3]
end

it "should not have a previous photo if there are photos with earlier flickr posted dates but none of them are active" do
@photos[1].update_attribute(:active, false)
@photos[2].previous.should be_nil
end

it "should not have a previous photo if there is no photo with an earlier flickr posted date" do
@photos[1].previous.should be_nil
end

# Next

it "should have a next photo if there is a photo with a later flickr posted date" do
@photos[4].next.should == @photos[5]
end

it "should have a next photo only when a photo with a later flickr posted date is active" do
@photos[4].update_attribute(:active, false)
@photos[3].next.should == @photos[5]
end

it "should have a next photo with the closest date out of all active photos with later flickr posted dates" do
@photos[1].next.should == @photos[2]
end

it "should not have a next photo if there are photos with later flickr posted dates but none of them are active" do
@photos[5].update_attribute(:active, false)
@photos[4].next.should be_nil
end

it "should not have a next photo if there is no photo with a later flickr posted date" do
@photos[5].next.should be_nil
end
end

describe Photo, "when creating" do
include PhotoSpecHelper

Expand Down Expand Up @@ -184,7 +247,7 @@ def stub_attributes(obj, attrs)
include PhotoSpecHelper

before(:each) do
@photo = Photo.new(flickr_photo_attributes.with(:flickr_id => '123', :flickr_updated_at => 2.days.ago.beginning_of_day))
@photo = Photo.new(valid_photo_attributes.with(:flickr_id => '123', :flickr_updated_at => 2.days.ago.beginning_of_day))

@flickr_photo = mock(FlickrPhoto, :id => '123')
stub_attributes(@flickr_photo, flickr_photo_attributes.with(:flickr_updated_at => 2.days.ago.beginning_of_day))
Expand Down

0 comments on commit c707288

Please sign in to comment.