2012-10-01 21:27:32 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class Band < ActiveRecord::Base
|
2012-11-07 13:10:41 +00:00
|
|
|
include Tire::Model::Search
|
|
|
|
|
include Tire::Model::Callbacks
|
2012-10-01 21:27:32 +00:00
|
|
|
|
2012-11-03 19:32:27 +00:00
|
|
|
attr_accessible :name, :website, :biography
|
|
|
|
|
|
2012-10-28 02:35:28 +00:00
|
|
|
self.primary_key = 'id'
|
2012-10-01 21:27:32 +00:00
|
|
|
|
2012-10-30 05:42:16 +00:00
|
|
|
# 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"
|
2012-10-01 21:27:32 +00:00
|
|
|
|
2012-11-04 22:54:53 +00:00
|
|
|
# followers
|
|
|
|
|
has_many :followers, :class_name => "JamRuby::BandFollower", :foreign_key => "band_id"
|
|
|
|
|
has_many :inverse_followers, :through => :followers, :source => :user, :class_name => "JamRuby::Band", :foreign_key => "follower_id"
|
|
|
|
|
|
2012-11-03 19:32:27 +00:00
|
|
|
after_save :limit_to_three_genres
|
|
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
def photo_url
|
|
|
|
|
# TODO: move image path to config
|
2012-10-30 05:42:16 +00:00
|
|
|
@photo_url = "http://www.jamkazam.com/images/bands/photos/#{self.id}.gif"
|
2012-10-29 10:45:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def logo_url
|
|
|
|
|
# TODO: move image path to config
|
2012-10-30 05:42:16 +00:00
|
|
|
@logo_url = "http://www.jamkazam.com/images/bands/logos/#{self.id}.gif"
|
2012-10-29 10:45:47 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-08 04:06:58 +00:00
|
|
|
<<<<<<< HEAD
|
2012-11-06 04:47:50 +00:00
|
|
|
def follower_count
|
|
|
|
|
return self.followers.size
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-08 04:06:58 +00:00
|
|
|
=======
|
2012-11-07 13:10:41 +00:00
|
|
|
def location
|
|
|
|
|
# TODO: implement a single string version of location;
|
|
|
|
|
# this will be indexed into elasticsearch and returned in search
|
|
|
|
|
return "Austin, TX"
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-08 04:06:58 +00:00
|
|
|
>>>>>>> elasticsearch
|
2012-11-03 13:54:55 +00:00
|
|
|
# helper method for creating / updating a Band
|
|
|
|
|
def self.save(params)
|
2012-11-03 19:32:27 +00:00
|
|
|
if params[:id].nil?
|
|
|
|
|
band = Band.new()
|
|
|
|
|
else
|
|
|
|
|
band = Band.find(params[:id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# name
|
|
|
|
|
unless params[:name].nil?
|
|
|
|
|
band.name = params[:name]
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-04 22:54:53 +00:00
|
|
|
# website
|
2012-11-03 19:32:27 +00:00
|
|
|
unless params[:website].nil?
|
|
|
|
|
band.website = params[:website]
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-04 22:54:53 +00:00
|
|
|
# biography
|
2012-11-03 19:32:27 +00:00
|
|
|
unless params[:biography].nil?
|
|
|
|
|
band.biography = params[:biography]
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-06 02:55:08 +00:00
|
|
|
# city
|
|
|
|
|
unless params[:city].nil?
|
|
|
|
|
band.city = params[:city]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# state
|
|
|
|
|
unless params[:state].nil?
|
|
|
|
|
band.state = params[:state]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# country
|
|
|
|
|
unless params[:country].nil?
|
|
|
|
|
band.country = params[:country]
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-03 19:32:27 +00:00
|
|
|
# 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
|
|
|
|
|
|
2012-11-04 22:54:53 +00:00
|
|
|
# loop through each genre in the array and save to the db
|
2012-11-03 19:32:27 +00:00
|
|
|
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
|
2012-11-03 13:54:55 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-03 19:32:27 +00:00
|
|
|
def limit_to_three_genres
|
|
|
|
|
if self.genres.count > 3
|
|
|
|
|
errors.add(:genres, "No more than 3 genres are allowed.")
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-11-07 13:10:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
### 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
|
2012-10-01 21:27:32 +00:00
|
|
|
end
|
|
|
|
|
end
|