jam-cloud/lib/jam_ruby/models/band.rb

78 lines
1.9 KiB
Ruby

module JamRuby
class Band < ActiveRecord::Base
attr_accessible :name, :website, :biography
self.primary_key = 'id'
# musicians
has_many :band_musicians
has_many :users, :through => :band_musicians, :class_name => "JamRuby::User"
# genres
has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "bands_genres"
after_save :limit_to_three_genres
def photo_url
# TODO: move image path to config
@photo_url = "http://www.jamkazam.com/images/bands/photos/#{self.id}.gif"
end
def logo_url
# TODO: move image path to config
@logo_url = "http://www.jamkazam.com/images/bands/logos/#{self.id}.gif"
end
# helper method for creating / updating a Band
def self.save(params)
if params[:id].nil?
band = Band.new()
else
band = Band.find(params[:id])
end
# name
unless params[:name].nil?
band.name = params[:name]
end
# name
unless params[:website].nil?
band.website = params[:website]
end
# name
unless params[:biography].nil?
band.biography = params[:biography]
end
# genres
genres = params[:genres]
unless genres.nil?
ActiveRecord::Base.transaction do
# delete all genres for this band first
unless band.id.nil? || band.id.length == 0
band.genres.delete_all
end
# loop through each instrument in the array and save to the db
genres.each do |genre_id|
g = Genre.find(genre_id)
band.genres << g
end
end
end
band.updated_at = Time.now.getutc
band.save
return band
end
def limit_to_three_genres
if self.genres.count > 3
errors.add(:genres, "No more than 3 genres are allowed.")
end
end
end
end