2008-06-27 00:13:15 -04:00
class Album < ActiveRecord :: Base
validates_presence_of :name
validates_uniqueness_of :name
validates_format_of :name , :with = > / ^[ \ w ]+$ /
has_and_belongs_to_many :tags , :order = > 'tags.name ASC'
2008-08-13 16:12:58 -04:00
has_many :photos , :order = > 'filename ASC'
2008-06-27 00:13:15 -04:00
after_create :save_tags
2008-07-03 12:37:33 -04:00
belongs_to :album_thumbnail , :class_name = > 'Photo'
2008-06-27 00:13:15 -04:00
def tag_names
self . tags . collect { | t | t . name } . join ( ' ' )
end
def tag_names = ( newtags )
tag_name_ary = newtags . split if newtags . is_a? ( String )
tag_name_ary || = newtags
new_tags = [ ]
tag_name_ary . each do | tname |
t = Tag . find_by_name tname
t || = Tag . create :name = > tname
new_tags << t
end
self . tags = new_tags
end
def self . for_select
self . find ( :all , :select = > 'name' , :order = > 'name ASC' ) . collect do | a |
a . name
end
end
def self . popular_tags ( limit = nil )
query = " SELECT tags.id, tags.name, count(*) AS count FROM albums_tags, tags, albums WHERE tags.id = tag_id AND albums_tags.album_id = albums.id GROUP BY tags.id, tags.name ORDER BY tags.name ASC "
query << " LIMIT #{ limit } " unless limit . nil?
Tag . find_by_sql ( query )
end
protected
def save_tags
self . tags . each { | x | x . save }
end
2008-08-13 16:12:58 -04:00
end