Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created March 19, 2011 05:22
Show Gist options
  • Save patrick99e99/877238 to your computer and use it in GitHub Desktop.
Save patrick99e99/877238 to your computer and use it in GitHub Desktop.
album model
class Album < ActiveRecord::Base
has_many :assets
belongs_to :user
def self.formatted
all.map do |album|
{:id => album.id,
:name => album.name}
end
end
def formatted_assets(favorite_ids)
assets.map {|asset| asset.to_formatted(favorite_ids)}
end
end
class Asset < ActiveRecord::Base
belongs_to :user
has_many :favorites
named_scope :photos, {:conditions => {:type => "Photo"}}
named_scope :videos, {:conditions => {:type => "Video"}}
named_scope :audios, {:conditions => {:type => "Audio"}}
def to_formatted(favorite_ids = [])
{:id => self.id,
:name => self.name,
:album_id => self.album_id,
:src => self.attachment.url(:small),
:is_private => self.is_private,
:is_favorite => favorite_ids.include?(self.id) }
end
end
class PhotosController < ApplicationController
def index
@favorite_ids = current_user.favorites.map(&:asset_id)
@photo_album = Album.find(params[:photo_album_id])
@photos = @photo_album.formatted_assets(@favorite_ids)
render :json => @photos.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment