221 lines
6.0 KiB
Ruby
221 lines
6.0 KiB
Ruby
module JamRuby
|
|
class Band < ActiveRecord::Base
|
|
include Tire::Model::Search
|
|
include Tire::Model::Callbacks
|
|
|
|
attr_accessible :name, :website, :biography, :city, :state, :country
|
|
|
|
self.primary_key = 'id'
|
|
|
|
# musicians
|
|
has_many :band_musicians, :class_name => "JamRuby::BandMusician"
|
|
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"
|
|
|
|
# recordings
|
|
has_many :band_recordings, :class_name => "JamRuby::BandRecording", :foreign_key => "band_id"
|
|
has_many :recordings, :through => :band_recordings, :class_name => "JamRuby::Recording", :foreign_key => "recording_id"
|
|
|
|
# likers
|
|
has_many :likers, :class_name => "JamRuby::BandLiker", :foreign_key => "band_id", :inverse_of => :band
|
|
has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id"
|
|
|
|
# followers
|
|
has_many :followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id", :inverse_of => :band
|
|
has_many :inverse_followers, :through => :followers, :class_name => "JamRuby::User", :foreign_key => "follower_id"
|
|
|
|
# invitations
|
|
has_many :invitations, :inverse_of => :band, :class_name => "JamRuby::BandInvitation", :foreign_key => "band_id"
|
|
|
|
# music_sessions
|
|
has_many :music_sessions, :class_name => "JamRuby::MusicSession", :foreign_key => "band_id"
|
|
|
|
def liker_count
|
|
return self.likers.size
|
|
end
|
|
|
|
def follower_count
|
|
return self.followers.size
|
|
end
|
|
|
|
def recording_count
|
|
return self.recordings.size
|
|
end
|
|
|
|
def session_count
|
|
return self.music_sessions.size
|
|
end
|
|
|
|
def location
|
|
# TODO: implement a single string version of location;
|
|
# this will be indexed into elasticsearch and returned in search
|
|
return "#{self.city}, #{self.state}, #{self.country}"
|
|
end
|
|
|
|
def add_member(user_id, admin)
|
|
BandMusician.create(:band_id => self.id, :user_id => user_id, :admin => admin)
|
|
end
|
|
|
|
# helper method for creating / updating a Band
|
|
def self.save(id, name, website, biography, city, state, country, genres, user_id, photo_url, logo_url)
|
|
|
|
user = User.find(user_id)
|
|
|
|
# new band
|
|
if id.nil?
|
|
|
|
# ensure person creating this Band is a Musician
|
|
unless user.musician?
|
|
raise JamRuby::PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
|
end
|
|
|
|
validate_genres(genres, false)
|
|
band = Band.new()
|
|
|
|
# band update
|
|
else
|
|
validate_genres(genres, true)
|
|
band = Band.find(id)
|
|
|
|
# ensure user updating Band details is a Band member
|
|
unless band.users.exists? user
|
|
raise PermissionError, ValidationMessages::USER_NOT_BAND_MEMBER_VALIDATION_ERROR
|
|
end
|
|
end
|
|
|
|
# name
|
|
unless name.nil?
|
|
band.name = name
|
|
end
|
|
|
|
# website
|
|
unless website.nil?
|
|
band.website = website
|
|
end
|
|
|
|
# biography
|
|
unless biography.nil?
|
|
band.biography = biography
|
|
end
|
|
|
|
# city
|
|
unless city.nil?
|
|
band.city = city
|
|
end
|
|
|
|
# state
|
|
unless state.nil?
|
|
band.state = state
|
|
end
|
|
|
|
# country
|
|
unless country.nil?
|
|
band.country = country
|
|
end
|
|
|
|
# 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 genre in the array and save to the db
|
|
genres.each do |genre_id|
|
|
g = Genre.find(genre_id)
|
|
band.genres << g
|
|
end
|
|
end
|
|
end
|
|
|
|
# photo url
|
|
unless photo_url.nil?
|
|
band.photo_url = photo_url
|
|
end
|
|
|
|
# logo url
|
|
unless logo_url.nil?
|
|
band.logo_url = logo_url
|
|
end
|
|
|
|
band.updated_at = Time.now.getutc
|
|
band.save
|
|
|
|
# add the creator as the admin
|
|
if id.nil?
|
|
BandMusician.create(:band_id => band.id, :user_id => user_id, :admin => true)
|
|
end
|
|
|
|
return band
|
|
end
|
|
|
|
### Elasticsearch/Tire integration ###
|
|
#
|
|
# Define the name based on the environment
|
|
# We wouldn't like to erase dev data during
|
|
# test runs!
|
|
#
|
|
index_name("#{Environment.mode}-#{Environment.application}-bands")
|
|
|
|
def to_indexed_json
|
|
{
|
|
:name => name,
|
|
:logo_url => logo_url,
|
|
:photo_url => photo_url,
|
|
:location => location
|
|
}.to_json
|
|
end
|
|
|
|
class << self
|
|
def create_search_index
|
|
Tire.index(Band.index_name) do
|
|
create(
|
|
:settings => Search.index_settings,
|
|
:mappings => {
|
|
"jam_ruby/band" => {
|
|
:properties => {
|
|
:logo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false },
|
|
:photo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false},
|
|
:name => { :type => :string, :boost => 100},
|
|
:location => { :type => :string },
|
|
}
|
|
}
|
|
}
|
|
)
|
|
end
|
|
end
|
|
|
|
def delete_search_index
|
|
search_index.delete
|
|
end
|
|
|
|
def search_index
|
|
Tire.index(Band.index_name)
|
|
end
|
|
end
|
|
### Elasticsearch/Tire integration
|
|
|
|
private
|
|
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_BAND
|
|
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_MINIMUM_NOT_MET
|
|
end
|
|
|
|
if genres.size > Limits::MAX_GENRES_PER_BAND
|
|
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_LIMIT_EXCEEDED
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|