Merge branch 'master' of bitbucket.org:jamkazam/jam-ruby

This commit is contained in:
Seth Call 2012-12-02 08:23:38 -06:00
commit ccce3297db
5 changed files with 53 additions and 5 deletions

View File

@ -1,9 +1,13 @@
module Limits
# genres
# band genres
MIN_GENRES_PER_BAND = 1
MAX_GENRES_PER_BAND = 3
# recording genres
MIN_GENRES_PER_RECORDING = 1
MAX_GENRES_PER_RECORDING = 3
# instruments
MIN_INSTRUMENTS_PER_MUSICIAN = 1
MAX_INSTRUMENTS_PER_MUSICIAN = 5

View File

@ -5,6 +5,8 @@ module JamRuby
self.primary_key = 'id'
BAND_INVITATION_FAN_RECIPIENT_ERROR = "A Band invitation can only be sent to a Musician."
belongs_to :receiver, :inverse_of => :received_band_invitations, :foreign_key => "user_id", :class_name => "JamRuby::User"
belongs_to :sender, :inverse_of => :sent_band_invitations, :foreign_key => "creator_id", :class_name => "JamRuby::User"
belongs_to :band, :inverse_of => :invitations, :foreign_key => "band_id", :class_name => "JamRuby::Band"
@ -16,7 +18,7 @@ module JamRuby
# ensure recipient is a Musician
user = User.find(user_id)
unless user.musician?
raise JamRuby::JamArgumentError, "A Band invitation can only be sent to a Musician."
raise JamRuby::JamArgumentError, BAND_INVITATION_FAN_RECIPIENT_ERROR
end
band_invitation = BandInvitation.new()

View File

@ -6,6 +6,9 @@ module JamRuby
# bands
has_and_belongs_to_many :bands, :class_name => "JamRuby::Band", :join_table => "bands_genres"
# genres
has_and_belongs_to_many :recordings, :class_name => "JamRuby::Recording", :join_table => "recordings_genres"
# music sessions
has_and_belongs_to_many :music_sessions, :class_name => "JamRuby::MusicSession", :join_table => "genres_music_sessions"

View File

@ -91,13 +91,13 @@ module JamRuby
private
def require_at_least_one_genre
if genres.count < Limits::MIN_GENRES_PER_BAND
if genres.count < Limits::MIN_GENRES_PER_RECORDING
errors.add(:genres, ValidationMessages::GENRE_MINIMUM_NOT_MET)
end
end
def limit_to_three_genres
if genres.count > Limits::MAX_GENRES_PER_BAND
if genres.count > Limits::MAX_GENRES_PER_RECORDING
errors.add(:genres, ValidationMessages::GENRE_LIMIT_EXCEEDED)
end
end

View File

@ -6,12 +6,15 @@ module JamRuby
has_many :musician_recordings, :class_name => "JamRuby::MusicianRecording"
has_many :band_recordings, :class_name => "JamRuby::BandRecording"
# genres
has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "recordings_genres"
# favorites
has_and_belongs_to_many :user_favorites, :class_name => "JamRuby::UserFavorite", :join_table => "users_favorites"
validates :description, presence: true, length: { maximum: 200 }
def self.save(id, is_public, description, updater_id, owner_id, is_band)
def self.save(id, is_public, description, genres, updater_id, owner_id, is_band)
creator = User.find(updater_id)
@ -25,9 +28,11 @@ module JamRuby
end
if id.nil?
validate_genres(genres, false)
recording = Recording.new()
recording.creator_id = updater_id
else
validate_genres(genres, true)
recording = Recording.find(id)
end
@ -43,6 +48,22 @@ module JamRuby
recording.description = description
end
# genres
unless genres.nil?
ActiveRecord::Base.transaction do
# delete all genres for this recording first
unless recording.id.nil? || recording.id.length == 0
recording.genres.delete_all
end
# loop through each genre in the array and save to the db
genres.each do |genre_id|
g = Genre.find(genre_id)
recording.genres << g
end
end
end
recording.updated_at = Time.now.getutc
# TODO: wrap in transaction with statements below
@ -78,6 +99,24 @@ module JamRuby
end
end
def self.validate_genres(genres, is_nil_ok)
if is_nil_ok && genres.nil?
return
end
if genres.nil?
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_MINIMUM_NOT_MET
else
if genres.size < Limits::MIN_GENRES_PER_RECORDING
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_MINIMUM_NOT_MET
end
if genres.size > Limits::MAX_GENRES_PER_RECORDING
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_LIMIT_EXCEEDED
end
end
end
=begin
def self.delete(id, owner_id, is_band)
if is_band?