Permalink
Browse files

Update the photo model & API a little. Create a field to store the ph…

…oto's tag. Fix up the specs for the photo model.
  • Loading branch information...
1 parent d1df74c commit e820f71f563d95fc448ef3c4e54f459676fd2c60 @timriley committed Dec 30, 2008
View
@@ -41,11 +41,16 @@ def medium_source_url
def fullsize_source_url
attributes[:fullsize_source_url]
end
-
- def self.find_all_by_user_and_tag(user_nsid, tag)
- flickr.photos.search(:user_id => user_nsid, :tags => tag, :sort => 'date-posted-desc').collect { |photo| FlickrPhoto.new(photo.id) }
+
+ # :user_id => user_nsid, :tags => tag
+ def self.find_all(options)
+ if !options.empty?
+ flickr.photos.search({:sort => 'date-posted-desc'}.merge(options)).collect { |photo| new(photo.id) }
+ else
+ # TODO return error because we can't have optionless searches
+ end
end
-
+
protected
def get_attributes
View
@@ -1,5 +1,7 @@
class Photo < ActiveRecord::Base
+ named_scope :with_tag, lambda { |tag| { :conditions => ['tag = ?', tag] } }
+
acts_as_audited
validates_uniqueness_of :flickr_id
@@ -19,10 +21,6 @@ class Photo < ActiveRecord::Base
:previous,
:next
- def update_from_flickr
- self.attributes = self.attributes.merge(FlickrPhoto.new(self.flickr_id).attributes)
- end
-
def previous
@previous_photo ||= Photo.find(:first, :order => 'created_at DESC', :conditions => ['created_at < ?', created_at])
end
@@ -31,7 +29,17 @@ def next
@next_photo ||= Photo.find(:first, :order => 'created_at ASC', :conditions => ['created_at > ?', created_at])
end
- def self.new_from_flickr(flickr_photo)
+ def update_from_flickr
+ self.attributes = self.attributes.merge(FlickrPhoto.new(self.flickr_id).attributes)
+ end
+
+ def update_from_flickr_photo(flickr_photo)
+ if flickr_updated_at.to_i < flickr_photo.flickr_updated_at.to_i
+ self.attributes = self.attributes.merge(flickr_photo.attributes)
+ end
+ end
+
+ def self.new_from_flickr_photo(flickr_photo)
Photo.new do |photo|
photo.flickr_id = flickr_photo.id
photo.title = flickr_photo.title
@@ -45,8 +53,8 @@ def self.new_from_flickr(flickr_photo)
end
end
- def self.create_from_flickr(flickr_photo)
- photo = Photo.new_from_flickr(flickr_photo)
+ def self.create_from_flickr_photo(flickr_photo)
+ photo = Photo.new_from_flickr_photo(flickr_photo)
photo.save
photo
end
@@ -55,9 +63,19 @@ def self.create_from_flickr(flickr_photo)
# def self.flickr_photo_attributes(flickr_id)
- def self.sync_with_flickr_by_user_and_tag(nsid, tag)
- flickr_photos = FlickrPhoto.find_all_by_user_and_tag(nsid, tag)
- local_photos = Photo.find(:all)
+ # Only allow the following options:
+ # :user_id => user_id, :tag => tag
+ def self.sync_with_flickr(options = {})
+ if options.empty?
+ # TODO return an error because we need at least a :user or a :tag
+ end
+
+ search_options = {}
+ search_options[:user_id] = options[:user_id] unless options[:user_id].blank?
+ search_options[:tags] = options[:tag] unless options[:tag].blank?
+
+ flickr_photos = FlickrPhoto.find_all(search_options)
+ local_photos = Photo.all
# sort all of the current photos by flickr id
local_photos_by_flickr_id = {}
@@ -75,7 +93,7 @@ def self.sync_with_flickr_by_user_and_tag(nsid, tag)
end
else
# if the photo does not exist locally, create it.
- Photo.create_from_flickr(flickr_photo)
+ Photo.create_from_flickr_photo(flickr_photo)
end
end
end
@@ -0,0 +1,11 @@
+class AddTagToPhotos < ActiveRecord::Migration
+ def self.up
+ add_column :photos, :tag, :string
+ add_index :photos, :tag
+ end
+
+ def self.down
+ remove_index :photos, :tag
+ remove_column :photos, :tag
+ end
+end
View
@@ -1,5 +1,5 @@
begin
- Photo.sync_with_flickr_by_user_and_tag(APP_CONFIG['flickr_user_id'], APP_CONFIG['synchronise_tag'])
+ Photo.sync_with_flickr(:user_id => APP_CONFIG['flickr_user_id'], :tag => APP_CONFIG['synchronise_tag'])
rescue
# FIXME log an error when sync fails
true
View
@@ -127,15 +127,15 @@ def stub_attributes(obj, attrs)
describe "and initialializing" do
before(:each) do
- @photo = Photo.new_from_flickr(@flickr_photo)
+ @photo = Photo.new_from_flickr_photo(@flickr_photo)
end
it_should_behave_like "a photo matching a flickr photo"
end
describe "and creating" do
before(:each) do
- @photo = Photo.create_from_flickr(@flickr_photo)
+ @photo = Photo.create_from_flickr_photo(@flickr_photo)
end
it_should_behave_like "a photo matching a flickr photo"
@@ -186,15 +186,15 @@ def stub_attributes(obj, attrs)
Photo.stub!(:find).with(:all).and_return([@photo])
@flickr_photo = mock(FlickrPhoto, :id => '123')
stub_attributes(@flickr_photo, flickr_photo_attributes.with(:flickr_updated_at => 2.days.ago.beginning_of_day))
- FlickrPhoto.stub!(:find_all_by_user_and_tag).and_return([@flickr_photo])
+ FlickrPhoto.stub!(:find_all).and_return([@flickr_photo])
FlickrPhoto.stub!(:new).with(@flickr_photo.id).and_return(@flickr_photo)
end
describe "when the photos are all up to date" do
it "should do nothing" do
@local_photo_1.should_receive(:save).exactly(0).times
@local_photo_2.should_receive(:save).exactly(0).times
- Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
+ Photo.sync_with_flickr(:user_id => 'foo', :tag => 'bar')
end
end
@@ -207,32 +207,32 @@ def stub_attributes(obj, attrs)
it "should update any values that are altered in the flickr photo" do
@photo.stub!(:save).and_return(true)
lambda {
- Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
+ Photo.sync_with_flickr(:user_id => 'foo', :tag => 'bar')
}.should change(@photo, :description).from('previous desc').to(@flickr_photo.description)
end
it "should update the photo from flickr" do
@photo.stub!(:save).and_return(true)
@photo.should_receive(:update_from_flickr)
- Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
+ Photo.sync_with_flickr(:user_id => 'foo', :tag => 'bar')
end
it "should save the photo" do
@photo.should_receive(:save)
- Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
+ Photo.sync_with_flickr(:user_id => 'foo', :tag => 'bar')
end
end
describe "when new photos have been posted to flickr" do
before(:each) do
@new_flickr_photo = mock(FlickrPhoto, :id => '456')
stub_attributes(@new_flickr_photo, flickr_photo_attributes)
- FlickrPhoto.stub!(:find_all_by_user_and_tag).and_return([@new_flickr_photo, @flickr_photo])
+ FlickrPhoto.stub!(:find_all).and_return([@new_flickr_photo, @flickr_photo])
end
it "should create the new photos from flickr" do
- Photo.should_receive(:create_from_flickr).with(@new_flickr_photo)
- Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
+ Photo.should_receive(:create_from_flickr_photo).with(@new_flickr_photo)
+ Photo.sync_with_flickr(:user_id => 'foo', :tag => 'bar')
end
end
end

0 comments on commit e820f71

Please sign in to comment.