Created
January 11, 2009 02:31
-
-
Save reagent/45619 to your computer and use it in GitHub Desktop.
A sample implementation of using Fleakr w/ ActiveRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Photo < ActiveRecord::Base | |
belongs_to :user | |
def self.flickr_mapping | |
{ | |
:flickr_id => :id, | |
:description => :description, | |
:title => :title, | |
:url => 'small.url' | |
} | |
end | |
def self.from_flickr(flickr_photo) | |
photo = self.find_by_flickr_id(flickr_photo.id) | |
if photo.nil? | |
photo = self.new | |
flickr_mapping.each do |attribute, flickr_attribute| | |
photo.send("#{attribute}=".to_sym, eval("flickr_photo.#{flickr_attribute}")) | |
end | |
end | |
photo | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'forwardable' | |
class PhotoSet < ActiveRecord::Base | |
extend Forwardable | |
def_delegator :flickr, :photos, :flickr_photos | |
belongs_to :user | |
has_many :photos, :foreign_key => :set_id | |
def self.flickr_mapping | |
{ | |
:flickr_id => :id, | |
:description => :description, | |
:title => :title | |
} | |
end | |
def save_photos | |
flickr_photos.each do |flickr_photo| | |
photo = self.photos.from_flickr flickr_photo | |
photo.save! | |
end | |
end | |
def self.from_flickr(flickr_set) | |
set = PhotoSet.find_by_flickr_id(flickr_set.id) | |
if set.nil? | |
set = self.new | |
flickr_mapping.each do |attribute, flickr_attribute| | |
set.send("#{attribute}=".to_sym, eval("flickr_set.#{flickr_attribute}")) | |
end | |
end | |
set | |
end | |
def flickr | |
@flickr ||= begin | |
set = Fleakr::Objects::Set.new | |
set.id = self.flickr_id | |
set | |
end | |
end | |
private :flickr | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'forwardable' | |
class User < ActiveRecord::Base | |
extend Forwardable | |
def_delegator :flickr, :photos, :flickr_photos | |
def_delegator :flickr, :sets, :flickr_sets | |
has_many :photos | |
has_many :sets, :class_name => 'PhotoSet' | |
def save_photos | |
flickr_photos.each do |flickr_photo| | |
photo = self.photos.from_flickr flickr_photo | |
photo.save! | |
end | |
end | |
def flickr | |
@flickr ||= Fleakr.user(self.flickr_username) | |
end | |
private :flickr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment